Jump to content

Merge two expressions with different mods


Lumio

Recommended Posts

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.

Link to comment
Share on other sites

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 >>
)

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.