Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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?

like image 610
Saurabh Kumar Avatar asked Jun 14 '11 09:06

Saurabh Kumar


People also ask

How do you identify a regular expression?

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.

Is a string a regex?

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.

How do you check if a string is a regular expression C#?

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.

How do you check whether a string matches a regex in Java?

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


4 Answers

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

like image 186
Joachim Sauer Avatar answered Oct 22 '22 21:10

Joachim Sauer


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;
    }
}
like image 29
hoipolloi Avatar answered Oct 22 '22 21:10

hoipolloi


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.

like image 1
JoséMi Avatar answered Oct 22 '22 22:10

JoséMi


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

like image 1
Sam Holder Avatar answered Oct 22 '22 21:10

Sam Holder