9

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);
1

4 Answers 4

40

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]}
}
2
  • Interesting, i get matches with brackets. Why? //my matches: {name}, {[email protected]}
    – Georg
    Commented Jun 13, 2018 at 7:35
  • 2
    @Georg Changed the answer accordingly. Included a group to display the value either with or without brackets. The difference to the other solutions is that Regex looks for matches, whereas the other answers are directly splitting the string. Commented Jun 13, 2018 at 8:36
17

Is using regex a must? In this particular example I would write:

s.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries)
3
  • @FabianBigler I was under the impression that regex can easily achieve this, So i included Regex in the title :). I have changed the title now ;)
    – Kurubaran
    Commented Jun 29, 2013 at 12:59
  • Wouldn't the proposed solution also take as correct a string such as string s = "}name{[email protected]";?
    – Josep
    Commented Aug 21, 2014 at 15:17
  • 2
    I voted too fast… This solution does not provide an answer. Consider input string : {name}blabla{[email protected]}, you’ll have also “blabla” in the array…
    – Jurion
    Commented Jan 8, 2015 at 3:37
0

here you go

string s = "{name}{[email protected]}";
s = s.Substring(1, s.Length - 2);// remove first and last characters
string pattern = "}{";// split pattern "}{"
string[] result = Regex.Split(s, pattern);

or

string s = "{name}{[email protected]}";
s = s.TrimStart('{');
s = s.TrimEnd('}');
string pattern = "}{";
string[] result = Regex.Split(s, pattern);
3
  • It works, but is there any way to get this result only using Regex pattern without using Substring ?
    – Kurubaran
    Commented Jun 29, 2013 at 11:05
  • im wondering if there is any was to to get the rsult only with regex pattern without doing any other string manipulation ?
    – Kurubaran
    Commented Jun 29, 2013 at 11:14
  • @Coder yes, take a look at my answer. ;) Commented Jun 29, 2013 at 11:34
0

If you want to avoid regex and also account for non-formatted text (stuff not inside the curly braces), Split on '{' and then split each of those on '}':

string s = "Hi {firstName}, my name is {firstName}, {myLastName}";

IEnumerable<string> tokens = s
    .Split('{')
    .Where(t => t.Contains('}'))
    .Select(t => t.Split('}').First());

// This will yield "firstName", "firstName", "myLastName".

IEnumerable<string> distinctTokens = tokens.Distinct(); // To weed out duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.