简体   繁体   English

java正则表达式来发现正则表达式

[英]java regular expression to discover regular expression

I would like to build regular expression which checks if entered string is regular expression so if it contains | 我想构建正则表达式,检查输入的字符串是否是正则表达式,如果它包含| , [ and then ] , ( and then ) for example: [然后](然后)例如:

A0[1-4].*Done or (L1 | L2).*Done'

I've build something simple like this: ".[\\\\[]..[\\\\]]" but it doesn't work at all even for simple examples. 我已经构建了这样简单的东西: ".[\\\\[]..[\\\\]]"但它根本不起作用,即使对于简单的例子也是如此。 I tried with Java matcher and just simple String.matches() 我尝试使用Java matcher和简单的String.matches()

You can use Java own parser to check if the input is regular expression or not: 您可以使用Java自己的解析器来检查输入是否是正则表达式:

public boolean isRegExp(String pattern) {
    try {
        Pattern.compile(pattern);
        return true;
    }
    catch(PatternSyntaxException ex) {
        return false;
    }
}

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

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