
voltaire
New User
Feb 27, 2005, 5:00 AM
Post #1 of 5
(2021 views)
|
|
conditional find+replace using regex
|
Can't Post
|
|
Is there a one-line regex way to specify a replacement in a string if certain conditions need to be met... eg if i had the string a b a b a b <tag> a b a b </tag> a b <tag> a b a b </tag> a b and i wanted to replace all 'a' with 'z' iff they are enclosed within a <tag></tag> pair (and i dont mean the first opening tag and the last closing tag) ie a b a b a b <tag> z b z b </tag> a b <tag> z b z b </tag> a b There are ways to do it using several lines of string manipulation, but i was wondering if there are some neat regex tricks to specify the condition neatly? eg hoping for a solution of the form $string =~ s/x/y/ where x and y describe/fulfill the condition (if it's even possible) many thanks
|