流口水的正则表达式(特殊字符 /* )
Regular expression with drools (special characters /* )
我正在尝试制定规则来捕获无效字符。这些是 & < > /* ' " --
我已经设法 "catch" 除了 /*
之外的所有符号
我的代码是:
rule "Invalid Chars"
when
c : Class ( field matches ".*[&<>'\"].*|.*(--).*" )
then
// DO SOMETHING
end
我的正则表达式如下。但是当我尝试添加 /* 符号时。我得到这个正则表达式
.*[&<>'\"].*|.*(--).*|.*(\/\*).*
但是当我尝试 运行 时,我得到了这个错误:
line 1:28 no viable alternative at character '*'
line 1:33 mismatched character '' expecting '"'
java.lang.IllegalStateException: ### errors ###
[Error: illegal escape sequence: *]~
如何 "catch" 所有这些无效字符?
这是*转义但/未转义的最小测试:
public static void main(String[] args) {
if("Hallo /* Test ".matches(".*[&<>'\"].*|.*(--).*|.*(/\*).*")) {
System.out.println("ILLEGAL");
}
}
试试这个:
String pattern = "[&|<|>|'|\"|]|(--)|(\/\*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher("YOUR STRING TO FIND MATCHES");
while (m.find( )) {
System.out.println("Matched value: " + m.group(0) );
}
我正在尝试制定规则来捕获无效字符。这些是 & < > /* ' " --
我已经设法 "catch" 除了 /*
之外的所有符号我的代码是:
rule "Invalid Chars"
when
c : Class ( field matches ".*[&<>'\"].*|.*(--).*" )
then
// DO SOMETHING
end
我的正则表达式如下。但是当我尝试添加 /* 符号时。我得到这个正则表达式
.*[&<>'\"].*|.*(--).*|.*(\/\*).*
但是当我尝试 运行 时,我得到了这个错误:
line 1:28 no viable alternative at character '*' line 1:33 mismatched character '' expecting '"' java.lang.IllegalStateException: ### errors ### [Error: illegal escape sequence: *]~
如何 "catch" 所有这些无效字符?
这是*转义但/未转义的最小测试:
public static void main(String[] args) {
if("Hallo /* Test ".matches(".*[&<>'\"].*|.*(--).*|.*(/\*).*")) {
System.out.println("ILLEGAL");
}
}
试试这个:
String pattern = "[&|<|>|'|\"|]|(--)|(\/\*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher("YOUR STRING TO FIND MATCHES");
while (m.find( )) {
System.out.println("Matched value: " + m.group(0) );
}