1

I have a string containing a number. Something like "Incident #492 - The Title Description".
I need to extract the number from this string.
Tried

Pattern p = Pattern.compile("\\d+");  
Matcher m = p.matcher(theString);  
String substring =m.group();  

By getting an error

java.lang.IllegalStateException: No match found  

What am I doing wrong?
What is the correct expression?
I'm sorry for such a simple question, but I searched a lot and still not found how to do this (maybe because it's too late here...)

2
  • Can you assert that there will not be other numbers in the String? Commented Aug 29, 2015 at 21:34
  • Yes, in my case there is only one, single number. Commented Aug 29, 2015 at 21:44

3 Answers 3

3

You are getting this exception because you need to call find() on the matcher before accessing groups:

Matcher m = p.matcher(theString);  
while (m.find()) {
    String substring =m.group();
    System.out.println(substring);
}

Demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Or matches() if theString is not allowed extraneous characters.
@Andreas That's right. OP's string allows other characters, though.
1

There are two things wrong here:

  • The pattern you're using is not the most ideal for your scenario, it's only checking if a string only contains numbers. Also, since it doesn't contain a group expression, a call to group() is equivalent to calling group(0), which returns the entire string.

  • You need to be certain that the matcher has a match before you go calling a group.

Let's start with the regex. Here's what it looks like now.

Regular expression visualization

Debuggex Demo

That will only ever match a string that contains all numbers in it. What you care about is specifically the number in that string, so you want an expression that:

  • Doesn't care about what's in front of it
  • Doesn't care about what's after it
  • Only matches on one occurrence of numbers, and captures it in a group

To that, you'd use this expression:

.*?(\\d+).*

Regular expression visualization

Debuggex Demo

The last part is to ensure that the matcher can find a match, and that it gets the correct group. That's accomplished by this:

if (m.matches()) {
    String substring = m.group(1);
    System.out.println(substring);
}

All together now:

Pattern p = Pattern.compile(".*?(\\d+).*");
final String theString = "Incident #492 -  The Title Description";
Matcher m = p.matcher(theString);
if (m.matches()) {
    String substring = m.group(1);
    System.out.println(substring);
}

2 Comments

The pattern OP is using is 100% correct, as long as you are not trying to use matches() on it. He does not need the stuff in front or behind the \\d+ part, and he does not need groups beyond the default group zero, unless he wishes to capture other parts of the string.
@dasblinkenlight: I'll rephrase it then - it's not the most ideal regex to use, then. It will work so long as you're using find, but when I author regexes, I like it to be clear that it's going to match specific things.
0

You need to invoke one of the Matcher methods, like find, matches or lookingAt to actually run the match.

Comments

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.