C# Regex.Match curly brackets- contents only? (exclude braces)

前端 未结 7 919
难免孤独
难免孤独 2020-12-01 12:17

I\'ve been unable to find an answer on this: can I use the Regex.Matches method to return only the contents of items with curly braces?

If I us

相关标签:
7条回答
  • 2020-12-01 12:47

    Little bit modifying the answer of @Milosz Krajewski

    (?<=\{)[^}{]*(?=\})
    

    this will skip the middle single opening and closing Curly braces in the string.

    0 讨论(0)
  • 2020-12-01 12:54

    In C#, as in many other programming language, the regex engine supports capturing groups, that are submatches, parts of substrings that match a whole regex pattern, defined in a regex pattern with the help of parentheses (e.g. 1([0-9])3 will match 123 and save the value of 2 into a capture group 1 buffer). Captured texts are accessed via Match.Groups[n].Value where n is the index of the capture group inside the pattern.

    Capturing is much more effecient that lookarounds. Whenever there is no need for complex conditions, capturing groups are much better alternatives.

    See my regex speed test performed at regexhero.net:

    Now, how can we get the substring inside curly braces?

    • if there is no other curly braces inside, with a negated character class: {([^{}]*)
    • if there can be nested curly brackets: {((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))

    In both cases, we match an opening {, and then match (1) any character other than { or }, or (2) any characters up to the first paired }.

    Here is sample code:

    var matches = Regex.Matches("Test {Token1} {Token 2}", @"{([^{}]*)");
    var results = matches.Cast<Match>().Select(m => m.Groups[1].Value).Distinct().ToList();
    Console.WriteLine(String.Join(", ", results));
    matches = Regex.Matches("Test {Token1} {Token {2}}", @"{((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))");
    results = matches.Cast<Match>().Select(m => m.Groups[1].Value).Distinct().ToList();
    Console.WriteLine(String.Join(", ", results));
    

    Result: Token1, Token 2, Token1, Token {2}.

    Note that RegexOptions.IgnoreCase is redundant when you have no literal letters that can have different case in the pattern.

    0 讨论(0)
  • 2020-12-01 12:56

    Just move the braces outside the parentheses:

     {([^}]*)}
    
    0 讨论(0)
  • 2020-12-01 12:56

    It is regex for C# .net.

    @"{(.*?)}"
    

    it display a

    token1 token2

    0 讨论(0)
  • 2020-12-01 13:02

    Thanks Milosz Krajewski, Nothing to add but here is the function

    private List<String> GetTokens(String str)
    {
        Regex regex = new Regex(@"(?<=\{)[^}]*(?=\})", RegexOptions.IgnoreCase);
        MatchCollection matches = regex.Matches(str);
    
        // Results include braces (undesirable)
        return matches.Cast<Match>().Select(m => m.Value).Distinct().ToList();
    }
    
    0 讨论(0)
  • 2020-12-01 13:02

    If I understand what you want. Change the regex to {([^}]*)}. That will only capture the text between {}, not including them.

    0 讨论(0)
提交回复
热议问题