0

any way would be fine. Perl, python, ruby...

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
lkahtz
  • 4,706
  • 8
  • 46
  • 72

4 Answers4

3

You can match this regex

\(.*?\)

Edit:

The above regex will also include the brackets as a part of matched string. To avoid getting the brackets as a part of match (i.e. only match string inside the starting and ending bracket, excluding brackets) you may use below regex.

(?<=\().*?(?=\))
Gopi
  • 10,073
  • 4
  • 31
  • 45
  • but please remember the match will include the brackets as well. – Gopi Jul 26 '10 at 05:56
  • It would only match "(this)" not "(this) is (sparta)" because the ? means that it matches as little as possible – chustar Jul 26 '10 at 05:58
  • @NullUserException no this wont. The .*? will make it lazy instead of greedy and first right bracket will be matched. – Gopi Jul 26 '10 at 05:58
  • 1
    @NullUserException: no, + and * are both greedy by default and non-greedy with ? after them. – ysth Jul 26 '10 at 07:30
  • The op want to capture the text, your regex don't – Toto Jul 26 '10 at 08:11
2

In perl, you can use this one,

Have a look

my $test = "Hello (Stack Overflow)";
   $test =~ /\(([^)]+)\)/;
my $matched_string = $1; 
print "$matched_string\n";  

OUTPUT:

Stack Overflow
Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
1

Do you only want to match outer braces?

For example:

In Python:

s = "(here is some text for you (and me))"

import re
print ''.join(re.split(r"^\(|\)$", s))
# Returns "here is some text for you (and me)"

Otherwise:

s = "(here is some text for you (and me))"

import re
print [text for text in re.split(r"[()]", s) if text]
# Returns "['here is some text for you ', 'and me']"
Nick Presta
  • 28,134
  • 6
  • 57
  • 76
0

On capturing groups

A capturing group, usually denoted with surrounding round brackets, can capture what a pattern matches. These matches can then be queried after a successful match of the overall pattern.

Here's a simple pattern that contains 2 capturing groups:

(\d+) (cats|dogs)
\___/ \_________/
  1        2

Given i have 16 cats, 20 dogs, and 13 turtles, there are 2 matches (as seen on rubular.com):

  • 16 cats is a match: group 1 captures 16, group 2 captures cats
  • 20 dogs is a match: group 1 captures 20, group 2 captures dogs

You can nest capturing groups, and there are rules specifying how they're numbered. Some flavors also allow you to explicitly name them.

References


On repeated captures

Now consider this slight modification on the pattern:

(\d)+ (cats|dogs)
\__/  \_________/
 1         2

Now group 1 matches \d, i.e. a single digit. In most flavor, a group that matches repeatedly (thanks to the + in this case) only gets to keep the last match. Thus, in most flavors, only the last digit that was matched is captured by group 1 (as seen on rubular.com):

  • 16 cats is a match: group 1 captures 6, group 2 captures cats
  • 20 dogs is a match: group 1 captures 0, group 2 captures dogs

References

Related questions specific to .NET

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623