0

I need to build regex dynamically, so I pass to my method a string of valid characters. Then I use that string to build regex in my method

string valid = "^m><";  // Note 1st char is ^ (special char)

string input = ...; //some string I want to check

Check(valid);


public void Check(string valid)
{
    Regex reg = new Regex("[^" + valid + "]");
    if (reg.Match(input).ToString().Length > 0)
    {
        throw new Exception(...);
    }
}

I want above Match to throw exception if it finds any other character than characters provided by valid string above. But in my case, even if I dont have any other character tahn these 3, Check method still throws new exception.

What is wrong with this regex?

pixel
  • 9,653
  • 16
  • 82
  • 149
  • 1
    `reg.Match(reading).ToString().Length > 0`? `reg.IsMatch(reading)` – Ry- Apr 15 '18 at 07:02
  • 1
    Is that *actually* the only thing you need to do with the regular expression: check that it doesn't match any of a set of specified characters? If so, there are simpler ways to do that (I'd use `string.IndexOfAny`). If you really need a more complicated regular expression than you've shown, that's a different matter. – Jon Skeet Apr 15 '18 at 07:03
  • Your current pattern is `[^^><]`. Is this what you intend? – Tim Biegeleisen Apr 15 '18 at 07:03
  • @TimBiegeleisen That is correct. I want to search for anyting that is not (hence 1st ^) part of these 3 characters "^><". I tried escaping 2nd ^ with "^\^><" but that did not resolve anything – pixel Apr 15 '18 at 07:05
  • I was wrong, updated question. I had a character 'm' in there that was supposed to be checked for too and I wasnt doing that. Appologies – pixel Apr 15 '18 at 07:55
  • Correction to previous comment: IndexOfAny won't help as we want to check for characters *not* in `valid`. But `input.All(x => valid.Contains(x))` would work fine, and avoid some characters having regex-related meanings. – Jon Skeet Apr 15 '18 at 08:01

1 Answers1

0

this resolved it, thanks to everyone for help

Regex reg = new Regex("[^" + valid + "]", RegexOptions.IgnoreCase);
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
pixel
  • 9,653
  • 16
  • 82
  • 149
  • I'd still say that's a bad idea, personally. Suppose `valid` is the string "a-z". By your description, that should mean that the valid characters are 'a', '-' and 'z' - but that's not what your regex will mean. As I asked in comments on the question, do you really need regex at all? – Jon Skeet Apr 15 '18 at 07:59
  • How would you use IndexOfAny to check that only "^m><" is included in string and nothing else? – pixel Apr 15 '18 at 08:00
  • Have just added another comment - `IndexOfAny` wouldn't work (I wrote the comment too quickly), but there are plenty of other simple alternatives which don't involve regular expressions. Unless you actively want some characters to have "special" meanings, I would strongly recommend against using regular expressions. I'm happy to add an answer along those lines if it would help. – Jon Skeet Apr 15 '18 at 08:02
  • You need to escape special `]`, ``\`` and `-` chars to make the regex engine treat them as literal inside the character class regardless of their positions.It should be `Regex reg = new Regex($"[^{Regex.Escape(valid)}]", RegexOptions.IgnoreCase);` – Wiktor Stribiżew Apr 15 '18 at 08:31