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.
Asked
Active
Viewed 257 times
0

Gordon
- 6,257
- 6
- 36
- 89
-
1You probably meant `^.*` by showing `^.?`. – revo Jun 18 '18 at 22:28
-
or maybe `^[^\.]*` – Mathias R. Jessen Jun 18 '18 at 22:31
-
^.* works just fine. Maybe it is what the OP is looking for – The Lyrist Jun 18 '18 at 22:32
-
Huzzah! That's it @revo. But, what is the difference between .* and .? ? I thought .? was any character, any number of times. – Gordon Jun 18 '18 at 22:32
-
@Gordon I made it an answer below. – revo Jun 18 '18 at 22:45
-
@MathiasR.Jessen Could be but chances are it couldn't since OP states *any possible characters*. – revo Jun 18 '18 at 22:48
1 Answers
1
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.

revo
- 47,783
- 14
- 74
- 117