简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM