Background
Consider the following input:
<Foo
Bar="bar"
Baz="1"
Bax="bax"
>
After processing, I need it to look like the following:
<Foo
Bar="bar"
Baz="1"
Bax="bax"
CustomAttribute="TRUE"
>
Implementation
This is all I need to do for no more than 5 files, so using anything other than a regular expression seems like overkill. Anyway, I came up with the following (Perl) regular expression to accomplish this:
$data =~ s/(<\s*Foo)(.*?)>/$1$2 CustomAttribute="TRUE">/sig;
Problems
This works well, however, there is one obvious problem. This sort of pattern is "dumb" because if CustomAttribute
has already been added, the operation outlined above will simply append another CustomAttribute=...
blindly.
A simple solution, of course, is to write a secondary expression that will attempt to match for CustomAttribute
prior to running the replacement operation.
Questions
Since I'm rather new to the scripting language and regular expression worlds, I'm wondering whether it's possible to solve this problem without introducing any host language constructs (i.e., an if-statement in Perl), and simply use a more "intelligent" version of what I wrote above?