java java正则表达式模式未封闭的字符类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14390722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java regex pattern unclosed character class
提问by Joseph Vance
I need some help. Im getting:
我需要一些帮助。我越来越:
Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 24
^[a-zA-Z└-?0-9£μ /.'-\]*$
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.clazz(Pattern.java:2254)
at java.util.regex.Pattern.sequence(Pattern.java:1818)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
Here is my code:
这是我的代码:
String testString = value.toString();
Pattern pattern = Pattern.compile("^[a-zA-Z0-70-9\u01536 \u002F.'-\]*$");
Matcher m = pattern.matcher(testString);
I have to use the unicode value for some because I'm working with xhtml.
我必须为某些人使用 unicode 值,因为我正在使用 xhtml。
Any help would be great!
任何帮助都会很棒!
回答by Jeff
Assuming that you want to match \
and -
and not ]
:
假设你想匹配\
and-
和 not ]
:
Pattern pattern = Pattern.compile("^[a-zA-Z0-70-9\u01536 \u002F.'\\-]*$");
You need to double escape your backslashes, as \
is also an escape character in regex. Thus \\]
escapes the backslash for java but not for regex. You need to add another java-escaped \
in order to regex-escape your second java-escaped \
.
您需要对反斜杠进行双重转义\
,正则表达式中的转义字符也是如此。因此\\]
转义 java 而不是正则表达式的反斜杠。您需要添加另一个 java-escaped\
以便正则表达式转义您的第二个 java-escaped \
。
So \\\\
after java escaping becomes \\
which is then regex escaped to \
.
因此,\\\\
在 java 转义之后\\
,然后正则表达式转义为\
.
Moving -
to the end of the sequence means that it is used as a character, instead of a range operator as pointed out by Pshemo.
移动-
到序列的末尾意味着它被用作字符,而不是 Pshemo 指出的范围运算符。
回答by Pshemo
It is hard to say what are you trying to achieve, but I can see few strange things in your regex:
很难说你想达到什么目的,但我可以在你的正则表达式中看到一些奇怪的东西:
- you have opened class of characters but never closed it. Instead you used
\\]
which makes]
normal character.- If you want to include
]
in your characters class then you need additional]
at the end, like"^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'-\\]]*$"
- if you want to include
\
in your characters class then you need to use\\\\
version, because you need to escape its special meaning two times, in regex engine, and in Javas String
- If you want to include
- you used
-
with ('-\\]
) which in character class is used to specify range of characters likea-z
orA-Z
. To escape its special meaning you need to use\\-
- 您打开了字符类,但从未关闭它。相反,您使用
\\]
which 使]
正常字符。- 如果你想包含
]
在你的角色类中,那么你最后需要额外]
的,比如"^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'-\\]]*$"
- 如果你想包含
\
在你的字符类中,那么你需要使用\\\\
version,因为你需要在正则表达式引擎和 Javas String 中两次转义它的特殊含义
- 如果你想包含
- 您
-
与 ('-\\]
)一起使用,它在字符类中用于指定字符范围,例如a-z
或A-Z
。为了逃避它的特殊含义,你需要使用\\-