Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Regex for <!--O--> ??

    This seems like it would be a pretty simple REGEX, but even simple REGEX seems to trip me up
    I have special opening and closing blocks in my file ("<!--O-->" and <!--C-->", respectively), and I'm trying to select everything in that block (working toward the goal of replacing it).

    Here's my latest attempt
    PHP Code:
    $subject 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
    $pattern '/\<!--O--\>*.\<!--C--\>/';
    preg_match($pattern$subject$matches);
    print_r($matches); 
    I'm trying to find "<!--O--> Hi there. <!--C-->", but $matches continues coming up empty.

    What am I missing?
    ~ Mo
    Last edited by mOrloff; 11-09-2011 at 11:42 PM.
    ...because it's dundant already.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Try:

    Code:
    $pattern = '~\<\!--O--\>*.\<\!--C--\>~';

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Hrmph!
    Still no luck.
    My test block now consists of:
    PHP Code:
    $subject 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
    $pattern '~\<\!--O--\>*.\<\!--C--\>~';
    preg_match($pattern$subject$matches);
    print_r($matches); 
    Other thoughts??

    ~ Mo
    ...because it's dundant already.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    You have your syntax slightly incorrect. Just noticed it. *. should be .*

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Ahah!
    That works.
    Thanks.
    Last edited by mOrloff; 11-09-2011 at 11:59 PM.
    ...because it's dundant already.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Dang it. I typed in the answer and then went to the bathroom and came back and you beat me to the punch.

    PHP Code:
    $pattern '/\<!--O--\>.*\<!--C--\>/'
    * says 0 or more and the . indicates any character so that means match any pattern with the beginning and end you specified that has 0 or more characters in between them.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Furthermore, I would probably end up saving myself some effort if I were only selecting what was between those tags (so only returned "Hi there.").

    Is that easy to do, or would that become a disaster??
    What key words should I be searching?
    ~ Mo
    ...because it's dundant already.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Wrap .* in ( and ). That will return it in its own subpattern match.

  • #9
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Cool, that worked.

    If my end goals to use it directly in a preg_replace() call, though, how would I do that?

    Or would I have to run a preg_match(), and then just work with $matches[1]?

    ~ Mo
    ...because it's dundant already.

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by mOrloff View Post
    Cool, that worked.

    If my end goals to use it directly in a preg_replace() call, though, how would I do that?

    Or would I have to run a preg_match(), and then just work with $matches[1]?

    ~ Mo
    PHP Code:
    <?php
    $replacement 
    "All Hail Spookster";
    $subject 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad'
    $pattern '/<!--O-->(.*)<!--C-->/';
    echo 
    htmlspecialchars($subject) . "</br>"
    $subject preg_replace($pattern$replacement$subject); 
    echo 
    htmlspecialchars($subject) . "</br>";
    ?>
    Output:
    Code:
    fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad
    fsdjf idsfi sidh dsfh All Hail Spookster sdofijos.dsflsad
    Last edited by Spookster; 11-10-2011 at 12:48 AM. Reason: added htmlspecialchar call
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    heh heh

  • #12
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Spookster View Post
    Dang it. I typed in the answer and then went to the bathroom and came back and you beat me to the punch.
    Should have held it.

  • #13
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Thanks.
    And if I only wanted to select the "Hi there" so that my special little blocks remain after the preg_replace(), how would I need to modify my regex?
    ~ Mo
    ...because it's dundant already.

  • #14
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $replacement = "\\1All Hail Spookster\\2";
    $subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
    $pattern = '~(<!--O-->).*(<!--C-->)~';
    or:

    Code:
    $replacement = "\\1All Hail Spookster\\3";
    $subject = 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad';
    $pattern = '~(<!--O-->)(.*)(<!--C-->)~';
    Last edited by MattF; 11-10-2011 at 01:01 AM.

  • #15
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:
    <?php
    $replacement 
    "All Hail Spookster";
    $subject 'fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad'
    $pattern '/(<!--O-->)(.*)(<!--C-->)/';
    echo 
    htmlspecialchars($subject) . "</br>"
    $subject preg_replace($pattern"$1 $replacement $3"$subject);  
    echo 
    htmlspecialchars($subject) . "</br>";
    ?>
    Output:
    Code:
    fsdjf idsfi sidh dsfh <!--O--> Hi there. <!--C--> sdofijos.dsflsad
    fsdjf idsfi sidh dsfh <!--O--> All Hail Spookster <!--C--> sdofijos.dsflsad
    Last edited by Spookster; 11-10-2011 at 01:11 AM. Reason: changed to doubles quotes around replacement concatenation
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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