-3

I am trying to get data between the two strings "Account:" and "Account" from the below string using regular expression.

    String str= "Order Confirmation Account: Sample Account ID: 1111"

The regular expression I used is:

    Pattern pattern = Pattern.compile("Account:(.*)Account");
    Matcher matcher = pattern.matcher(str);
    System.out.println("Account name is:"+matcher.group(1));

The actual output I get is:

java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Unknown Source)
at WorkingProgram$1.run(WorkingProgram.java:102)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

The expected output is:

Sample

What is wrong with my regular expression?

1 Answers1

-1

The pattern needs to match the whole String, so

".*Account:(.*)Account.*"
daniu
  • 12,131
  • 3
  • 23
  • 46