I need to write regex for the following rules in which button (a,b,start) are the only field required, all other groups are optional but having problems. Some look-behind logic is also required which I am having problem with as well
Examples:
My regexp:
^(a|b|start)(\d*%)?(\d*(s|ms))?
Here is the test bed https://regex101.com/r/jGf0VR/3
The regex is not working for me with the following problems
(\d*%)?
- this group is not working, suppose to capture 500%, 200% but it is not working (yes tried it with backslash still does not work). The fact it should be optional but it is never taken as optional
a200ms300s
- should be captured but it is not captured, similarly a200s300ms
should also be captured
a200ms200%
- should be invalid, I can't seem to fix this one primarily because of (\d*%)?
is not working for me as highlighted in prob1
Use the following to match a string containing a single button press:
^(a|b|start)(\d+%)?(\d+(s|ms))?$
I changed \d*
to \d+
to indicate that at least one digit is required before %
or s|ms
. I added $
at the end so it won't match if there's something extra after the match.
To match a sequence of them, put a group around everything except the anchors, and allow it to repeat.
^((a|b|start)(\d+%)?(\d+(s|ms))?)+$