http://qs321.pair.com?node_id=556891


in reply to Regex to replace a particular part of content

First off, what did you try so far? Please read Is PM a good place to get answers for homework?.

Then, what you are referring to as paragraphs in your example are lines. This may seem picky, but the distinction between lines and paragraphs makes sense. There is a paragraph mode reading files. See perlrun, there the switch -0 and the special value 00 for it.

blah blah blah #line \ blah blah blah #line - paragraph blah blah blah #line / <--- paragraph separator blah blah blah #line \ blah blah blah #line - paragraph blah blah blah #line /

I would do this:

#!/usr/bin/perl $file = shift; open(I,"<$file"); local $/; $_ = <I>; s|<list>(.*?)</list>|"<list>".join("\n",map{"<item>$_</item>"} split"\n",$1)."</list>"|ges; print;
Note that the "\n" for join and split should be "\r\n" for Windows and "\r" for Mac systems.

You can stuff that into a one-liner:

perl -p00 -e 's|<list>(.*?)</list>|"<list>".join("\n",map{"<item>$_</i +tem>"}split"\n",$1)."</list>"|ges;' textfile
Now go and read perlre and perlvar.

greets,
--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}