I spent lot time figuring out a simple regex to return a group (only 1st group).
So the string can be –
“No purchase required” or “Purchase of $50.00 worth groceries is required.”
I am trying to write a regex which can parse “No” or “50” based on the given string.
This is what I have written.
(?:(No) monthly maintenance|Purchase of \$([\d\.]+ worth groceries)
This works fine but I want my output as 1st group/group 1 only.
Why not just use
/(?:No monthly maintenance|Purchase of $([0-9.]+) worth groceries)/.The match will fail if it’s not in one of those formats, and Group 1 matches
''for the “No monthly maintenance” case, or the number for other case.If you really need to capture the string
Noor the number, you might need to get a little more complicated and do something like: