Multiple negative regex expression
Here's my regex pattern: [Ss]ection\s\d+(?![a-zA-z])(?!</ref>)
For example, it must match: section 5
orsection 50
For example, it must match not : section 5A
or section 5</ref>
or section 5A</ref>
orsection 50A
The problem is that it doesn't actually match them: http://regexr.com?33ien
Not sure what's wrong with the template though ...
source to share
Maybe try it [Ss]ection\s\d++(?![a-zA-z])(?!</ref>)
. ++ is a possessive quantifier . This quantifier is similar to the greedy quantifier, except that it blocks the chunk of the string that it matched using the later part of the regular expression.
Example
System.out.println("ababab".matches("(ab)++ab"));
// prints false since last "ab" is possessed by (ab)++
source to share
Wrong matches: In your regex, you want a "section" followed by one or more digits, not followed by any text, or "
This is true for section 50A
:
section 5
follows 0A
, and this does not apply to your negative view.
You can do something like:
[Ss]ection\s\d+(?![a-zA-Z0-9])(?!</ref>)
source to share
This should work:
[Ss]ection\s\d+(?!\d)(?![a-zA-z])(?!</ref>)
I explained the problem with our regexp regexp weirdness with negative lookahead assertion in Java regex , it applies here too.
The situation is slightly different here: a negative lookahead matches when we don't want it, because the match tends to accept a shorter match for the preview part if it helps match the expression as a whole. So it's important to be aware of the input boundary if you are using a lookahead: be it a word boundary, an anchor, $
or some statement about the next text (without looking at the number in my proposed solution).
source to share