in reply to Regex to replace a particular part of content
You can't do it easily with a single regex, but you can do it with a modest amount of code:
use strict; use warnings; while (<DATA>) { if (m|<list>| .. m|</list>|) { s/^(?!<list>)|(?<=<list>)/<item>/g; s-(?=</list>)|(?<!</list>)(?=\n)-</item>-g; } print; } __DATA__ The first quick brown fox jumps over the lazy dog. <list>The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</list> The last quick brown fox jumps over the lazy dog.
Prints:
The first quick brown fox jumps over the lazy dog. <list><item>The quick brown fox jumps over the lazy dog.</item> <item>The quick brown fox jumps over the lazy dog.</item> <item>The quick brown fox jumps over the lazy dog.</item></list> The last quick brown fox jumps over the lazy dog.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex to replace a particular part of content
by johngg (Canon) on Jun 23, 2006 at 14:22 UTC | |
by GrandFather (Saint) on Jun 23, 2006 at 18:10 UTC | |
by johngg (Canon) on Jun 23, 2006 at 18:57 UTC |