Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a string unless it contains something?

I have a PowerShell script that will get a list of all files within a folder and then (based on regex matches within a Switch statement) will move each file to a specified folder (depending on the regex match).

I'm having an issue with a particular list. A group of files (PDF files named after their part number) that begin with "40" get moved to a specified folder.

The regex itself for just THAT is easy enough for me, the problem I am having is that, IF the file contains _ol OR _ol_ then it cannot be a match.

For example, the file names below should all match:

401234567.pdf
401234567a.pdf
401234567_a.pdf
401234567a_something.pdf

Those below should NOT match:

401234567_ol.pdf
401234567_ol_something.pdf

Using a ^(?i)40\w+[^_ol].pdf$ regex is the closest it seems I can get. It will negate the 401234567_ol.pdf as being a match; however, it accepts the 401234567_ol_something.pdf. Does anybody know how I can negate that as being a match as well?

like image 955
Lost Avatar asked May 27 '15 14:05

Lost


2 Answers

You can use a negative look ahead in your regex. The following regex will match any string that doesn't contains _ol:

^((?!_ol).)*$

DEMO

Note that you need to use modifier m (multiline) for multiline string.

like image 103
Mazdak Avatar answered Oct 29 '22 17:10

Mazdak


Use a negative look-ahead:

^(?i)(?!.*_ol)40\w+\.pdf$

See demo

The look-ahead (?!.*_ol) in the very beginning of the pattern check if later in the string we do not have _ol. If it is present, we have no match. Dot must be escaped to match a literal dot.

like image 34
Wiktor Stribiżew Avatar answered Oct 29 '22 17:10

Wiktor Stribiżew