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?
A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.
In formal language theory, a regular expression (a.k.a. regex, regexp, or r.e.), is a string that represents a regular (type-3) language. Huh?? Okay, in many programming languages, a regular expression is a pattern that matches strings or pieces of strings.
You can't detect regular expressions with a regular expression, as regular expressions themselves are not a regular language. However, the easiest you probably could do is trying to compile a regex from your textbox contents and when it succeeds you know that it's a regex. If it fails, you know it's not.
Java - String matches() Method This method tells whether or not this string matches the given regular expression. An invocation of this method of the form str. matches(regex) yields exactly the same result as the expression Pattern. matches(regex, str).
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With