1

I want to extract 12345 Rial from the String s :

String regx= ".* (\\d*) Rial";
String  s = "your balance is 12345 Rial your last"
Pattern pattern = Pattern.compile(regx);
Matcher matcher = pattern.matcher(s);
System.out.println(matcher.group(1));

but the following exception is being thrown

java.lang.IllegalStateException: No match found
 at java.util.regex.Matcher.group(Unknown Source)
 at ir.dena.avl.server.util.modbus.ChargeMessage.getBalanceFromIranCell(ChargeMessage.java:95)

Can anyone point out why?

2
  • Do you want to extract both the number and the adjacent word in the same regex? Commented Jun 18, 2014 at 15:22
  • no i only want to extract number Commented Jun 18, 2014 at 15:37

1 Answer 1

1

Pattern first needs to be found before you will be able to use it. You need to use either find or matches first methods from Matcher class to traverse string and get match.

So use

...
if (matcher.find())
    System.out.println(matcher.group(1));

or if you want to make sure that entire string is matched by regex

...
if (matcher.matches())
    System.out.println(matcher.group(1));
Sign up to request clarification or add additional context in comments.

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.