Link to home
Start Free TrialLog in
Avatar of Star79
Star79Flag for United States of America

asked on

Need a regular expression in c#

Hello,

I need a regular expression in c# which accepts + only in the begining. Currently i am using this
[-+]?[0-9]*  but this will allow special character + to be inserted anywhere in the string.

Regards,
Rithesh
Avatar of kaufmed
kaufmed
Flag of United States of America image

You simply need to add the beginning-of-string marker:
^[-+]?[0-9]*

Open in new window

Avatar of pmasotta
pmasotta

I think what you really need is

^[-+]?[0-9]+

Open in new window

Actually, you might need to add a bit more to the pattern. The above would still allow the following:

    +123+
    +123ABC

etc. Let's add the end-of-line-marker as well:

^[-+]?[0-9]*$

Open in new window


Change the * to a + if you require at least one digit, as pmasotta demonstrates.
the former one also match single + or - signs

+
-

this one requires a valid integer number per line

^[-+]?[0-9]*$

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial