views:

598

answers:

4

Hi, i am looking for a regex pattern that will return me the contents of the first set of brackets in a string.

Eg - text text text text (hello) text (hello2) (hello3) text

..will return "hello"

does anyone know what the pattern looks like for c#?

+7  A: 

The regexp pattern would look something like this:

\(([^)]*)\)

Pattern autopsy:

\( - a literal "("

( - start of subpattern:

[^)]* match 0 or more characters which are not ")" - note: we're defining a character group, so we don't have to escape the ) character here.

) - end of subpattern

\) - a literal ")"

The full pattern will match the brackets and the text inside them, the first subpattern will match only the text inside the brackets (see C# reference how to get them - I don't speak C# ;))

Piskvor
Can this be adapted to not return the brackets themselves? only the contents?
Grant
This looks correct to me. Match an actual '(' character, then start a group, then match zero or more non-')' characters, then close the group; then match an actual ')' character. The resulting group should get just what was inside the parens. This example is slightly confusing because the character to start a group just happens to be '(' and we are looking to match an actual '('. To turn off the "magic" of the '(' character and just match an actual '(' character, we put a backslash first, like "\(", as shown here.
steveha
This doesn't return the brackets themselves.
Yannick M.
thats weird because it does in my test app.. thats ok though.. no big deal
Grant
@Grant, this pattern looks correct to me. It should not return the parentheses enclosing the matched text, just the matched text. The pattern "[^)]" is a range expression, where "^)" means "match any character except the following characters:" where the only following character is ')'. Thus, this matches anything but ')'. Then a '*' means "match zero or more of this". Here is a good intro to regular expressions: http://www.codeproject.com/KB/string/re.aspx
steveha
If you are using C# use match.Groups[1].Value to get at the value between parentheses.
Peter van der Heijden
@Grant, are you absolutely sure that this pattern is the one in your test app? Here is a similar-looking pattern that would capture the parentheses. This is not the same as the pattern in the answer: `(\([^)]*\))`
steveha
thank you Peter... that did the trick.. and thanks everyone else too!!!
Grant
+1  A: 

Bare regex:

\((.*?)\)

In Python you can use it this way:

import re

rx = re.compile(r'\((.*?)\)')
s = 'text text text text (hello) text (hello2) (hello3) text'
rxx = rx.search(s)
if rxx:
    print(rxx.group(1))
Michał Niklas
Wouldn't python regex do greedy matching here? Returning `(hello) text (hello2) (hello3)` as the first and only match?
Yannick M.
Okay, the expression ".*?" is a non-greedy version of ".*" in Python. Does it also work in C#?
steveha
Hmm, the non-greedy trick probably does work in C#, see "Lazy and Greedy matching" in this page: http://www.dijksterhuis.org/regular-expressions-in-csharp-the-basics/
steveha
Ah, indeed, missed that :-)
Yannick M.
A: 

If the strings are relatively small, you could use a replace instead of a match:

string s = Regex.Replace("text text text text (hello) text (hello2) (hello3) text", @"^.*?\(([^)]*)\).*$", "$1");
Bart Kiers
A: 

This will return you only what is within the first set of brackets:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Match match = Regex.Match("foo bar (first) foo bar (second) foo", @"\((.*?)\)");

            if (match.Groups.Count > 1)
            {
                string value = match.Groups[1].Value;
                System.Console.WriteLine(value);
            }
        }
    }
}
Taylor Leese