开发者

How to match two strings with integers greater than zero using regex?

I'm looking for a simple regex to match this:

int.int"

where the integer is greater then 0.

matches:

开发者_运维知识库
1.1"
1.5"
5.1"
40.30"
1.29"

mismatches:

1.1
0.4"
4.0"
0.30"
39.0"


You can use the following regex:

^[1-9][0-9]*\.[1-9][0-9]*"$

Rubular Link

^     : Start anchor
[1-9] : Non zero digit
[0-9]*: Zero or more of any digit 0-9
\.    : A literal period
"     : A literal "
$     : End anchor

The anchors are essential. Without them you'll match any string that has the pattern you want anywhere, say foo11.22bar. With the anchors the regex will try to match the entire string not just any proper subset of it.

. is a regex meta character which matches any character (other than newline).
To match a literal . you need to escape it as \..


Is this for .NET?

[1-9]\.[1-9]"


I've founded this "myself":

[1-9][0-9]*\.[1-9][0-9]*"

Of course with the initial help of the people who reacted. (and a fresh head in the morning :-) )

As Alan Moore commented in coaddict answer, you can also use:

[1-9]\d*\.[1-9]\d*"

Thanks for all who helped.

Bruno

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜