Java正则表达式匹配IllegalStateException

我正在尝试使用匹配器,并得到一个IllegalStateException:没有匹配问题

public static final Pattern PATTERN= Pattern.compile("(d+)(|)(d+)((*)(d+)(|)(d+))?");

然后我在下面的输入字符串上使用它

Matcher matcher = PATTERN.matcher("20150220|10");

根据RegexPlanet,这应该符合以下模式:

http://www.regexplanet.com/advanced/java/index.html

基本上我的模式应该匹配以下两个字符串:

"20150120|10"
AND
"20150120|10*20150121|15"

有任何想法吗?

调用matcher.group(1)时会出现此异常

java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at java.util.regex.Matcher.group(Matcher.java:445)
at com.myproject.handle(handler.java:128)
at com.myproject.handle(handlerTest.java:195)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

看过这篇文章后,显然我需要先调用.matches(),如果我想匹配完整的字符串

在致电小组之前为我工作:

http://ideone.com/cjV5rc

    /* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
Pattern PATTERN= Pattern.compile("(d+)(|)(d+)((*)(d+)(|)(d+))?");
Matcher matcher = PATTERN.matcher("20150220|10");
matcher.matches();
System.out.println(matcher.group(1));
    }
}

使用匹配器组方法时“找不到匹配”


我通常在if块中调用group方法,条件是matcher.matches()matcher.find()来防止发生异常,尽管这两种方法有一些区别。 有关更多信息,请参阅文档。

Matcher matcher = PATTERN.matcher("20150220|10");

if(matcher.matches())
{
    System.out.println(matcher.group(1) + ","
            + matcher.group(2) + ","
            + matcher.group(3) + ",
            + matcher.group(4) + ","
            + matcher.group(5) + ","
            + matcher.group(6) + ","
            + matcher.group(7) + ","
            + matcher.group(8));
}

matcher = PATTERN.matcher("20150120|10*20150121|15");
if(matcher.find())
{
        System.out.println(matcher.group(1) + ","
            + matcher.group(2) + ","
            + matcher.group(3) + ","
            + matcher.group(4) + ","
            + matcher.group(5) + ","
            + matcher.group(6) + ","
            + matcher.group(7) + ","
            + matcher.group(8)
        );
}

另外,可以在group方法中使用的索引应该等于模式中的括号对。

链接地址: http://www.djcxy.com/p/30577.html

上一篇: Java Regex Matcher IllegalStateException

下一篇: Matcher throws IllegalStateException after accessing group