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
Little bit modifying the answer of @Milosz Krajewski
(?<=\{)[^}{]*(?=\})
this will skip the middle single opening and closing Curly braces in the string.
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?
{([^{}]*)
{((?>[^{}]+|{(?<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.
Just move the braces outside the parentheses:
{([^}]*)}
It is regex for C# .net.
@"{(.*?)}"
it display a
token1 token2
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();
}
If I understand what you want. Change the regex to {([^}]*)}
. That will only capture the text between {}, not including them.