in reply to string substitution

Assuming "on either side" to mean "on both sides" (see my post above), try this awfully ugly code:

use strict; use warnings; my $ariths = quotemeta '()+</*><-'; while ( <DATA> ) { s/(?:(?:(?<=[$ariths])BAD)|^BAD)(?=[$ariths])/HELL/g; print; } __DATA__ +BAD # No BAD/ # Yes BAD) # Yes (BAD<) # Yes BAD # No BADGER # No BADGER+ # No +SINBAD+ # No BAD (BAD+ # No then Yes

Improvements welcome!

dave

Update: davido's corrected regex above already represents a significant improvement!

Replies are listed 'Best First'.
Re: Re: string substitution
by Anonymous Monk on Oct 09, 2003 at 17:07 UTC
    Sorry for the late reply

    Basically, this:

    $string="HELL +HELL +(HELL) + (HELL) + ( HELL ) REALHELL REALHELL HELL +";

    Should be changed to this:

    BAD +BAD +(BAD) + (BAD) + ( BAD ) REALHELL REALHELL BAD

      $string =~ s/\bHELL\b/BAD/g; $string =~ s/(?<\W)\s|\s(?=\W)//g;
      First pass does the HELL -> BAD substitution based on word boundries. So far none of your examples have shown characters that are not either mathematical symbols, alpha, or space. So I'm not going to waste your time with worrying about whether BAD is next to a mathematical symbol. If your examples showed non-mathematical symbols, then I'd worry about that part again.

      The second pass removes any whitespace that has a non-word character on one side or the other of it. That will get rid of the space between BAD and +, but not between REALHELL and REALHELL.

      Until we know what you're really trying to do we will continue playing the guessing game and falling short of your needs. Be sure to read perlretut and perlre. It's your turn to come up with a solution.


      Dave


      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
        Thanks Dave,

        I will read as recommended. As an aside, I appreciate your help. Using \b...\b worked perfectly.

        For those who care, I am building an application that will parse sar files on any UNIX machine running sar (aix, solaris, hp-ux, sgi, linux, etc.) and either:

        1) create a graph of user selected data (selected by option & date(s)

        2) apply performance tuning rules for that specific OS (version & OS) based upon en expert system & rules base.

        This question dealt directly with the rules base, namely converting:

        ((0 < SML_MEM_FAIL ) or (0 < LG_MEM_FAIL ) or (0 < OVSZ_MEM_FAIL )) and ( FREEMEM <= 64 )

        without being to picky about spaces and the such.

        thanks

        g