How to undo all regex?

I have a regular expression like (ma|(t){1}) . It matches ma and t and does not match bla .

I want to cancel the regex, so it should match bla , not ma and t , by adding something to this regex . I know that I can write bla , however the real regex is more complex.

+62
regex regex-negation
Apr 14 '10 at 13:25
source share
3 answers

Use a negative search: (?! pattern )

Positive images can be used to claim that the pattern matches. Negative images are the opposite: he claimed that the pattern does NOT match. Some flavors support claims; some set limits on lookbehind, etc.

Links to regular-expressions.info

see also

  • How to convert CamelCase to human readable names in Java?
  • Regex for all rows not containing a string?
  • A regular expression to match a substring that is not followed by a specific other substring.

Additional examples

These are attempts to come up with solutions to regular expressions for toy problems as exercises; they should be educational if you are trying to learn different ways to use search queries (embedding them, using them to capture, etc.):

  • codingBat plusOut using regex
  • codingBat repeatEnd using regex
  • codingbat wordEnds using regex
+66
Apr 14 2018-10-14T00:
source share

Assuming you want to completely ban strings that match the regex (i.e. mmbla is ok, but mm not), this is what you want:

 ^(?!(?:m{2}|t)$).*$ 

(?!(?:m{2}|t)$) is a negative lookahead ; he says: "Starting at the current position, the next few characters are not mm or t , and then the end of the line." The starting anchor ( ^ ) at the beginning ensures that the lookahead is applied at the beginning of the line. If it succeeds,. .* Goes ahead and consumes the string.

FYI, if you use the Java matches() method, you really don't need ^ and the final $ , but they do no harm. However, $ inside lookahead is required.

+34
Apr 14 '10 at 13:52
source share
 \b(?=\w)(?!(ma|(t){1}))\b(\w*) 

this is for a given regular expression.
\ b - find the word boundary.
a positive look ahead (? = \ w) is here to avoid spaces.
A negative view of the original regular expression is to prevent it from matching.
and finally (\ w *) must catch all the words that remain.
the group that will hold the words is group 3.
a simple (?! pattern) will not work, since any substring will correspond to a simple ^ (?! (?: m {2} | t) $). * $ will not work, since it is grit - these are full lines

0
Oct 08 '17 at 8:01
source share



All Articles