#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    UK
    Posts
    556
    Rep Power
    82

    Question Remove all chars except anything in the quotes.


    Hi this sounds easy but I can't get it to work.
    I want to remove all chars from the following string:
    Code:
    define('NAME', 'VALUE');
    and be left with
    Code:
    NAME, VALUE
    So far I have been able to remove define(' but nothing else. everything I try breaks.
    This is the regex so far:
    Code:
    s/^define*\('([^']'*)//g;
    Its in perl
    Anyone wanna help?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,296
    Rep Power
    9400
    Think replace, not remove.

    Code:
    s/^define\('([^']+)', '([^']+)'\);/$1, $2/g;
    My Perl's a bit weak - that should get you most of the way.

    Comments on this post

    • prometheuzz agrees : Nothing wrong with your Perl! :)
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    UK
    Posts
    556
    Rep Power
    82
    Originally Posted by requinix
    Think replace, not remove.

    Code:
    s/^define\('([^']+)', '([^']+)'\);/$1, $2/g;
    My Perl's a bit weak - that should get you most of the way.
    Hi thanks for the reply.

    I couldn't get your example work however I took a differant approch and done this:

    Code:
    $txt='define(\'test1\',\'test2\');';
    
    $re1='.*?';	
    $re2='(?:[a-z][a-z]+)';
    $re3='.*?';	
    $re4='((?:[a-z][a-z]+))';	# Word 1
    $re5='.*?';	
    $re6='((?:[a-z][a-z]+))';	# Word 2
    
    $re=$re1.$re2.$re3.$re4.$re5.$re6;
    if ($txt =~ m/$re/is)
    {
        $word1=$1;
        $word2=$2;
        print "($word1) ($word2) \n";
    }
    I think my problem was that I wasn't breaking down the string into separate parts
  6. #4
  7. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    938
    requinix' suggestion works like a charm!

    perl Code:
    $text = "define('NAME', 'VALUE');";
    $text =~ s/^define\('([^']+)', '([^']+)'\);/$1, $2/g;
    print "$text\n";
    Last edited by prometheuzz; October 1st, 2009 at 04:30 PM.

IMN logo majestic logo threadwatch logo seochat tools logo