简体   繁体   English

带有流口水的正则表达式(特殊字符/ *)

[英]Regular expression with drools (special characters /* )

I'm trying to make a rule to catch invalid characteres. 我正在尝试制定规则来捕获无效字符。 Those are & < > /* ' " -- 这些是&<> / *'“-

I already managed to "catch" all the symbols except the /* 我已经设法“捕获”除/ *以外的所有符号

My code is: 我的代码是:

rule "Invalid Chars"
when
    c : Class ( field matches ".*[&<>'\"].*|.*(--).*" )
then
    // DO SOMETHING     
end

My regex is below. 我的正则表达式如下。 But when I try to add the /* symbol. 但是当我尝试添加/ *符号时。 I get this regex 我得到这个正则表达式

.*[&<>'\"].*|.*(--).*|.*(\/\*).*

But when I try to run I get this error: 但是,当我尝试运行时,出现此错误:

line 1:28 no viable alternative at character '*' line 1:33 mismatched character '' expecting '"' java.lang.IllegalStateException: ### errors ### [Error: illegal escape sequence: *]~ 第1:28行在字符'*'处没有可行的替代方法第1:33行不匹配的字符”期望“”” java.lang.IllegalStateException:###错误### [错误:非法转义序列:*]〜

How to "catch" all these invalid characters? 如何“捕获”所有这些无效字符?

Here's minimal Test with * escaped but / not: 这是带有*的最小测试,但/不是:

public static void main(String[] args) {
    if("Hallo /* Test ".matches(".*[&<>'\"].*|.*(--).*|.*(/\\*).*")) {
        System.out.println("ILLEGAL");
    }
}

Try this one: 试试这个:

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) );
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM