What is a correct regex expression to match exactly "1/2" or "50%"?
Matches:
1/2 1/10 10/23 13/30 50% 2% No matches:
0/2 1/0 02/15 50 4 1/2% I've tried /^([0-9]+/[0-9]+)|([0-9]+%)$/ but without success. I need to get exact matches
My attempt: ^(([1-9][0-9]*/[1-9][0-9]*)(?!%))|((?<!/)([1-9][0-9]*%))$ - https://regex101.com/r/rHvyKR/1
If you don't want to allow for a single 0, then you can't use [0-9]+, you need to demand one character out of [1-9] first, and then [0-9]* optional after that. And I threw in a Negative Lookahead and a Negative Lookbehind, to exclude the edge cases.
^ and $ used in the question, I'm guessing that lines like 1/2abc and abc50% should not match but your regex does not exclude them (you need extra parentheses around the | or duplicate ^...$ around each case)ab|cd when you need a(b|c)d or abd|acdAssuming that the whitespace in your first "Matches" line is a typo, you could do:
^[1-9][0-9]*(%|/[1-9][0-9]*)$ It is not clear if 1/234, 345/6, 78900%, etc, should match. I've assumed that they should. Note that excluding the second example (two numbers separated by slash, where first number is not smaller than second) is a bit complicated with basic regex syntax.
^[1-9][0-9]*(?:%|\/[1-9][0-9]*)$/1\/2|50%/