I need to specify in a regular expression to match all positive and negative numbers. I want it to match only a single - at the start, but if its not present (i.e in a positive number) that should work too.
If I try:
^[-][0-9]+$
This matches only -100, -200, but not 200, 100, etc. How can I change it to match both 100, 200, -100, but not –100?
You need the
?modifier (means zero or one). i.e.-?or-{0,1}.