in reply to Regex stored in a scalar

EDIT: Updated script. This works for me:
use strict; use warnings; print "Enter left side for search: "; my $LeftSide = <STDIN>; print "Enter right side replacement: "; my $RightSide = <STDIN>; chomp($LeftSide); chomp($RightSide); while(<DATA>){ print if s/$LeftSide/$RightSide/g; #print "Replaced \"$LeftSide\" with \"$RightSide\" at: line $.: $_ +" if ($_ =~ s/$LeftSide/$RightSide/g); } __DATA__ hello this. is line 2 line, 3 ),( this is test for line 4 ) testing line ,5 now testing line (6)
ran it with script.pl \),\( \)\n\(
Outputs:
C:\Users\James\Desktop>test.pl \),\( \)\n\( Replaced "\),\(" with "\)\n\(" at: line 3 "line, 3 \)\n\("

Posting some example input would be helpful :)

Replies are listed 'Best First'.
Re^2: Regex stored in a scalar
by Laurent_R (Canon) on Aug 21, 2015 at 20:11 UTC
    Hum, I was just going to tell something to the effect that your script was not very useful, but as I hit the reply button, I just saw your edited version. This is indeed much more to the point.
      I tested it then noticed that he was indeed trying to search and replace. Did a ninja edit ; I was also about to change ARGV's to <STDIN>'s and chomp them as well.
Re^2: Regex stored in a scalar
by poj (Abbot) on Aug 21, 2015 at 20:20 UTC
    try
    __DATA__ (123),(456),(789) (abc),(def),(ghi)
    the result should be
    (123) (456) (789) (abc) (def) (ghi)
    poj
      I was able to get that output by changing the script like:
      print $file $_ if s/$LeftSide/$RightSide/eegi;

      And then left side is just a comma ',' without the quotes and right side is "\n" WITH the quotes

      EDIT: Using above little snippet, Use left side as \),\( and right side as "\)\n\(". And from what I understand the above snippet is the same as doing:

      print $_ if s/$LeftSide/eval $RightSide/egi;

      So I also suggest reading PerlDoc: eval.

      Heres another more hackish way to get the job done haha:

      Create your main script like so, with keywords in the s///

      # main.pl # all this is just a template that creates "run.pl" use strict; use warnings; while(<DATA>){ print $_ if s/search_here/replace_here/gi; } __DATA__ (123),(456),(789) (abc),(def),(ghi)

      Then this following script will search and replace the keywords "search_here" and "replace_here" in the script above, with whatever you input and put it in "run.pl"!

      # prepare_run.pl open my $file, '+<', 'main.pl'; #your original script we will replace +keywords open my $run, '+>', 'run.pl'; #newly created script that we will execu +te below print "Enter left side of s///: "; chomp(my $LeftSide = <STDIN>); print "Enter right side of s///: "; chomp(my $RightSide = <STDIN>); while(my $line = <$file>){ print $run $line if $line !~ /.*search_here.*/ || /.*replace_here.*/; print $run $line if $line =~ s/(.*)search_here(.*)/$1$LeftSide$2/ && + $line =~ s/(.*)replace_here(.*)/$1$RightSide$2/; } close($file); close($run); system("run.pl"); #or whatever the the equivalent of your OS.

      Here is the script that the above will create and run:

      # run.pl, will be created after running "prepare_run.pl" while using " +main.pl" as a template. use strict; use warnings; while(<DATA>){ print $_ if s/\),\(/\)\n\(/gi; } __DATA__ (123),(456),(789) (abc),(def),(ghi)

      Download all the above and then just run "prepare_run.pl" and it will copy lines from main.pl while replacing keywords with your regex from STDIN and put it all in run.pl for execution. You can use \),\( and \)\n\( for STDIN per normal without using any quotes.

      Here is the output:

      (123) (456) (789) (abc) (def) (ghi)

        I liked the idea of generating a script so I developed your idea so that you don't have to escape the input string and you can configure the input/output sources

        #!perl use strict; use warnings; my $infile = 'in.txt'; my $outfile = 'out.txt'; print 'enter a match : '; chomp( my $match = <STDIN>); print 'enter a replacement : '; chomp( my $replace = <STDIN>); open OUT,'>','regex.pl' or die "Could not create regex.pl : $!"; $match = qr(\Q$match\E); my $regex = "s/$match/$replace/gi"; print "Regex = $regex\n"; while (<DATA>) { s{#REGEX#}{$regex}g; s{#INFILE#}{$infile}g; s{#OUTFILE#}{$outfile}g; print OUT $_; } close OUT; print "Done\n"; system( 'perl regex.pl'); __DATA__ #!perl use strict; use warnings; my $t0 = time(); open my $in,'<','#INFILE#' or die "Could not open #INFILE# : $!"; open my $out,'>','#OUTFILE#' or die "Could not open #OUTFILE# : $!"; my $count = 0; while (<$in>){ #REGEX#; print $out $_; ++$count; } my $dur = time() - $t0; print "$count records processed #INFILE# >> #OUTFILE# in $dur seconds\n";
        poj