2

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;
2
  • You need to use matches for an exact match of the whole regex or find to find the regex in a sub-string. Unfortunately it doesn't give you a more useful error like Match not found, did you forget to call matches() or find() ;) Commented Jan 9, 2013 at 10:29
  • Also reading the API documentation is actually a good practice for a programmer. Commented Jan 9, 2013 at 10:35

3 Answers 3

8

Just instantiating Matcher isn't enough: you must call m.matches() and the standard practice would be to put it in an if:

if (m.matches()) s0 = m.group(1);
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but I think you should be using group(1), not group(0). I know the OP used group(0), but he also said he was trying to extract the number.
6

you have to invoke Matcher.find() before calling Matcher.group()

String text = "Kane is 12345 feet high";
Pattern p = Pattern.compile("Kane is (\\d+) feet high");
Matcher m = p.matcher(text);
if(m.find()){
String s0 = m.group(0);

}

Suppose the text is Kane is 12345 feet high. I want to capture 12345

i think, your Regex should be like below in order to just capture the number in your text.

    Pattern p = Pattern.compile("\\d+");

3 Comments

No, only \d+ won't suffice, for there may be numeric chunks in Kane as well, like Kane142..So in that case I will need to capture the 2nd group.
@Cupidvogel ohk.. i tought you only needed literal digits, :P
Yeah, that would look like it from my question..:P
1

Try This:

public static String stripNonDigits(final String input){
  final StringBuilder sb = new StringBuilder();
  for(int i = 0; i < input.length(); i++){
    final char c = input.charAt(i);
    if(c > 47 && c < 58){
        sb.append(c);
    }
}
return sb.toString();

} Test Code:

public static void main(final String[] args){
final String input = "0-123-abc-456-xyz-789";
final String result = stripNonDigits(input);
System.out.println(result);
 }

Output:

0123456789

Might be helpfull thanks

1 Comment

I'm sure that works fine, but it doesn't answer the question. What the OP wants to know is why his own code doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.