in reply to Re: Multiline Regex replacement in Multiline file
in thread Multiline Regex replacement in Multiline file

Hi Athanasius, Thanks for that stuff. But I already have a working code like this. But I wanted to avoid the below "for" loop and instead do multi regex replacement.
for my $matchkey (keys %dic) { $line =~ s%$matchkey%$dic{$matchkey}%g; }
Also for each regex I have seperate set of pre defined replacement String not like Replace1 or Replace2( that was just used as an example) and I want replace the complete matched string with predefined Replacement String.

Replies are listed 'Best First'.
Re^3: Multiline Regex replacement in Multiline file
by Athanasius (Archbishop) on Sep 16, 2014 at 03:57 UTC

    Hello again, akamboj84,

    But I wanted to avoid the below "for" loop and instead do multi regex replacement.

    ...as my regex file is a long one and also i need to process a lot of files, so its kind of time/cpu consuming. I want to get rid of "for" loop...
    Re^3: Multiline Regex replacement in Multiline file

    There may be some clever way to do this using Perl’s extended regular expression patterns, but if so I haven’t found it. But deployment of the for loop can likely be made much more efficient. At present, the regexen are being applied to the whole input file; but if you can read in a paragraph at a time, this will save a lot of processing, as there will then be no need for the regex engine to re-search those parts of the input string that have already been matched and replaced:

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi Athanasius, the only solution i was looking for was to avoid loops, otherwise i am already doing it via looping through Keys and replacing matching string. I am not an expert but this is what i have been doing already :)

      my $line=join "", <F>; foreach $k (keys %dic) { $line =~ s/$k/$dic{$k}/gism; } print $line;
        You can create a pattern that matches all the keys, and then replace:
        my $pattern = join '|', map quotemeta, keys %dic; $line =~ s/($pattern)/$dic{$1}/g;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ