0

I'm trying to parse a text looking for data inside this pattern:

{{([^]+)}}

i.e. any sequence of characters between {{ and }} . But, when I try to build a Regex object:

Regex _regex = new Regex("{{([^]+)}}", RegexOptions.Compiled);

I got this error:

analysis of "{{([^]+)}}" - Set of [] not terminated....

whatever it means... Someone has an hint?

weirdgyn
  • 776
  • 12
  • 42

2 Answers2

1

The purpose of [^...] is to negate character classes present in the specified list. After the ^ symbol, in order to define a correct regular expression, you should include a set of characters to exclude like, for example [^a]+ (this matches one or more characters that don't include the literal a).

The regex you are attempting to define is probably:

{{\s*([\w]+)\s*}}

Visit this link for trying a working demo.

Tommaso Belluzzo
  • 22,356
  • 7
  • 68
  • 95
1

This is because [^] is not a valid regex, because you need to specify at least one symbol that you wish to exclude.

In order to capture the string up to the closing }} change the expression to this:

{{((?:[^}]|}[^}])*)}}

Demo.

Sergey Kalinichenko
  • 697,062
  • 78
  • 1,055
  • 1,465