java 如何检查字符串是否为正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6341367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:29:03  来源:igfitidea点击:

How to check if the string is a regular expression or not

javaregex

提问by Saurabh Kumar

I have a string. How I can check if the string is a regular expression or contains regular expression or it is a normal string?

我有一个字符串。如何检查字符串是正则表达式或包含正则表达式还是普通字符串?

回答by Joachim Sauer

The only reliable check you could do is if the Stringis a syntactically correct regular expression:

您可以做的唯一可靠检查是 是否String是语法正确的正则表达式:

boolean isRegex;
try {
  Pattern.compile(input);
  isRegex = true;
} catch (PatternSyntaxException e) {
  isRegex = false;
}

Note, however, that this will result in trueeven for strings like Hello Worldand I'm not a regex, because technically they are valid regular expressions.

但是请注意,true即使对于Hello World和这样的字符串,这也会导致I'm not a regex,因为从技术上讲,它们是有效的正则表达式。

The only cases where this will return falseare strings that are not valid regular expressions, such as [unclosed character classor (unclosed groupor +.

这将返回的唯一情况false是不是有效正则表达式的字符串,例如[unclosed character classor(unclosed group+

回答by hoipolloi

This is ugly but will detect simple regular expressions (with the caveat they must be designed for Java i.e. have the relevant back-slash character escaping).

这是丑陋的,但会检测简单的正则表达式(注意它们必须是为 Java 设计的,即具有相关的反斜杠字符转义)。

public boolean isRegex(final String str) {
    try {
        java.util.regex.Pattern.compile(str);
        return true;
    } catch (java.util.regex.PatternSyntaxException e) {
        return false;
    }
}

回答by JoséMi

Maybe you'd try to compile that regular expression using regexp package from Apache ( http://jakarta.apache.org/regexp/) and, if you get an exception then that's not a valid regexp so you'd say it's a normal string.

也许您会尝试使用来自 Apache ( http://jakarta.apache.org/regexp/) 的regexp 包编译该正则表达式,如果出现异常,则这不是有效的正则表达式,因此您会说这是正常的细绳。

boolean validRE = true;
try {
    RE re = new RE(stringToCheck);
} catch (RESyntaxException e) {
    validRE = false;
}

Obviously, the user would have typed an invalid regexp and you'd be handling it as a normal string.

显然,用户输入了无效的正则表达式,而您将其作为普通字符串进行处理。

回答by Sam Holder

there is no difference between a 'normal' sting and a regular expression. A regular expression is just a normal string which is used as a pattern to match occurrences of the pattern in another string.

“正常”刺痛和正则表达式之间没有区别。正则表达式只是一个普通字符串,用作模式来匹配另一个字符串中出现的模式。

As others have pointed out, it is possible that the string might not be a validregular expression, but I think that is the only check you can do. If it is valid then there is no way to know if it is a regular expression or just a normal string because it will be a regular expression

正如其他人指出的那样,该字符串可能不是有效的正则表达式,但我认为这是您可以做的唯一检查。如果它有效,则无法知道它是正则表达式还是普通字符串,因为它将是正则表达式

It is just a normal string which is interpreted in a specific way by the regex engine.

它只是一个普通字符串,由正则表达式引擎以特定方式解释。

for example "blah" is a regular expression which will only match the string "blah" where ever it occurs in another string.

例如“blah”是一个正则表达式,它只会匹配字符串“blah”出现在另一个字符串中的地方。

When looked at this way, you can see that a regular expression does not need to contain any of the 'special characters' that do more advanced pattern matching, and it will only match the string in the pattern

以这种方式查看时,您可以看到正则表达式不需要包含任何进行更高级模式匹配的“特殊字符”,它只会匹配模式中的字符串

回答by Anonymous

/**
 * If input string is a regex, matches will always return a false.
 */ 
public boolean isRegex(final String str) {   
    return str != null ? !str.matches(str) : false;
}