0

I am trying to put together a piece of regex to match lines that doesn't contain a certain string. So far I have the following:

/^((?!abc).)*$/igm

The issue I have is that the system I am trying to use doesn't support regex fully and so I am not able to use the ?! notation.

How would I accomplish this?

dnit13
  • 2,478
  • 18
  • 35
Andrew Davis
  • 460
  • 3
  • 17

2 Answers2

0

The notation you are trying to use is a negative lookahead assertion, and there are certain programming languages that don't support lookaround in regex, so if you are using one of those languages try to find an alternative library that support them .

If you can't enable support for lookaround assertions you simply can't negate the part of regex.

aleroot
  • 71,077
  • 30
  • 176
  • 213
0

Technically it is possible, but in a very clumsy way. Even something as simple as abc already results in a fairly obscure pattern:

^([^a]|a+[^ab]|a+b[ab]*[^abc])*[ab]*$

Fiddle: https://regex101.com/r/xH8dE9/3

Explanation:

^                    start of string
(
[^a]                 not 'a'
|
a+[^ab]              'a' but not 'ab'
|
a+b[ab]*[^abc]       'ab' but not 'abc'
)*
[ab]*$               left-overs at the end of the string

For longer words the pattern grows quadratically. Becomes particularly complicated for words with the first letter appearing more than once, e.g. adam. It would look something like this (untested):

^([^a]|a+[^ad]|a*(ad)+([^ad]|a[^adm]))*[ad]*$

You may want to use a tool to generate such patterns, rather than write them yourself.

Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45