Lumio Posted February 9, 2009 Share Posted February 9, 2009 Hello! I got two expressions: /(\{\$[\w\d\[\]]+\})/ ans /(\<\<.+?\>\>/s How can I merge these two expressions? Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/ Share on other sites More sharing options...
.josh Posted February 9, 2009 Share Posted February 9, 2009 we can't possibly tell you that without you posting example data and what you are wanting to be matched. Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758375 Share on other sites More sharing options...
Lumio Posted February 9, 2009 Author Share Posted February 9, 2009 Okay. In the first case I face the following data: a little string with {$var} in it the second case: a little string with <<foo bar >> in it The first expression should just match in one line, the second in multiples. Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758389 Share on other sites More sharing options...
nrg_alpha Posted February 9, 2009 Share Posted February 9, 2009 Perhaps one suggestion could be? $str = 'a little string with {$var} in it. a little string with <<foo bar >> in it'; preg_match_all('#((?:{|<<)[^}>]+(?:}|>>))#s', $str, $matches); $matches[1] = str_replace("\r\n", '', $matches[1]); // clear out any newlines and such... echo '<pre>'.print_r($matches[1], true); Due to < and >, you'll have to view source to see the full results from the array. Output: <pre>Array ( [0] => {$var} [1] => <<foobar >> ) Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758409 Share on other sites More sharing options...
Lumio Posted February 9, 2009 Author Share Posted February 9, 2009 Thank you I made it a little bit different now: /\({\$[\w\d\[\]^\n]+\})|(\<\<.+?\>\>)/ Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758455 Share on other sites More sharing options...
nrg_alpha Posted February 9, 2009 Share Posted February 9, 2009 First, sorry for the s modifier in my version... realized afterwards that since I'm not using a dot match all, it's not needed.. my bad. (and I forgot about adding the dollar sign in my opening alternation...): #((?:{\$|<<)[^}>]+(?:}|>>))# This code also assumes that if something starts with {$, it will finish with } to match {$something}. Thank you I made it a little bit different now: /\({\$[\w\d\[\]^\n]+\})|(\<\<.+?\>\>)/ I don't think this will work..after your opening delimiter, you escape the first bracket \(, which means you are looking for a literal bracket in your string.. then your character class is not correct.. what this is saying is, look for any word character, or digit, or [ or ] or ^ or newline one or more times, then a \} (which you don't need to escape). Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758472 Share on other sites More sharing options...
nrg_alpha Posted February 9, 2009 Share Posted February 9, 2009 Another possible version: preg_match_all('#((?:{\$[^}]+}|<<[^>]+>>))#', $str, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/144517-merge-two-expressions-with-different-mods/#findComment-758490 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.