#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    30

    S/(.*)/$1 something/gi; "double dipping"


    I have some user variables (a high-level user with knowledge of regex and admin privileges, so it doesn't need to be sanitized) going into a substitution which is equivalent to:
    Code:
    $i = "TEST";
    $i =~ s/(.*)/$1 Blah/gi;
    print $i;
    And it outputs:
    TEST Blah Blah
    (it's doing it twice) Ok, so I know that the issue is the global. The problem is that sometimes I would want the same code to handle something like:
    Code:
    $i = "TEST THING is the BEST THING";
    $i =~ s/(.*?) THING/$1 Blah/gi;
    print $i;
    which it correctly outputs:
    TEST Blah is the BEST Blah
    Do I have to make some sort of conditional testing for the veracity of the wildcard with different multiple substitution syntaxes (or additional variable input allowing the user to turn on/off the global flag, which is what I'll probably end up doing), or is there a simpler way?

    Thanks
    If you can read this, I'm probably very confused and disorientated.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Actually I believe you're running into a weirdness with regular expressions. You're using .* which is causing the replace to be evaluated twice. Try using .+ and .+? in your two expressions, respectively.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    55
    Rep Power
    30
    Thanks for the response... I did end up just adding a separate user variable (checkbox) that turns on/off the global flag, and it works fine.
    If you can read this, I'm probably very confused and disorientated.

IMN logo majestic logo threadwatch logo seochat tools logo