简体   繁体   中英

Regular Expression Lookbehind doesn't work as expected

I have a string in .net.

<p class='p1'>Para 1</p><p>Para 2</p><p class="p2">Para 3</p><p>Para 4</p>

Now, I want to get only text inside the tag p (Para 1, Para 2, Para 3, Para4).

I used the following regular expression but it doesn't give me expected result.

(?<=<p.*>).*?(?=</p>)

If I use (?<=<p>).*?(?=</p>) it will give Para 2 and Para 4 which both p tags doesn't have class attribute?

I'd like to know what's wrong with (?<=<p.*>).*?(?=</p>) that code.

Let's illustrate this using RegexBuddy :

RegexBuddy屏幕截图

Your regex matches more than you think - the dot matches any character, so it doesn't care about tag boundaries.

What it is actually doing:

  • (?<=<p.*>) : Assert that there is <p (followed by any number of characters) anywhere in the string before the current position, followed by a > .
  • .*? : Match any number of characters...
  • (?=</p>) : ...until the next occurence of </p> .

Your question is a bit unclear, but if your plan is to find text within <p> tags regardless of whether they contain any attributes, you shouldn't be using regular expressions anyway but a DOM parser, for example the HTML agility pack .

That said, if you insist on a regex, try

(?<=<p[^<>]*>)(?:(?!</p>).)*

另一个截图

Explanation:

(?<=<p[^<>]*>)  # Assert position right after a p tag
(?:(?!</p>).)*  # Match any number of characters until the next </p>

Have you tried using following expression?

<p[\s\S]*?>(?<text_inside_p>[\s\S]*?)</p>

group named text_inside_p will contain desired text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM