tags:

views:

38

answers:

3

Hi all,

I'm still a noob with reqular expression, so I need a little help. I need to capture either

\d+\.\d+

or

\d+

but nothing else. For instance, "0.02", "1" and "0.50" should match positively. After few tests, I noticed that I cannot simply put something like

[\d+\.\d+|\d+]

It'll be highly appreciated, if somebody could show me, how to do this.

Many, many thanks in advance,
nhaa123

+1  A: 
(\d+\.\d+|\d+)

should do the trick.

KiNgMaR
Of course. Thank you very much.
nhaa123
A: 

Or \d+(\.\d+)? if you find that easier to read :)

jensgram
+1  A: 

You can do either:

(\d+|\d+\.\d+)

or

(\d+(\.\d+)?)

but that creates a second capturing group. The more sophisticated version is:

(\d+(?:\.\d+)?)

That's called a non-capturing group.

By the way Regular Expression Info is a superb site for regular expression tutorials and information.

cletus
http://www.regexbuddy.com/ is a nice tool if you use a lot of regular expressions - it's not free, though.
TrueWill