in reply to how do I swap 2 strings in a file?

Why don't you try this to switch the strings. It doesn't involve a regex, and is easier to follow:
while(<ORIG>){ if ($_ eq $string_1) {$string .= $string_2; next;} elsif ($_ eq $string_2) {$string .= $string_1; next;} $string.= $_; }


The 15 year old, freshman programmer,
Stephen Rawls

Replies are listed 'Best First'.
Re: Re: how do I swap 2 strings in a file?
by TGI (Parson) on May 09, 2001 at 20:45 UTC

    I don't think this will work. This assumes that the string to replace takes up a whole line (or chunk based on whatever you've got $/ set to).

    If the file looks like this:

    1221323asdl;lerTHIS IS
    THE STRING I WANTasdlf
    asdajla;sdfasdfasdfass
    

    nothing will happen.

    That being said, the above technique is a clever way to change (or slightly modified, insert) whole lines in a file.


    TGI says moo

      Ok, agreed. But I'd still like to clean up that regex, so here it goes:
      $string=~s/($string_1)(.*)($string_2)/$3$2$1/msg;
      That should be a bit better than matching the whole string, also I added the /m modifier. I put the /s modifier in too because I thought /msg just looked cool : )

      The 15 year old, freshman programmer,
      Stephen Rawls