I am a total Regex newbie. I am trying to write a regex for a string that will have a hyphen, followed by any number between 1 – 9, followed by any of {mo, w, h, m, d}. I came up with:
-[1-9]+(mo)?|[h|w|d|m]
That doesn’t quite do what I want.
For ex: it matches -2wh which it shouldn’t. What am I doing wrong?
You almost got it right.. just an small change:
-([1-9]+)(mo|[hwdm])
if you want to limit the string to have that (and nothing else) you must add:
^-([1-9]+)(mo|[hwdm])$
Also note that -10h is not going to work because you excluded the zero.. if you want to include it but not as first number is as easy as this:
^-([1-9][0-9]*)(mo|[hwdm])$