jamesxg1 Posted June 3, 2010 Share Posted June 3, 2010 Hiya peeps! I was wondering what regex I would use in preg_match_all to seek things in curly brackets ( { } ). Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/ Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 '/{([^}]+)}/' Maybe. I am not super good at it, but that should cover some of the cases. The tricky thing about regexes is that they are tricky to cover corner cases...what if...? Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067340 Share on other sites More sharing options...
jamesxg1 Posted June 3, 2010 Author Share Posted June 3, 2010 '/{([^}]+)}/' Maybe. I am not super good at it, but that should cover some of the cases. The tricky thing about regexes is that they are tricky to cover corner cases...what if...? Spot on mate, cheers. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067343 Share on other sites More sharing options...
Psycho Posted June 3, 2010 Share Posted June 3, 2010 Another option '/{(.*?)}/' Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067344 Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 Another option '/{(.*?)}/' What is '.*? '? Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067346 Share on other sites More sharing options...
jamesxg1 Posted June 3, 2010 Author Share Posted June 3, 2010 Another option '/{(.*?)}/' Thats what I had wrote originally but I thought it might be unstable, is this the case? Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067349 Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 Another option '/{(.*?)}/' Thats what I had wrote originally but I thought it might be unstable, is this the case? . means any character * means zero or more of the preceding character ? means zero or one of the preceding character (character could be a set of characters, or whatever, a unit e.g. (blah)*, a*, [a-s]*...) So what is zero or one of zero or more of any character? I don't think it makes sense, but I could be missing something, that's why I asked if he knows what it really means. The problem with '{.*}' would be that .* can be a }, which is not what you want. You want to match opening brace, anything other than a closing brace, then a closing brace, so '{[^}]*}'. Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067351 Share on other sites More sharing options...
Psycho Posted June 3, 2010 Share Posted June 3, 2010 The question mark makes the match "non greedy". If you leave the '?' off the expression and there are two words within braces you would get everything from the start of the first word to the end of the last word (as dabaR stated). The ? modifier corrects this by making the match non greedy Regular Expression Tutorial Part 5: Greedy and Non-Greedy Quantification Example: $string = "This {is a} string {of} text."; //Without the non-greedy modifier preg_match_all('/{(.*)}/', $input, $matches); print_r($matches); //Ouput: //Array //( // [0] => Array // ( // [0] => {is a} string {of} // ) // [1] => Array // ( // [0] => is a} string {of // ) //) //With the non greedy modifier preg_match_all('/{(.*?)}/', $input, $matches); print_r($matches); //Output: //Array //( // [0] => Array // ( // [0] => {is a} // [1] => {of} // ) // [1] => Array // ( // [0] => is a // [1] => of // ) //) Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067358 Share on other sites More sharing options...
dabaR Posted June 3, 2010 Share Posted June 3, 2010 Ah OK, there, I learned something too. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/203793-what-preg_match_all-regex-would-i-use/#findComment-1067368 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.