开发者

What is the regex patten for this?

开发者 https://www.devze.com 2022-12-25 07:24 出处:网络
I am looking for a regex pattern to find all the content b/w curly brackets. For example, there is a string.

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.


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 "<br/>";
}

I will have:

{first find me} 
{second find me}

But I expect:

first find me
second find me

Thanks.


If the {} are not nested,

\{([^}]*)\}

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

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

Otherwise, make a simple parser.


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"


/\{(.+)\}/

It will be returned in the first capture.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号