Skip to content Skip to sidebar Skip to footer

Regex Non-capturing Group Is Capturing

I have this regex (?:\]*?)> The point of this regex is to capture every closing tag ('>') of an anchor that has an href t

Solution 1:

Rewrite your regex as :

(?:\<a[^*]href="(?:http://[^"]+?|[^"]+?\.pdf)"+?[^>]*?)(>)
   non capture __^^                                    ^ ^
                                             capture __|_|

As Tony Lukasavage said, there is an unnecessary non-capture group, and, moreover, there is no need to escape <, so it becomes:

  <a[^*]href="(?:http://[^"]+?|[^"]+?\.pdf)"+?[^>]*?(>)
non capture __^^                                    ^ ^
                                          capture __|_|

Solution 2:

You're conflating two distinct concepts: capturing and consuming. Regexes normally consume whatever they match; that's just how they work. Additionally, most regex flavors let you use capturing groups to pluck out specific parts of the overall match. (The overall match is often referred to as the zero'th capturing group, but that's just a figure of speech.)

It sounds like you're trying to match a whole <A> tag, but only consume the final >. That's not possible in most regex flavors, JavaScript included. But if you're using Perl or PHP, you could use \K to spoof the match start position:

(?i)<a\s+[^>]+?href="http://[^"]+"[^>]*\K>

And in .NET you could use a lookbehind (which, like a lookahead, matches without consuming):

(?i)"(?<=<a\s+[^>]+?href="http://[^"]+"[^>]*)>

Of the other flavors that support lookbehinds, most place restrictions on them that render them unusable for this task.

Solution 3:

If I'm understanding correctly that you want to match just the greater-than sign (>) that's part of the closing anchor tag, this should do it:

\<a[^*]href="(http://[^"]+?|[^"]+?\.pdf)"+?[^>]*?(>)

Solution 4:

If I'm understanding your request correctly...

\<a[^*]href="(?:http://[^"]+?|[^"]+?\.pdf)"+?[^>]*?(>)

Solution 5:

Your parentheses are around the tag itself and the href's contents, so that's what will be captured. If you need to capture the closing > then put the parenthesis around it.

Post a Comment for "Regex Non-capturing Group Is Capturing"