victorz22 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am want to create a function to replace strings in a multi-line variable. The part I am stuck on is setting up the parse to go line by line. Thank you monks!

#Function Call my $newText = replaceText($inputText, $regex, $replacement); #Function sub replaceText{ my($inputText, my $regex, my $replacement)= @_; my $replacedText; if($inputText =~ s/$regex/$replacement/g){ $replacedText = $1; } return $replacedText; }

Replies are listed 'Best First'.
Re: searching and replacing strings in a multi line variable
by huck (Prior) on Apr 20, 2017 at 22:56 UTC

    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; }

      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:  <%-{-{-{-<

      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>
Re: searching and replacing strings in a multi line variable
by LanX (Saint) on Apr 20, 2017 at 22:42 UTC
    Your code allows several possible interpretations.

    Could you please give us an example of the input and output data you expect?

    Plus $regex and $replacement, please,

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!