-1

I need to be able to grab specific elements out of a string that start and end with curly brackets. If I had a string:

"asjfaieprnv{1}oiuwehern{0}oaiwefn"

How could I grab just the 1 followed by the 0.

Maroun
  • 94,125
  • 30
  • 188
  • 241
Josh Starrett
  • 149
  • 1
  • 10
  • String has methods that can help you do that. If that's not enough take a look at Regex. – Brian Rasmussen Feb 26 '13 at 21:34
  • @BrianRasmussen that string is not regular, RegEx won't work and neither will conventional parsing. – evanmcdonnal Feb 26 '13 at 21:36
  • 3
    @evanmcdonnal Sorry, I don't follow. Perhaps I didn't understand what he's trying to do. My understanding was that he wanted to grab the numbers in the curlies. That's certainly possible with RegEx. – Brian Rasmussen Feb 26 '13 at 21:38
  • http://stackoverflow.com/questions/1717611/regex-c-sharp-find-a-string-between-2-known-values – Forte L. Feb 26 '13 at 21:38
  • @BrianRasmussen languages with balanced parens (or other braces that enclose data) are not regular and cannot be parsed with regular expressions (at least not in the general case). RegEx could parse that exact string, but is not a general solution. – evanmcdonnal Feb 26 '13 at 21:49
  • @evanmcdonnal I'm perfectly aware of that, but with the information given in the question there may or may not be nested curlies in the input (you seem to assume that there will be, but we don't really know at this point). I stated the assumptions I made. If the assumptions are correct Regex can be used in this case. – Brian Rasmussen Feb 26 '13 at 22:18

3 Answers3

3

Regex is very useful for this.

What you want to match is:

\{   # a curly bracket
     # - we need to escape this with \ as it is a special character in regex
[^}] # then anything that is not a curly bracket
     # - this is a 'negated character class'
+    #  (at least one time)
\}   # then a closing curly bracket
     # - this also needs to be escaped as it is special

We can collapse this to one line:

\{[^}]+\}

Next, you can capture and extract the inner contents by surrounding the part you want to extract with parentheses to form a group:

\{([^}]+)\}

In C# you'd do:

var matches = Regex.Matches(input, @"\{([^}]+)\}");
foreach (Match match in matches)
{
    var groupContents = match.Groups[1].Value;
}

Group 0 is the whole match (in this case including the { and }), group 1 the first parenthesized part, and so on.

A full example:

var input = "asjfaieprnv{1}oiuwehern{0}oaiwef";
var matches = Regex.Matches(input, @"\{([^}]+)\}");
foreach (Match match in matches)
{
    var groupContents = match.Groups[1].Value;
    Console.WriteLine(groupContents);
}

Outputs:

1
0
porges
  • 30,133
  • 4
  • 83
  • 114
  • That string is not regular, regex is completely useless for this. – evanmcdonnal Feb 26 '13 at 21:37
  • 4
    @evanmcdonnal: The question reads to me that they only want to extract the simple contents of the `{...}`, which is regular. There is no matching of nested parentheses. (Even if there was, the .NET regex engine is capable of matching nested parentheses, it is not restricted to pure regex.) – porges Feb 26 '13 at 21:40
  • You're assuming that he will never have `{{0}}` in a string which is quite a lot to assume. If you can assume there will never be nested braces then the solution is trivial and RegEx is still a poor solution. – evanmcdonnal Feb 26 '13 at 21:44
1

Use the Indexof method:

int openBracePos = yourstring.Indexof ("{");
int closeBracePos = yourstring.Indexof ("}");
string stringIWant = yourstring.Substring(openBracePos, yourstring.Len() - closeBracePos + 1);

That will get your first occurrence. You need to slice your string so that the first occurrence is no longer there, then repeat the above procedure to find your 2nd occurrence:

yourstring = yourstring.Substring(closeBracePos + 1);

Note: You MAY need to escape the curly braces: "{" - not sure about this; have never dealt with them in C#

Melanie
  • 3,021
  • 6
  • 38
  • 56
  • Thank you sir this works well however I did have to change one thing to get just the contents of the braces I had to change your third line to: string stringIWant = yourstring.Substring(openBracePos, closeBracePos + 1 - openBracePos); – Josh Starrett Feb 27 '13 at 17:08
0

This looks like a job for regular expressions

using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "asjfaieprnv{1}oiuwe{}hern{0}oaiwefn";
            Regex regex = new Regex(@"\{(.*?)\}");

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