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
  • 343,984
  • 15
  • 249
  • 495
vks
  • 65,133
  • 10
  • 87
  • 119