开发者

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

I have a string. How I can check if the string is a regular expression or contains regular expre开发者_高级运维ssion or it is a normal string?


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

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

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

The only cases where this will return false are strings that are not valid regular expressions, such as [unclosed character class or (unclosed group or +.


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).

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


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.

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.


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 valid regular 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.

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


If anyone just want to distinguish just plain text strings and regular-expressions:

static boolean hasSpecialRegexCharacters(String s){
    Pattern regexSpecialCharacters = Pattern
            .compile("[\\\\\\.\\[\\]\\{\\}\\(\\)\\<\\>\\*\\+\\-\\=\\!\\?
      \\^\\$\\|]");
     return regexSpecialCharacters.matcher(s).find();
}


/**
 * 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜