bruno
bruno

Reputation: 1841

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"

Upvotes: 2

Views: 853

Answers (3)

codaddict
codaddict

Reputation: 455000

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 \..

Upvotes: 5

bruno
bruno

Reputation: 1841

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

Upvotes: 0

user541686
user541686

Reputation: 210437

Is this for .NET?

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

Upvotes: 3

Related Questions