joshis Posted December 2, 2009 Share Posted December 2, 2009 I would like to get strings within the '{}' symbols. My expression is like this. $string = 'The quick brown {fox} jumped {over} the lazy dog.'; $patterns[0] = '/({)+(.)*(})+/'; preg_match_all($patterns[0],$string,$matches); foreach($matches[0] as $match) { echo( $match).'<br />'; } but it gives result as {fox} jumped {over} but I need fox over Can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/183713-get-strings-within-simple-regex/ Share on other sites More sharing options...
cags Posted December 2, 2009 Share Posted December 2, 2009 Will there be any nested values. What I mean by this is will the source ever take the form of... This is an example {of what {I mean} will it ever be like this? } I don't quite understand the layout of your pattern, for the job described I'd probably opt for something along the lines of... #{\K[^}]*# Quote Link to comment https://forums.phpfreaks.com/topic/183713-get-strings-within-simple-regex/#findComment-969647 Share on other sites More sharing options...
salathe Posted December 2, 2009 Share Posted December 2, 2009 Pending the answer to cags' post, it might also be fine to use something like /{(.+?)}/ (you'd then need to loop over $matches[1]). There are a tonne of varieties of regular expression which might be useful, better, faster, easier, etc. and as usual there's no "one right answer". If you do need to cater for nested values, then both of our replies will need some rethinking. Quote Link to comment https://forums.phpfreaks.com/topic/183713-get-strings-within-simple-regex/#findComment-969656 Share on other sites More sharing options...
nrg_alpha Posted December 2, 2009 Share Posted December 2, 2009 In the event of nested braces, one possible solution could be: Example: function stripBraces($arr){ foreach($arr as &$val){ $val = trim($val, '{}'); } return $arr; } $str = 'Some text {of what {I mean} will it ever be like this?} Some more {text}!'; preg_match_all('#{[^{}]*(??R)|.*?)+}#', $str, $matches); $matches = array_map('stripBraces', $matches); echo '<pre>'.print_r($matches, true); // within array element 0... // [0] => of what {I mean} will it ever be like this? // [1] => text Granted, simply using trim, this would leave inner nested braced words intact. A quick and dirty remedy to this would be to simply remove all instances of {} within the stripBraces function using strtr; function stripBraces($arr){ $trans = array('{' => '', '}' =>''); foreach($arr as &$val){ $val = strtr($val, $trans); } return $arr; } Quote Link to comment https://forums.phpfreaks.com/topic/183713-get-strings-within-simple-regex/#findComment-969894 Share on other sites More sharing options...
nrg_alpha Posted December 2, 2009 Share Posted December 2, 2009 I just realised through that my solution won't isolate inner nested braced words.. that would require more re working if isolation would be required. Quote Link to comment https://forums.phpfreaks.com/topic/183713-get-strings-within-simple-regex/#findComment-969908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.