Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中获取两个字符串之间数据的正则表达式_Java_Regex - Fatal编程技术网

在java中获取两个字符串之间数据的正则表达式

在java中获取两个字符串之间数据的正则表达式,java,regex,Java,Regex,我试图使用正则表达式从下面的字符串中获取两个字符串“Account:”和“Account”之间的数据 String str= "Order Confirmation Account: Sample Account ID: 1111" 我使用的正则表达式是: Pattern pattern = Pattern.compile("Account:(.*)Account"); Matcher matcher = pattern.matcher(str); System

我试图使用正则表达式从下面的字符串中获取两个字符串“Account:”和“Account”之间的数据

    String str= "Order Confirmation Account: Sample Account ID: 1111"
我使用的正则表达式是:

    Pattern pattern = Pattern.compile("Account:(.*)Account");
    Matcher matcher = pattern.matcher(str);
    System.out.println("Account name is:"+matcher.group(1));
我得到的实际输出是:

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)
预期产出为:

Sample

正则表达式有什么问题?

模式需要匹配整个字符串,所以

".*Account:(.*)Account.*"
if(matcher.find()){System.out.println(“帐户名:”+matcher.group(1));}