Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match text that doesn't start with substring?

Tags:

regex

I have text with file names scattered throughout. The filenames appear in the text like this:

|test.txt|
|usr01.txt|
|usr02.txt|
|foo.txt|

I want to match the filenames that don't start with usr. I came up with (?<=\|).*\.txt(?=\|) to match the filenames, but it doesn't exclude the ones starting with usr. Is this possible with regular expressions?

like image 267
Steven Avatar asked Jul 10 '26 12:07

Steven


1 Answers

(?<=\|)(?!usr).*\.txt(?=\|)

You were nearly there :)

Now you have a positive lookbehind, and a positive and negative lookahead.

like image 193
Tim Pietzcker Avatar answered Jul 13 '26 09:07

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!