SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression - doesn't match as expected

    Hi folks,

    I've got the following regexp:
    PHP Code:
    preg_replace('#{skip receiveNews}([^{]*){/skip receiveNews}#im'''$mailMessage); 
    This matches perfectly - but only if there is no "{" between my tags.
    If I use it as this:
    PHP Code:
    preg_replace('#{skip receiveNews}(.*){/skip receiveNews}#im'''$mailMessage); 
    it doesn't match.
    What I want to achieve is replace evereything between the two tags and the two tags but there may be other tags between those two tags.

    Examples:
    Working with RegExp 1 but not 2:
    this is a test.{skip receiveNews}
    You can only read this if you checked the checkbox.{/skip receiveNews}
    Working neither with RegExp 1 nor 2:
    this is a test.{skip receiveNews}
    You can only read this if you checked the checkbox.{skip something}
    Please check back soon{/skip something}
    {/skip receiveNews}
    I experimented and read a lot - but haven't found the solution yet.
    Would be really glad to see some comments & tips on this.

    Thanks!

  2. #2
    SitePoint Evangelist praetor's Avatar
    Join Date
    Aug 2005
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    {} must be escaped, so use \{ \} .

  3. #3
    Twitter - @CarlBeckel busy's Avatar
    Join Date
    May 2004
    Location
    Richmond, VA, USA
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this...
    PHP Code:
    preg_replace('/{skip receiveNews}.*{.skip receiveNews}/'''$mailMessage); 
    heres what I use for testing these:
    http://www.quanetic.com/regex.php

    It wasn't escaping for me, so that's why the . is in there before skip.

  4. #4
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm...normally I would also think so - but RegExp one _does_ work without that.
    It only doesn't match if I have other tags between those 2.
    But I'll give it a try when I'm back at the office.

  5. #5
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, doesn't work with the link you posted - "pattern not found".
    Pattern: {skip receiveNews}.*{.skip receiveNews}
    Haystack: outleft{skip receiveNews}Hello world!{/skip receiveNews}outright

  6. #6
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_replace("/\{skip receiveNews\}(.*)\{\/skip receiveNews\}/"''$mailMessage); 
    ... works for me, when $mailMessage is all on one line, but breaks when it includes a new line. I thought regular expressions in PHP were in multiline mode by default, so I'm not sure how to fix that.
    Blog
    PHP, ruby, web design and photography.

  7. #7
    Twitter - @CarlBeckel busy's Avatar
    Join Date
    May 2004
    Location
    Richmond, VA, USA
    Posts
    819
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by G.Schuster View Post
    Sorry, doesn't work with the link you posted - "pattern not found".
    Pattern: {skip receiveNews}.*{.skip receiveNews}
    Haystack: outleft{skip receiveNews}Hello world!{/skip receiveNews}outright
    Should have been
    Pattern: /{skip receiveNews}.*{.skip receiveNews}/


  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Code:
    $mailMessage2 = "this is a test.{skip receiveNews}You can only read this if you checked the checkbox {skip something}Please check back soon{/skip something}{/skip receiveNews}";
    
    
    $str2 = preg_replace("#{skip receiveNews}(.*){/skip receiveNews}#", '', $mailMessage2);
    Looks like you are discovering preg's multiline woes. Above outputs this is a test. until you stick a line end in the middle of it. Maddening.
    Last edited by Cups; Jun 17, 2007 at 21:31. Reason: forgot surrounding curly brackets

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by G.Schuster View Post
    If I use it as this:
    PHP Code:
    preg_replace('#{skip receiveNews}(.*){/skip receiveNews}#im'''$mailMessage); 
    it doesn't match.
    Add "s" modifier to make dot match a newline as well. "m" is not needed here.

    As to escaping { and }, regex engine is relaxed on this and allows bare { if not followed by digit.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    pcre - modifiers, dang it all, all those wasted hours ...

    Quote Originally Posted by manual
    s (PCRE_DOTALL)
    If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
    Thanks stereofrog

  11. #11
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awsome! That's it
    Thanks for all responses.

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    or could just change (.*) to ([^{]+)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •