-1

I'm trying to instantiate a new Regex class with the regular expression

var reg = new Regex("[[" + token.Key + "]]");

But I'm getting the following error

System.ArgumentException: 'parsing "[[Impact]]" - [x-y] range in reverse order.'

How can I instantiate the new class with the double square brackets in the regular expression?

Train
  • 3,420
  • 2
  • 29
  • 59
  • Did you try `"\\[\\[" + token.Key + "\\]\\]"`? – vc 74 Jul 26 '18 at 06:28
  • First of all, interestingly I don't get such an exception. On the other hand, what you are trying to achieve? you want the regex to match [[ and ]] as literals? – ZorgoZ Jul 26 '18 at 06:29
  • Yes, it doesn't work. I'll try `\\[\\[` and see what happens – Train Jul 26 '18 at 06:29
  • @ZorgoZ Yes That's what I want. – Train Jul 26 '18 at 06:29
  • @ZorgoZ According to my duplicate down vote which isn't really a duplicate, but very informative none the less, it might be a different flavor of regex you're using. – Train Jul 26 '18 at 15:53
  • @OrthoHomeDefense I did not know that .net has different flavors of RegEx. I was using the latest one in LinqPad. – ZorgoZ Jul 26 '18 at 16:41

5 Answers5

4

You could use built-in functions to escape things:

var re = Regex.Escape("[[") + token.Key + Regex.Escape("]]");
var reg = new Regex(re);

But you may also want to escape the token.Key?

Or just use a string.Contains(...), if you don't want the special features of regex.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
3

You need to escape the characters "[" and "]" for the regular expression, and if you are using "\" to escape the brackets, then you need to escape those in your C# string as well.

That means it can be either:

"\\[\\[" + key + "]]"

or

@"\[\[" + key + "]]"

or

Regex.Escape("[[") + key + "]]"

You don't need to escape the latter "]" because Regex recognizes them as normal characters if you don't have an unescaped "[" before them. I tried it on Regexpal (a regex testing website) and other answers suggest it as well.

Manuel Hoffmann
  • 539
  • 1
  • 7
  • 23
2

This one works:

var reg = new Regex(@"\[\[" + token.Key + @"]]");

enter image description here

(Note: the second @ is not actually needed)

Update: This is even better:

var reg = new Regex($@"\[\[{token.Key}]]");
ZorgoZ
  • 2,974
  • 1
  • 12
  • 34
  • what is that extension called? – slow Jul 26 '18 at 06:36
  • 2
    This screenshot is a LinqPad snippet. `.Dump` is part of it, but this is something similar: https://github.com/victorjspinto/101-Linq-Samples/blob/master/LINQ%20-%20Quantifiers/C%23/Quantifiers/ObjectDumper.cs – ZorgoZ Jul 26 '18 at 06:39
  • @vc74 yes, I have already noted that, as you can see – ZorgoZ Jul 26 '18 at 06:45
1

Just escape every bracket with a bracket like is shown below:

var reg = new Regex("[[[["+ token.Key +"]]]]");
Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
1

Try

var reg = new Regex("[[][[]" + token.Key + "[]][]]");

it will match, for example [[Impact]] if token.Key equals "Impact".

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69