I'm really bad at regex and was wondering if anyone could help me with this. I need to accept a value where $,€, and £ are allowed but not required with a min 5.00 (.00 is not required but is allowed) and 2,000 ( comma is not required but is allowed).
Some examples of valid inputs
$5
€5
£5
$5.00
5
1000
1,000
1,000.00
If you want to allow values like £5.32:
^[$€£]?(\d+([\.,]\d{2})?)$
If not:
^[$€£]?(\d+([\.,]00)?)$
Once you've checked whether it's formatted correctly using the regex, you can go on to check whether 5 < int(value) < 2000
using the first returned group of the regex.
EDIT: Having thought about it, here's a regex which will take care of the bounding too:
^[$€£]?((([1-5],?)?\d{2,3}|[5-9])(\.\d{2})?)$
RegexMagic to the rescue:
\b(?:[$£€])? *(?:2,?000|1,?[0-9]{3}|[1-9][0-9]{1,2}|[5-9])(?:\.[0-9]{1,2})?\b
Do you agree that doing all this in a regex (while possible) is not really a very good idea?
精彩评论