Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Regex to replace a particular part of content

by shmem (Chancellor)
on Jun 22, 2006 at 11:26 UTC ( #556891=note: print w/replies, xml ) Need Help??


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}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://556891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2022-08-18 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?