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

时间:2021-07-31 12:38:46

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?

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

5 个解决方案

#1


16  

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.

但是,请注意,即使对于像Hello World这样的字符串,这也会导致true,而且我不是regex,因为从技术上讲,它们是有效的正则表达式。

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

唯一会返回false的情况是字符串不是有效的正则表达式,比如[unclosed character类或(unclosed group或+)。

#2


4  

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

#3


1  

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的regexp包(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.

显然,用户将键入一个无效的regexp,您将把它作为一个普通字符串来处理。

#4


1  

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.

它只是一个普通的字符串,regex引擎以特定的方式解释它。

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

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

#5


-3  

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

#1


16  

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.

但是,请注意,即使对于像Hello World这样的字符串,这也会导致true,而且我不是regex,因为从技术上讲,它们是有效的正则表达式。

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

唯一会返回false的情况是字符串不是有效的正则表达式,比如[unclosed character类或(unclosed group或+)。

#2


4  

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

#3


1  

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的regexp包(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.

显然,用户将键入一个无效的regexp,您将把它作为一个普通字符串来处理。

#4


1  

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.

它只是一个普通的字符串,regex引擎以特定的方式解释它。

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

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

#5


-3  

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