SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RegEx I can't solve

    Hello,

    Here's one I can't solve.

    I need to remove a string if it's doesn't appear in comments (only comments targeted are "/**/"). Everywhere else, the string should be removed.

    PHP Code:
    $foo = var;
    /* And this is some comment $foo = var; which isn't touched*/ 
    Output should become:
    PHP Code:
    /* And this is some comment $foo = var; which isn't touched*/ 
    $foo = var; has been removed. "$foo = " will never change. However, "var" could be anything.

    Thanks in advance for the help.



    -jj.

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    If you have only one-line comments then using lookaheads in regex can be used:
    PHP Code:
    // Assume $s contains the original string

    // this will work for single-line comments only
    $out preg_replace('#(\$foo *= *\w+;)(?!.*?\*/)#'''$s); 
    This will work as long as $foo = var is either in one-line comments or is in the last line of a multi-line comment because it only checks for the closing */.

    Unfortunately, PHP doesn't support variable-length lookbehinds so this can't be used for multi-line comments because we have no way of detecting preceding comment content back to the opening comment tag, which can be any length. So a workaround can be used by first splitting the string into non-comment sections and comment sections and then doing the replacements only in the non-comment ones:

    PHP Code:
    // split string into non-comment elements and comment elements
    $split preg_split('#(/\*.*?\*/)#s'$snullPREG_SPLIT_DELIM_CAPTURE);

    $out '';

    foreach (
    $split as $key=>$elem) {
        if (
    $key == 0) {
            
    // element with no comment - do replacements
            
    $out .= preg_replace('#(\$foo *= *\w+;)#'''$elem);
            
        } else {
            
    // element with commented code - copy as is
            
    $out .= $elem;
        }

    In these examples var can be any word character, that is letters, numbers and underscore.

    Edit: if you want any content as var, then simply replace
    Code:
    (\$foo *= *\w+;)
    with
    Code:
    (\$foo *=.*?;)
    - but I guess that is the easy part


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
  •