开发者

Reqular expression begin and end of string

开发者 https://www.devze.com 2023-02-13 13:00 出处:网络
I need c开发者_StackOverflow中文版heck that string begins with a digit and ends with a digit, even the length is one symbol.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号