Groovy matcher.matches似乎不适用于有效的正则表达式 [英] Groovy matcher.matches does not seem to be working for valid regex
问题描述
之前我问过一个类似的问题,但我没有使用Groovy / Java 8正则表达式/模式/匹配器API的另一部分。
Groovy 2.4 :
class TestPatterns {
static void main(String [] args){
Matcher匹配器
String illNameChars = / [`\ /; \ [\]&<>?:\()| - ] + /
String input =Bupo`dupo
// String input =Bupo / dupo
// String input =Bupo; dupo
// String input =Bupo [dupo
// String input =Bupo ]
//字符串输入=Bupo& dupo
//字符串输入=Bupo< dupo
//字符串输入=Bupo> dupo
/ / String input =Bupo?dupo
// String input =Bupo:dupo
// String input =Bupo\\dupo
// String input = Bupo(dupo
// String input =Bupo)dupo
// String input =Bupo | dupo
// String inp ut =Bupo-dupo
if(input){
matcher = input =〜illNameChars
if(matcher.matches()){
println检测到非法字符!
} else {
println输入完全合法。
$ / code>
这是否打印出输入是完全合法的。? 输入
字符串显然在其中有一个反引号,这是在正则表达式中定义的。我是否使用 matcher.matches()
不正确?
最后,我希望以下字符被视为非法:
$ b
`/; []&<>?:()| -
blockquote>
但是上面的代码对于所有的
input
排列似乎都失败了,不管我有哪一个切换到(没有注释掉)。任何想法?解决方案当使用
Matcher.matches()
时,整个输入必须匹配正则表达式,因此您必须将正则表达式更改为/.* [`\ /; \ [\]&<>?:\ ()| - ]。* /
。
用
=〜$ c创建Matcher对象$ c>然后使用它的方法,你也可以简单地使用
检查正则表达式是否匹配输入字符串的任何子字符串。==〜
匹配运算符。 (if(input ==〜illNameChars)...
)。
另外你也可以做
if(matcher)...
,然后用=〜〜
创建它。
最后一个选择是保持正则表达式,但使用
if(matcher.find())...
来代替find() I asked a similar question earlier, but I'm not using a different part of the Groovy/Java 8 regex/pattern/matcher API.
Groovy 2.4 here:
class TestPatterns { static void main(String[] args) { Matcher matcher String illNameChars = /[`\/;\[\]&<>?:\()|-]+/ String input = "Bupo`dupo" // String input = "Bupo/dupo" // String input = "Bupo;dupo" // String input = "Bupo[dupo" // String input = "Bupo]dupo" // String input = "Bupo&dupo" // String input = "Bupo<dupo" // String input = "Bupo>dupo" // String input = "Bupo?dupo" // String input = "Bupo:dupo" // String input = "Bupo\\dupo" // String input = "Bupo(dupo" // String input = "Bupo)dupo" // String input = "Bupo|dupo" // String input = "Bupo-dupo" if(input) { matcher = input =~ illNameChars if(matcher.matches()) { println "Illegal character detected!" } else { println "The input is perfectly legal." } } } }
Why does this print out "The input is perfectly legal."? The
input
string clearly has a backtick in it and this is defined in the regex. Am I usingmatcher.matches()
incorrectly?Ultimately, I want the following characters to be considered "illegal":
`/;[]&<>?:()|-
But the code above seems to fail for all the
input
permutations, regardless of which one I have "toggled on" (not commented out). Any ideas?解决方案When using
Matcher.matches()
, the whole input must "match" the regex, so you would have to change your regex to be/.*[`\/;\[\]&<>?:\()|-].*/
.Alternatively to creating the Matcher object with
=~
and then using its method, you can also simply use the==~
matches operator. (if (input ==~ illNameChars) ...
).Alternatively you can also just do
if (matcher) ...
after you created it with=~
.The last alternative you have is to keep your regex as it is, but use
if (matcher.find()) ...
instead, asfind()
checks whether the regex matches any substring of the input string.这篇关于Groovy matcher.matches似乎不适用于有效的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文