I want to check whether there is string starting from number and then optional character with the help of the regex.So what should be the regex for matching the string which must be started with number and then character might be there or not.Like there is string "30a"
or "30"
it should be matched.But if there is "a"
or some else character or sereis of characters, string should not be matched.
views:
57answers:
2^\d.*
The ^
matches the start of the string, \d
matches a single digit, and then the .*
matches any number of additional characters.
Thus, the net result is that it will only match if the string begins with a digit.
Sounds like there should be able to be any number of numeric characters at the beginning followed by optional other characters. To match any other character after a series of numbers at the beginning I would use:
\d+.*
To match only alpha numeric characters after the mandatory numeric beginning I would use:
\d+\w*
Note: as pointed out by Dav, if you add a ^
to the start of the expression and a $
to the end of the expression like this ^\d+\w*$
you will ensure the whole string matches. However if you leave those off, you will be able to search the input string for what you need. It just depends on what your needs are.