tags:

views:

118

answers:

4

How can I check if a String in an textbox is a plain String ore a RegEx?

I'm searching through a text file line by line.

Either by .Contains(Textbox.Text); or by Regex(Textbox.Text) Match(currentLine)

(I know, syntax isn't working like this, it's just for presentation)

Now my Program is supposed to autodetect if Textbox.Text is in form of a RegEx or if it is a normal String.

Any suggestions? Write my own little RexEx to detect if Textbox contains a RegEx?

Edit:

I failed to add thad my Strings can be very simple like Foo ore 0005 I'm trying the suggested solutions right away!

+5  A: 

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.

But this would classify ordinary strings like "foo" as a regular expression too. Depending on what you need to do, this may or may not be a problem. If it's a search string, then the results are identical for this case. In the case of "foo.bar" they would differ, though since it's a valid regex but matches different things than the string itself.

My advice, also stated in another comment, would be that you simply always enable regex search since there is exactly no difference if you split code paths here. Aside from a dubious performance benefit (which is unlikely to make any difference if there is much of a benefit at all).

Joey
+3  A: 

Many strings could be a regex, every regex could actually be a string.

Consider the string "thin." could either be a string ('.' is a dot) or a regex ('.' is any character).

I would just add a checkbox where the user indicates if he enters a regex, as usual in many applications.

Stefan Steinegger
A: 

Try and use it in a regex and see if an exception is thrown.

This approach only checks if it is a valid regex, not whether it was intended to be one.

Another approach could be to check if it is surrounded by slashes (ie. ‘/foo/‘) Surrounding regexes with slashes is common practice (although you must remove the slashes before feeding it into the regex library)

Charlie Somerville
+2  A: 

One possible solution depending on your definition of string and regex would be to check if the string contains any regex typical characters.

You could do something like this:

string s = "I'm not a Regex";

if (s == Regex.Escape(s))
{
   // no regex indeed
}
Frank Bollack
How about "Am I a RegEx?"
0xA3
As per documentation of Regex.Escape() it would be considered a regex.
Frank Bollack
The result is actually the same as if the string is always interpreted as regex. The only difference: if there is nothing regex specific, it performs faster.
Stefan Steinegger
Stefan: That's some sort of micro-optimization that's in at least 99 % of cases unnecessary. Also regular expressions that don't need any backtracking are not really slower than normal text search.
Joey
@Johannes: so this implementation is completely useless. It produces the same result as always treating the text as regular expression.
Stefan Steinegger
@stefan: Regarding the OPs intention of scaning a text file you are right. Performance is no criteria here. But even if it is of no use in this scenario, it still answers the initial question "How to Check if a String is a “string” or a RegEx?" ;-)
Frank Bollack
Yes, I hesitated being so harsh calling this useless, in the context of the question it is certainly and useful a correct answer. It just doesn't really make sense most of the time. :-)
Joey
Sorry if someone thinks that this was rude. I just mean that it doesn't make sense to have such an implementation if there is no difference if this condition is there or not.
Stefan Steinegger