RoadBump
RoadBump

Reputation: 751

.NET Regular expression how to

New to Regex. I want to validate to this format:

After these strings any character can go

Example:

abc{FILE:any text} def {FILE:mno{ENV:xyz}}

FILE: and ENV: are an example of specific strings required after a '{' char. I wrote this regex:

^
(
  [^\{\}]+
  |
  (?<Depth>\{)(FILE:|ENV:)
  |
  (<-Depth>\})
)*
(?(Depth)(?!))
$

but it doesn't match my desired format. What i miss?
Thanks a lot.
EDIT: Links that do the same, succesfully i hope:-) MSDN, Other site

Upvotes: 1

Views: 110

Answers (1)

Qtax
Qtax

Reputation: 33928

You forgot the question mark in the balancing group.

string pattern = @"(?x)
^
(?:
    [^{}]+
    |
    (?<Depth>{) (?:FILE|ENV):
    |
    (?<-Depth>})
)*
(?(Depth)(?!))
$
";

Should match strings like a {FILE: {ENV: foo } bar } baz

Upvotes: 3

Related Questions