Find uppercase letters within <p></p> tags using regex

蓝咒 提交于 2019-12-25 06:51:45

问题


I want to use a regular expression to grab ONLY uppercase characters within a <p></p> html tag.

<p>I WANT TO GET THIS TEXT</p>

<p>I don't want to get this text because it has some Lower Case Characters</p>

Sometimes the document doesn't contain this at all. Sometimes it's there 3-5 times. The document DOES contain other HTML within <p></p> tags which I don't want to change. Only the paragraph tags which contain ALL Uppercase characters I want to return.

I not very familiar with regular expressions so this is stumping me. It seems like I should be able to do something like this: <p>[A-Z]</p>

Once I find the uppercase text, I want to pass the data through a ProperCase function and re-insert the data as a <H3> tag.


回答1:


Try this:

(<p>)([^a-z]+)(</p>)

This evaluates to:

  • true for <p>I WANT TO GET THIS TEXT</p>
  • true for <p>I DON'T WANT TO GET MIXED CASE TEXT 123.</p>
  • false for <p>I don't want to get this text because it has some Lower Case Characters</p>



回答2:


well I am not familiar with visual-studio. but you can use the regex below:

(?<=<p>)[A-Z ]*(?=</p>)

testing with grep:

kent$  echo "<p>I WANT TO GET THIS TEXT</p>"|grep -Po '(?<=<p>)[A-Z ]*(?=</p>)'   

output

I WANT TO GET THIS TEXT

if there are some small letters:

kent$  echo "<p>BIGBIG BIG and some small letters</p>"|grep -Po '(?<=<p>)[A-Z ]*(?=</p>)'

(output nothing)


来源:https://stackoverflow.com/questions/8482362/find-uppercase-letters-within-p-p-tags-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!