I’m having problems getting my regular expression working.
The regular expression is:
([0-9]m)* ([0-9]f)*
A digit must come before “m” or “f” but “m” or “f” are optional. Example are:
1m 2f
1m
6f
What have I done wrong?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
*means match the previous token 0 or more times, which doesn’t look like what you want.These should help you to build the regular expression you need:
?to mean 0 or 1 matches.|for alternation.(?:...)for a non-capturing group.^and$to anchor at the start and end of the string.Knowing that, I imagine that you can probably find a solution by yourself, but for the sake of completion, I’ll show one possible solution.
Your question isn’t very clear so I’m just going to assume that you want the following to match:
and that you want the following to fail to match:
If those assumptions are incorrect, then please make your question more clear.
With those assumptions, try this:
If you also want
2f 3mto match then use this: