tags:

views:

152

answers:

7

Hi friends i was in need of a regex which will return only 25,36,30 for me from

25 10a 36 10b 30

as they are the only pure numbers in the sequence.

I tried this but it did not work for me :

(^|[ ])\d+|($|[ ])

Any suggestions?

A: 

Noticing your multiple regex questions might I suggest some educational reading. Regular-Expressions.info has some great tutorials for getting acquainted with RegEx.

Quintin Robinson
That will also match "10"
Greg
A: 

You can use \b for word boundary.

i guess something like:

\b\d+\b
Ikke
A: 

This one should do the work:

\b\d+\b
madgnome
+2  A: 

You could use the word boundary option:

\b(\d+)\b

but some punctuation characters will be marked as a word boundary so this won't give what you want.

So it looks like you'll have to explicitly look for whitespace:

/(^|\s+)(\d+)(\s+|$)/

When extracting the number remember to get the second group as the first will only have spaces in it.

Dave Webb
Anirudh Goel
So it does. Bother. Will find something else.
Dave Webb
A: 

If you're looking for integers only, use \b\d+\b.

spoulson
A: 

\d+ is all you need. If you're using C#, note the difference between 'Match' and 'Matches' on the Regex object. One finds a single match, the other finds multiple non-overlapping matches.

http://msdn.microsoft.com/en-us/library/30wbz966(VS.71).aspx

Jarret Hardie
+5  A: 

Apply globally:

(?<=^|\s)\d+(?=\s|$)
Tomalak