java正则表达式unclosed字符类 [英] java regex pattern unclosed character class
问题描述
我需要一些帮助。我得到:
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)
这是我的代码:
String testString = value.toString();
Pattern pattern = Pattern.compile("^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'-\\]*$");
Matcher m = pattern.matcher(testString);
我必须使用unicode值,因为我正在使用xhtml。
I have to use the unicode value for some because I'm working with xhtml.
任何帮助都会很棒!
推荐答案
假设您要匹配 \
和 -
而不是]
:
Assuming that you want to match \
and -
and not ]
:
Pattern pattern = Pattern.compile("^[a-zA-Z\300-\3770-9\u0153\346 \u002F.'\\\\-]*$");
你需要双倍逃避反斜杠,如 \
也是正则表达式中的转义字符。因此 \\]
转义java的反斜杠,但不转义正则表达式。你需要添加另一个java-escaped \
,以便regex-escape你的第二个java-escaped \
。
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转义成为 \\\之后\\
然后正则表达式转义为 \
。
So \\\\
after java escaping becomes \\
which is then regex escaped to \
.
移动 -
到序列的末尾意味着它被用作一个字符,而不是Pshemo指出的范围运算符。
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.
这篇关于java正则表达式unclosed字符类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!