I have Googled it, and found the following results:
- http://icfun.blogspot.com/2008/03/regular-expression-to-handle-negative.html
- http://regexlib.com/DisplayPatterns.aspx?cattabindex=2&categoryId=3
With some (very basic) Regex knowledge, I figured this would work:
r\.(^-?\d+)\.(^-?\d+)\.mcr
For parsing such strings:
- r.0.0.mcr
- r.-1.5.mcr
- r.20.-1.mcr
- r.-1.-1.mcr
But I don’t get a match on these.
Since I’m learning (or trying to learn) Regex, could you please explain why my pattern doesn’t match (instead of just writing a new working one for me)? From what I understood, it goes like so:
- Match r
- Match a period
- Match a prefix negative sign or not, and store the group
- Match a period
- Match a prefix negative sign or not, and store the group
- Match a preiod
- Match mcr
But I’m wrong, apparently :).
You are very close.
^matches the start of a string, so it should only be located at the start of a pattern (if you want to use it at all – that depends on whether you will also accept e.g.abcr.0.0.mcror not). Similarly, one can use$(but only at the end of the pattern) to indicate that you will only accept strings that do not contain anything after what the pattern matches (so that e.g.r.0.0.mcrabcwon’t be accepted). Otherwise, I think it looks good.