0

If I create a RegEx from an empty string, how do I make it match nothing?

I was looking for an option like RegexOptions.DoNotMatchWithEmpty or something like that.

To clarify:

Let's assume I have no control over the string that is used to create the RegEx. The string can be empty and I have to create the RegEx object. I probably could set the RegEx to null and add a lot of null checking upstream but I'd rather not do that. Best solution would have been a RegexOptions flag that tells to match nothing when RegEx is constructed with an empty string. But that isn't available.

mikoro
  • 259
  • 1
  • 3
  • 12
  • 5
    Test the string for emptiness first? – lc. Oct 16 '12 at 12:41
  • 2
    What do you mean and why would you want to do it? What have you tried already? – Dmitry Ledentsov Oct 16 '12 at 12:42
  • I added some clarifications to the question. – mikoro Oct 16 '12 at 14:01
  • I fail to see the rationale behind this check. The regex `""` correctly matches an empty string. Depending on how you use it, it can also match lots of places in non-empty strings (once between each two characters and at the start and end of the string). I can think of lots of other regexes that behave the same, for example `"()"` - just checking for an empty string won't help you. Even `".*?"` will match the empty string, if it can. – Tim Pietzcker Oct 16 '12 at 14:19
  • If user has given `".*?"` that is OK because it is intentional. Empty regex string means that user has most likely forgot to set it and doesn't have the intention to match everything. And in this particular program there are list of regexes tested. A regex created from an empty string at the top of the list will match and stop the search. This is what I would like to prevent. – mikoro Oct 16 '12 at 14:33

2 Answers2

3

How about just doing this

if (string.IsNullOrEmpty(regex))
{
    return false;
}
// standard regex lookup goes here

Update: Alternatively just return a regular expression that matches nothing

if (string.IsNullOrEmpty(regex))
{
    return new Regex("a^");
}
return new Regex(regex);

It works because the ^ character matches the start of the string being tested (unless the Multiline regex option is supplied, which it isn't), however this clearly can't match as we have already matched another character, a.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
  • The RegEx object has to be created whether the input string is empty or not. See my clarification. – mikoro Oct 16 '12 at 13:57
  • @mikoro Then return a regualr expression that doesn't match anything – Justin Oct 16 '12 at 14:05
  • I was just thinking about that. Is there a regex that doesn't match _anything_? Of course I could just generate some long random string. – mikoro Oct 16 '12 at 14:38
1

Try the following as a default:

Regex.IsMatch(inputString, @"^(?!\s*$).+")

This should work for any string that is not empty or whitespace. If you just want to check if the RegEx is actually an empty string just use String.IsNullOrWhitespace.

Andrew
  • 815
  • 8
  • 17
  • Sorry, this doesn't answer my question. The input string in my case would be the second argument to IsMatch function. – mikoro Oct 16 '12 at 14:00
  • I hardcoded the regex for readability. You don't have to do the same. – Andrew Oct 16 '12 at 15:01