Issue
Say I want to match files where the second digit doesn't equal 0.
e.g.
./update110000.pt -> match
./update170000.pt -> match
./update100000.pt -> not match
so I wrote this
.*update[0-9](?!0)[0-9]{5}.pt
which passed the regex check on https://www.regexpal.com/
But when I use the find command on Linux like below it doesn't work anymore
find . -regextype sed -regex '.*update[0-9](?!0)[0-9]{5}.pt'
I also tried adding backslashes but didn't work either
find . -regextype sed -regex '.*update[0-9]\(?!0\)[0-9]{5}.pt'
I know that in this case I could simply use
find . -regextype sed -regex '.*update[0-9][1-9][0-9]{4}.pt'
which actually worked. But I still wonder how to properly use the (?!x)
syntax in the find command.
Thanks!
Solution
If you define the regex flavor as sed
, it is actually POSIX BRE that does not support lookarounds (you have a negative lookahead (?!0)
in your pattern) and also follows some specific escaping rules:
find . -regextype sed -regex '\(.*/\)\{0,1\}update[0-9][1-9][0-9]\{4\}\.pt'
Details:
\(.*/\)\{0,1\}
- an optional sequence of any text and then/
update
- a fixed string[0-9]
- a digit[1-9]
- a non-zero digit (this is instead of the lookahead)[0-9]\{4\}
- four digits** (note4
here, not5
)\.pt
- a.pt
string.
If you use POSIX ERE, you can avoid overescaping:
find . -regextype posix-extended -regex '(.*/)?update[0-9][1-9][0-9]{4}\.pt'
Answered By - Wiktor Stribiżew Answer Checked By - Marilyn (WPSolving Volunteer)