Quick RegExp Job For A Bag Of Green

Discussion in 'Services' started by T0PS3O, Mar 7, 2006.

  1. #1
    Green rep up for grabs for the first to give me working code that replaces:

    [::anything here::]

    with empty space i.e. removes it from $string.

    Between the 'opening tag' [:: and the 'closing tag' ::] can be any case alpha numeric and the usual _ - @. Between those tags is an unknown amount of characters and spaces are possible.

    So:

    $string = "The dog was walking [::special shite-here::] on the street."

    Becomes:

    $string = "The dog was walking on the street."

    (That actually leaves one space too many, if you wipe that as well you're star.)

    I suck at RegExps.

    Thanks!
     
    T0PS3O, Mar 7, 2006 IP
  2. mincus

    mincus Peon

    Messages:
    63
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    This is how I would do it in Perl:
    
    my $string = "The dog was walking [::special shite-here::] on the street.";
    $string =~ s/\s?\[::(.*?)::]\s?/ /g;
    Code (markup):
    This also saves the data between the tags in the variable $1
     
    mincus, Mar 7, 2006 IP
  3. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    Thanks! PHP RegExps are PErl-like I believe, do you know the PHP equivalent?
     
    T0PS3O, Mar 7, 2006 IP
  4. mincus

    mincus Peon

    Messages:
    63
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    maybe something like:

    preg_replace( "/\s?\[::(.*?)::]\s?/", " ", "The dog was walking [::special shite-here::] on the street." );
    Code (markup):
    It's just a guess though, I don't venture into PHP much, mostly Perl and Javascript these days.
     
    mincus, Mar 7, 2006 IP
    T0PS3O likes this.
  5. NubKnacker

    NubKnacker Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    I suck at regxps myself so i'd just use.
    
    $myStr = "The dog was walking [::special shite-here::] on the street.";
    $start = strpos($myStr,"[::") + 3;
    $end = strpos($myStr, "::]");
    $total = $end - $start;
    $temp = substr($myStr, $start, $total);
    $myStr = str_replace("[::".$temp."::]"," ", $myStr);
    
    Code (markup):
    Sorry, couldn't resist writing it. :-/
     
    NubKnacker, Mar 7, 2006 IP
    T0PS3O likes this.
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    LOL, that's what I would do as well but I dislike longwinded ugly code more and more hence the RegExp whish.

    I'll use it for the time being :)
     
    T0PS3O, Mar 8, 2006 IP