in reply to Re: Help with multiple line regex subsitution
in thread Help with multiple line regex subsitution
The s modifier might help you.The //s regex modifier would not seem helpful since it modifies the behavior of the . (dot) regex metacharacter, and this metacharacter is used in neither the OP nor in your reply.
The OPer seems to be looking for a simple way to get the contents of the file as a single string. A simple way to do this is with the idiomatic statement
my $string = do { local $/; <$fh> }
(where $fh is an already-opened file handle). After modification, this string can be printed with a single print statement. Caution: this approach does not scale well to files of more than several megabytes (depending on your system's memory).
Also, the following regex accepts only balanced double-quotes on the "string" and requires the trailing '{' character.
Updates:>perl -wMstrict -le "my $s = join '', @ARGV; $s = eval qq[qq[$s]]; my $words = qr{ foo | bar }xms; $s =~ s{ (Kw \s+) (\"?) ($words) (\2 \s* \{) }{$1${2}1_$3$4}xmsg; print 'output:'; print $s; " "Kw \n foo \n { \n Kw \n \"bar\"\n { \n Kw foo { Kw \"foo\" { " "Kw \"foo { Kw foo\" { Kw foo Kw {" output: Kw 1_foo { Kw "1_bar" { Kw 1_foo { Kw "1_foo" { Kw "foo { Kw foo" { Kw foo Kw {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Help with multiple line regex subsitution
by Porculus (Hermit) on Apr 14, 2009 at 22:18 UTC | |
by AnomalousMonk (Archbishop) on Apr 15, 2009 at 16:43 UTC |