【发布时间】:2013-07-12 20:00:54
【问题描述】:
我正在用 java 编写一个正则表达式,但是当我运行程序时出现错误。
private final static Pattern QUOTE_VALUE = Pattern.compile("[_]?([a-zA-Z0-9_]+)=(\"[^]*\"),");
// Then later on down the road......
Macher m = QUOTE_VALUE.matcher(myString);
while (m.find()){
System.out.println("Found " + m.group(1) + " " + m.group(2));
}
我想让我的正则表达式匹配这些示例值。
_MyKey="ID IN [ "ABC" ]", // Note - it has a comma after the ]
_MyKey="ID IN [ ""XYZ"" ]", // Note - it has a comma after the ]
我使用在线正则表达式助手进行了尝试 - 我的正则表达式实际上工作正常。但是当我运行程序时,我得到了这个错误:
Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 28
[_]?([a-zA-Z0-9_]+)=("[^]*"),
另一个问题是,我如何格式化正则表达式,以便我也可以将它与这个字符串匹配?
MyKey="ID IN [ "ABC" ]", // without the _
_MyKey="ID IN [ "ABC" ]", // with the _
谢谢。
[编辑]
你能帮我解决这部分问题吗?
另一个问题是,我如何格式化正则表达式,以便我也可以将它与这个字符串匹配?
MyKey="ID IN [ "ABC" ]", // 没有 _ _MyKey="ID IN [ "ABC" ]", // 带有 _
【问题讨论】:
-
我猜你有一个未封闭的字符类
-
查看this page explaining character classes 上标题为“字符类中的元字符”的部分,他们以
[^]x]为例,可以很好地解释您的错误。