-1

I have a string like this:

{abc}@{defgh}mner{123}

How can I get all strings between { and } as an array or list?

Like this:

{ {abc},{defgh},{123} }

Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
radin
  • 251
  • 3
  • 16
  • 1
    Have you tried something already? There's a couple of ways to do this – shree.pat18 Jan 08 '19 at 06:41
  • Using regular expressions with capturing groups – Caius Jard Jan 08 '19 at 06:43
  • yes, i write a loop for get strings between { and }, but it is not best way. @shree.pat18 – radin Jan 08 '19 at 06:44
  • Is this possible `{abc}}}@{defgh}mner{123}`? Or this `asd{asd`? – FCin Jan 08 '19 at 06:48
  • @radin how do you know loop is not the best way? Post your code so we can tell you. Also tell us what is “best” for you – Caius Jard Jan 08 '19 at 06:57
  • 1
    Possible duplicate of [Get values between curly braces c#](https://stackoverflow.com/questions/17379482/get-values-between-curly-braces-c-sharp) and [many others](https://www.google.com/search?q=site%3Astackoverflow.com+c%23+get+text+between+brackets). – Lance U. Matthews Jan 08 '19 at 06:57
  • @FCin Yes, is possible. – radin Jan 08 '19 at 08:56
  • @CaiusJard i did this [link](https://stackoverflow.com/questions/13780654/extract-all-strings-between-two-strings). but my string is different. `{asd{asd}dd}34{wqw}` – radin Jan 08 '19 at 09:06
  • Yeah, I think that edit you made will invalidate a lot of answers here. We also can't post any more answers because this question is closed as a duplicate. Ask a new question, and put the edited version; {a{b}c}d{e} is very different requirement to {a}b{c}d{e} and probably requires a loop that counts the braces and only emits a text when the count becomes 0 – Caius Jard Jan 08 '19 at 09:07
  • @CaiusJard Ok, Thank you. – radin Jan 08 '19 at 09:11
  • @BACON I Edit this question, please check it. – radin Jan 08 '19 at 09:46
  • 2
    @radin this question has been **closed** as a duplicate, which your original question was. The new addition changes the scope of the question and as such it's better if you simply open a new one (in general). In this particular case, this question here answers your exact need: https://stackoverflow.com/questions/2778532/regular-expression-to-match-nested-braces – Sascha Wolf Jan 08 '19 at 10:23

7 Answers7

2

using Regex is a good choice

    string str = "{abc}@{defgh}mner{123}";
    foreach (Match match in Regex.Matches(str,"{[^}]+}"))
    {
        Console.WriteLine(match.Value);
    }
ojlovecd
  • 4,812
  • 1
  • 20
  • 22
1

Using sites like RegExr, you can easily try your regex before using it in code:

string str = "{abc}@{defgh}mner{123}";
foreach (Match match in Regex.Matches(str,"(\{.+?\})"))
{
    Console.WriteLine(match.Value);
}

https://regexr.com/460a3

Arcturus
  • 26,677
  • 10
  • 92
  • 107
0
var input = "{abc}@{defgh}mner{123}";
var pattern = @"\{(.+?)\}";

var matches = Regex.Matches(input, pattern);
IList<string> output = new List<string>();
foreach (Match match in matches)
    output.Add(match.Groups[0].Value);

Fiddle Result


Simple Version

var input = "{abc}@{defgh}mner{123}";
var pattern = @"\{(.+?)\}";

var matches = Regex.Matches(input, pattern);
IList<string> output = matches.Cast<Match>().Select(x => x.Groups[0].Value).ToList();           
output.Dump();  

Fiddle Result

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

You can use Regex

var str = "{abc}@{defgh}mner{123}";
var regex = new Regex(@"({\w+})",RegexOptions.Compiled);
var result = regex.Matches(str).Cast<Match>().Select(x=>x.Value);

result is IEnumerable<string> as required in OP

Output (value of Result)

{abc} 
{defgh} 
{123} 
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

Using Regex and turning them into a list using LINQ

var l = new Regex(@"\{(\w+)\}")
        .Matches("{abc}@{defgh}mner{123}l")
        .Cast<Match>()
        .Select(m => m.Groups[0].Value)
        .ToList();

How it works:

The regex {(\w+)} means:

  • {: Find a {
  • (: start capturing data group
  • \w+: match one or more word characters (a to z, 0 to 9)
  • ): end of capture
  • ): find a }

This will find all the text between the curly brackets

The regex will give a MatchCollection

We have to use Cast to turn it into something linq can query

We .Select items from the collection, m is an individual item, m.Groups[0].Value is the text captured by the group, between the curly brackets

.ToList returns all the texts in one list

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You can try following code, it doesn't uses regular expressions, so you don't need to know it!

static void Main(string[] args)
{
  string s = "{abc}@{defgh}mner{123}";
  int i1, i2 = 0;
  while ((i1 = s.IndexOf('{', i2)) >= 0)
  {
    i2 = s.IndexOf('}', i1);
    // Here you can add Substring result to some list or assign it to a variable...
    Console.WriteLine(s.Substring(i1 + 1, i2 - i1 - 1));
  }
}
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
0
using System;
using System.Text.RegularExpressions;

public class Example
{
  public static void Main()
  {
   string pattern = @"{([A-Za-z0-9\-]+)}" ; 
   string input = "{abc}@{defgh}mner{123}";
   MatchCollection matches = Regex.Matches(input, pattern);

   foreach (Match match in matches)
   {
     Console.WriteLine(match.Groups[1].Value);
   }
   Console.WriteLine();
  }
}

Output :

abc

defgh

123

You can check the Online Execution of the code : http://tpcg.io/uuIxo1

BlackMirror
  • 17
  • 1
  • 9