in reply to searching and replacing strings in a multi line variable

This should give you an example

use strict; use warnings; my $inputtext="a multi\nline\nstring\nanother line\n"; print $inputtext; my $newText = replaceText($inputtext, 'line', 'xx'); print "\n\n".$newText; sub replaceText{ my($inputText, my $regex, my $replacement)= @_; $inputText =~ s/$regex/$replacement/g; return $inputText; }

Replies are listed 'Best First'.
Re^2: searching and replacing strings in a multi line variable
by AnomalousMonk (Archbishop) on Apr 22, 2017 at 14:27 UTC

    Another little quibble. I would not pass a search/replacement pattern as a string; rather, as a  Regexp object as created by the  qr// operator. The reason is that  qr// m// s/// all speak the same language. Consider:

    c:\@Work\Perl\monks>perl -wMstrict -le "for my $sr ('k\s', 'k\\s', 'k\\\\s', qr{ k \\ s }xms, qr{k\\s}xms) { my $s = 'remove back\slash'; printf qq{'$s' %s -> }, ref $sr ? $sr : qq{'$sr'}; $s =~ s{$sr}{xx}xmsg; print qq{'$s'}; } " 'remove back\slash' 'k\s' -> 'remove back\slash' 'remove back\slash' 'k\s' -> 'remove back\slash' 'remove back\slash' 'k\\s' -> 'remove bacxxlash' 'remove back\slash' (?^msx: k \\ s ) -> 'remove bacxxlash' 'remove back\slash' (?^msx:k\\s) -> 'remove bacxxlash'

    Another advantage of using  Regexp objects to talk about regex patterns is precision of control. "The fact that the input is a multi-line string is not an issue..." except when it is. And the best time to consider this issue is at the point at which the regex is created, and perhaps in the context of the string against which you are matching.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $inputtext = qq{a multi line\nstring and\nanother line\nthing here +\n}; print qq{<<$inputtext>> \n}; ;; my $newText = replaceText($inputtext, qr{ lin .*? ing }xms, 'xx-xx'); print qq{<<$newText>>}; ;; sub replaceText { my ($inputText, $regex, $replacement) = @_; $inputText =~ s/$regex/$replacement/g; return $inputText; } " <<a multi line string and another line thing here >> <<a multi xx-xx and another xx-xx here >>
    In this example, the multi-line match and replacement works as expected even though the  s///g substitution ultimately used has no  /s "dot matches all" modifier. No matter. The behavior of dot was determined and could be understood at the point of creation of the  Regexp object; the object could have been passed around endlessly and its behavior would not change. Of course, the same behavior could be achieved using a  'lin(?s:.)*?ing' string literal to define the pattern, but that just qives you a few more regex hoops to jump through. (And on a related note, Regex Best Practice dictates that all  qr// m// s/// operators should effectively have an  /xms tail IMHO.)


    Give a man a fish:  <%-{-{-{-<

Re^2: searching and replacing strings in a multi line variable
by BillKSmith (Monsignor) on Apr 22, 2017 at 03:34 UTC
    This example does not compile. Remove the extra my's from 'my($input...)' as perl tells you. The example then works fine. The fact that the input is a multi-line string is not an issue.
    Bill

      While you are right that there are redundant "my"s , it compiles and runs just fine with no warnings or errors.And I never said multi anything was an issue

      D:\goodies\pdhuck\down1\perl\monks>perl 1188497.pl a multi line string another line a multi xx string another xx D:\goodies\pdhuck\down1\perl\monks>
        How strange.
        C:\Users\Bill\forums\monks>perl -v This is perl 5, version 24, subversion 1 (v5.24.1) built for MSWin32-x +64-multi-t hread Copyright 1987-2017, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. C:\Users\Bill\forums\monks>perl 1188497.pl Can't redeclare "my" in "my" at 1188497.pl line 11, near ", " Execution of 1188497.pl aborted due to compilation errors. C:\Users\Bill\forums\monks>
        Bill