PatternSyntaxException: 在 Java 中使用正则表达式时的非法重复

我知道的正则表达式不多,但我需要匹配一个简单的模式,

Pattern.matches("{\"user_id\" : [0-9]*}", inputLine)

when inputLine is

{"user_id" : 34}

然而,我得到了这个例外:

java.util.regex.PatternSyntaxException: Illegal repetition
{"user_id" : 24}
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.closure(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.matches(Unknown Source)
at org.whispercomm.manes.server.http.IntegrationTest.createUser(IntegrationTest.java:173)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我做错了什么?

114504 次浏览

There should be plus operator:

user_id : [0-9]+

只有当字符串必须包含它时,才使用双撇号。

When the string including curly brackets use:

\{user_id : [0-9]+\}

{}在 Java 的 regex 方言(以及大多数其他方言)中是特殊的: 它们是重复量词 {n,m}的开始和结束标记,其中 nm是整数。因此出现了错误消息: “非法重复”。

你应该摆脱他们: "\\{\"user_id\" : [0-9]*\\}"

And since you seem to be trying to parse JSON, I suggest you have a look at 杰克逊.

例如,@IntelliJ IDE 检测到不一致性。根据输入的不同,您可以转义字符或使用异常提供的数据处理问题。

enter image description here

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;


public class StackOverflow {
private static String REGEX = "{\"user_id\" : [0-9]*}";


public static void main(String[] args) {
try {
Pattern pattern = Pattern.compile(REGEX);


// get a matcher object
String input = "Hello stackoverflow";
Matcher matcher = pattern.matcher(input);
input = matcher.replaceAll("something");
} catch (PatternSyntaxException e){
System.out.println("PatternSyntaxException: ");
System.out.println("Description: "+ e.getDescription());
System.out.println("Index: "+ e.getIndex());
System.out.println("Message: "+ e.getMessage());
System.out.println("Pattern: "+ e.getPattern());
}
}
}

输出

PatternSyntaxException:
Description: Illegal repetition
Index: 1
Message: Illegal repetition near index 1
{"user_id" : [0-9]*}
^
Pattern: {"user_id" : [0-9]*}

可能的解决办法


catch... ex
StringBuilder sb = new StringBuilder(text);
sb.insert(ex.getIndex() + 1, "\\");
matcher.replaceAll(sb.toString())