Let's say I want to write a regular expression to change all <abc>
, <def>
, and <ghi>
tags into <xyz>
tags.. and I also want to change their closing tags to </xyz>
. This seems like a reasonable regex (ignore the backticks; StackOverflow has trouble with the less-than signs if I don't include them):
`s!<(/)?(abc|def|ghi)>!<${1}xyz>!g;`
And it works, too. The only problem is that for opening tags, the optional $1 variable gets assigned undef, and so I get a "Use of uninitialized value..." warning.
What's an elegant way to fix this? I'd rather not make this into two separate regexs, one for opening tags and another for closing tags, because then there are two copies of the taglist that need to be maintained, instead of just one.
Edit: I know I could just turn off warnings in this region of the code, but I don't consider that "elegant".