1

I have written a regex to pick files of the format (ABC.*\.DAT) in perl.

How to write a negation for the above regex?

I already tried expressions like (?!ABC.*)\.DAT or (?!(ABC.*\.DAT))

Any help is appreciated.

1 Answers1

1
(?s:(?!ABC).)*\.DAT

You can try this negation based regex. See demo.

The above can be safely embedded into a larger pattern. For example,

/^(?:(?!ABC).)*\.DAT\z/s

If you are trying to match the whole input, and if ABC doesn't end with ., .D, .DA or .DAT, then the following will be faster:

/^(?!.*ABC)\.DAT\z/s
ikegami
  • 322,729
  • 15
  • 228
  • 466
vks
  • 63,206
  • 9
  • 78
  • 110