tags:

views:

86

answers:

4

Hi there,

I am looking for a regex pattern to find all the content b/w curly brackets. For example, there is a string.

$string = {xxx yyy zzz}

I want to find a regex pattern so that it can extract the "xxx yyy zzz" out but no {}.

Thank you very much for your help.

+5  A: 

If the {} are not nested,

\{([^}]*)\}

If you're using PCRE and they're nested ref,

\{([^{}]++|(?0))*\}

Otherwise, make a simple parser.

KennyTM
To elaborate, you'll need to pull the first group match out using whatever language you're executing the regex in. For example, in javascript it would be: `text.match(/{([^}]*)}/)[1]`.
tloflin
I'm testing on PHP. Here is my code.$string ='start {first fine me} and {second find me}'; preg_match_all("/{([^{][^}]*)}/", $string, $matches); foreach($matches[0] as $value) { echo $value;echo "<br/>"; }What I want is this.first fine mesecond find meBut I still get this with the pattern.{first fine me}{second find me}
Leon
@Leon: `echo $value[1];`
KennyTM
A: 
/\{(.+)\}/

It will be returned in the first capture.

Tim Harper
this has possibly incorrect behavior if the string looks like '{xxx yyy zzz} {aaa bbb}', FWIW. I say "possibly" since the original question doesn't really say if this sort of input is possible or likely, or what the expected output would be in such a case.
Bryan Oakley
It could be anything inside {}.I tested the pattern but it still has the {} in the return result.
Leon
stackoverflow munched my backslashes. grr.
Tim Harper
A: 

Thank you for the responses. I'm using PHP for testing. And here is my test code.

$string ='start {first find me} and {second find me}'; preg_match_all("/{([^{][^}]*)}/", $string, $matches); foreach($matches[0] as $value) { echo $value;echo "
"; }

I will have
{first find me}
{second find me}
But I expect
first find me
second find me

Thanks.

Leon
A: 

building on what Kenny already had, you have to check for extra stuff at the beggining, and add the global modifier. I'm not sure about php, but I can do it in javascript

var str = "start {first fine me} and {second find me}";
reStr = new RegExp(/[^\{]*\{([^}]*)\}/g);
var result = str.replace(reStr, "$1")

result will be "first fine mesecond find me"

David