Reqular expression begin and end of string
I need c开发者_StackOverflow中文版heck that string begins with a digit and ends with a digit, even the length is one symbol.
Examples:
"1" => true
"1a1" => true
"1a" => false
"a1" => false
This should work:
^[0-9](.*[0-9])?$
Try something like:
^[0-9].*?[0-9]$ | ^[0-9]+$
The first part matches digit something digit, the second digits
If you are using perl, the look behind assetions may be used
^[0-9].*(?<=[0-9])$
If you need to use standard regex you can use:
^[0-9](.*[0-9])?$
more details about look behind see here
Maybe this would work for you:
^\d[^&]*\d$
^\d.*\d$|^\d$
Assuming ^
and $
mean the beginning and the end of the string in your regex dialect. Otherwise use \A
and \Z
or whatever your regex dialect uses for the beginning and the end of the string.
Or in cases like java's match
, where regexen are anchored by default, you can just use \d.*\d|\d
.
If you want to check that you have only one digit, something like this should work :
^[0-9]$
edit : since you've edited your post, here's a new proposition :
^([0-9].*)?[0-9]$
You might want to read the CodeProject articles on RegEx here and here.
精彩评论