Mark Feldman
Mark Feldman

Reputation: 16119

Grouping reluctant regular expressions with LINQ

I'm using the following LINQ command to extract a list of bracket-delimited parameters from a string using a reluctant regular expression:

var result = Regex.Matches("foo[a][b][cccc]bar", @"(\[.+?])")
    .Cast<Match>()
    .Select(match => match.ToString())
    .ToArray();

This returns the following string array as expected:

- result    {string[3]} string[]
    [0] "[a]"   string
    [1] "[b]"   string
    [2] "[cccc]"    string

Is there a way to modify the regular expression itself so that the brackets aren't included in the output? I tried placing the .+ part of the expression inside a named group but it broke the matching. Obviously I could run each result through another regular expression to remove the brackets but I'd like to find out if there's a cleaner/better way to do this.

Upvotes: 1

Views: 206

Answers (1)

Szymon
Szymon

Reputation: 43023

Yes, you can use look-behind and look-ahead assertion:

(?<=\[)(.+?)(?=])

The code is then:

var result = Regex.Matches("foo[a][b][cccc]bar", @"(?<=\[).+?(?=])")
    .Cast<Match>()
    .Select(m => m.ToString())
    .ToArray();

Please also note that you don't need grouping brackets () in your regex as you're not trying to capture any groups.

Upvotes: 2

Related Questions