Reputation: 6873
Given a file name of 22-PLUMB-CLR-RECTANGULAR.0001.rfa
I need a RegEx to match it. Basically it's any possible characters, then . and 4 digits and one of four possible file extensions.
I tried ^.?\.\d{4}\.(rvt|rfa|rte|rft)$
, which I would have thought would be correct, but I guess my RegEx understanding has not progressed as much as I thought/hoped. Now, .?\.\d{4}\.(rvt|rfa|rte|rft)$
does work and the ONLY difference is that I am not specifying the start of the string with ^. In a different situation where the file name is always in the form journal.####.txt
I used ^journal\.\d{4}\.txt$
and it matched journal.0001.txt
like a champ. Obviously when I am specifying a specific string, not any characters with .? is the difference, but I don't understand the WHY.
Upvotes: 0
Views: 299
Reputation: 48741
That never matches the mentioned string since ^.?
means match beginning of input string then one optional single character. Then it looks for a sequence of dots and digits and nothing's there. Because we didn't yet pass the first character.
Why does it work without ^
? Because without ^
it is allowed to go through all characters to find a match and it stops right before R
and continues matching up to the end.
That's fine but with first approach it should be ^.*
. Kleene star matches every thing greedily then backtracks but ?
is the operator here which makes preceding pattern optional. That means one character, not many characters.
Upvotes: 1