tags:

views:

411

answers:

4

I am trying to find a good way to capture a number that is at no more than N characters away from a given string.

For example, if the String is "age" and N=4 have to find

"Age 5" => 5
"My age is 10 and I my name is John" => 10
"My age is almost 5 and I my name is Mary" => null

In the last case, the number is separated more than 4 characters from "age".

+5  A: 

What about

age[^0-9]{0,4}[0-9]+

if you want to capture the number possibly found:

age[^0-9]{0,4}([0-9]+)

?

Johannes Weiß
Perfect. Thank you. I prefer the \d syntax but I accept it :)
Guido
+3  A: 

Something like the following:

age[^\d]{,4}(\d+)

this means "age followed by 0 to 4 non-digits followed by one or more digits...capture the digits"

Rich
Thanks. In Java you get a java.util.regex.PatternSyntaxException: Illegal repetition, and "{0,4}" has to be used instead of just "{,4}"
Guido
A: 
[Aa]ge[\D]{,N}(\d+)

and then get the contents of the first group ($1).

Eric Wendelin
A: 

Expanding on the other answers here, if you need it to be within 5 characters in either direction:

/((\d+)\D{,4})?age(\D{,4}(\d+))?/i

Then:

if(matches[2] != null)
{
  if(matches[4] != null)
    return max(matches[2], matches[4]);  //or however you want to resolve this..
  else
    return matches[2];
}
return matches[4];
Kip