Reputation: 17
i have to write the Regular expression that accept two signs only + and - and can be of any no. of digit but + should be the first sign if it is there
Upvotes: 1
Views: 118
Reputation: 3827
I am assuming the following:
+
may only be at the beginning of the number-
should not be
You could try this regular expression:
^\+?(?:[\d]+\-)*[\d]+$
Passes the following string examples:
Fails the following string examples:
EDIT
As @Alovchin pointed out, the OP expressed the need to allow for dots (.
) as well in the numbers in one of the answer comments. Although this requirement does not reflect in the question I am going to add it here just-in-case.
If the requirement is to allow for dots (.
) as well then the above regex will need to be updated to the following.
^\+?(?:[\d]+[\-\.])*[\d]+$
Hope this helps.
Upvotes: 3
Reputation: 683
In C# try this one:
^\+?(?![\-\.])[\d\-\.]+(?<![\-\.])$
There's a better answer by Tanzeel Kazi.
Upvotes: 0