Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I never used regex before. I was abel to see similar questions in forum but not exactly what im looking for

I have a string like following. need to get the values between curly braces

Ex: "{name}{[email protected]}"

And i Need to get the following splitted strings.

name and [email protected]

I tried the following and it gives me back the same string.

string s = "{name}{[email protected]}";
string pattern = "({})";
string[] result = Regex.Split(s, pattern);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

Use Matches of Regex rather than Split to accomplish this easily:

string input = "{name}{[email protected]}";
var regex = new Regex("{(.*?)}");
var matches = regex.Matches(input);
foreach (Match match in matches) //you can loop through your matches like this
{
  var valueWithoutBrackets = match.Groups[1].Value; // name, [email protected]
  var valueWithBrackets = match.Value; // {name}, {[email protected]}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...