I am trying to capture a certain number chunk from within a text. Suppose the text is Kane is 12345 feet high. I want to capture 12345. I am trying to use this:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String text = "Kane is 12345 feet high";
Pattern p = Pattern.compile("Kane is (\\d+) feet high");
Matcher m = p.matcher(text);
String s0 = m.group(0);
However I am getting a Match not found error. What am I doing wrong here? I mean, in Perl, this perfectly prints out 12345:
$foo = "Kane is 12345 feet high";
$foo =~ /Kane is (\d+) feet high/;
print $1;
you have to invoke Matcher.find() before calling Matcher.group()
i think, your Regex should be like below in order to just capture the number in your text.