-edit- NOTE the ?
at the end of .{2,}?
I found out you can write
.{2,}?
Isnt that exactly the same as bel
No. {2,}
means two times or more while {2}
means exactly two times. Quantifiers are greedy by default, so given the string foo
you would get foo
if you use .{2,}
, but fo
if you use .{2,}?
because you made it lazy. However, the latter is allowed to match more than two times if necessary, but .{2}
always means exactly two characters.
So if you have the string test123
and the pattern .{2,}?\d
, you would get test1
because it has to match up to four characters so the \d
can also match.