Home > Mobile >  Attempting to create a negative look ahead to exclude strings of some specific codes but allow all o
Attempting to create a negative look ahead to exclude strings of some specific codes but allow all o

Time:06-10

I am searching a large number of text log files that contain various negative error codes, mostly 4-digits preceded with a minus sign, eg -3304, -3315, etc. I would like to exclude a small set of these error codes while allowing all others to match.

I have tried to use an adaptation from this answer:

 "^/(?!-3301|-3304|-3306|-3308|-3309)(-[0-9]{4})"

To exclude -3301, -3304, -3306, -3308, -3309 but match all other 4-digit negative strings. I know there are matches, such as -3220 in the set of files, but they are not matching.

What have I missed in this attempt at a negative look ahead?

CodePudding user response:

You have some a / char before the number matching pattern that is not supposed to be part of a negative number. Besides, you are only matching at the start of string with ^.

Note that you match more than 4-digit numbers with that pattern, you need a digit boundary on the right.

You can use

-(?!330[14689])\d{4}(?!\d)

Details:

  • - - a hyphen
  • (?!330[14689]) - on the right, there should be no 330 and then a digit from 14689 set
  • \d{4} - four digits
  • (?!\d) - no digit allowed immediately on the right.
  • Related