How To Use Regex Negative Lookahead To Exclude Strings
erics, Posted May 31st, 2019 at 1:20:12pm
I have a task in Perl to list specific files based on pattern match, those with and those without the string “_from_”.
There are two files in the directory to filter:
static-east.properties
static-east_from_west.properties
To capture the files with the _from_ string was easy:
1 |
/^static-(.*?)_from_(.*?)\.properties$/ |
To capture the files WITHOUT the _from_ string was not quite so easy until I learned about Negative Lookahead:
1 |
/^static-((?!_from_).)*?\.properties$/ |
This is a great interactive regex testing site that I found (note: I have zero affiliation with them, I just think it is a great resource):
https://www.regextester.com/15
Leave Your Comment
All fields marked with "*" are required.