Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to replace an xml attribute

Tags:

regex

I have an xml file of the form:

<property name="foo" value="this is a long value">stuff</property>

There are many properties but I want to match the one with name foo and then replace its value attribute with something else as so:

<property name="foo" value="yet another long value">stuff</property>

I was thinking to write a regular expression to match everything after "foo" to the end of the tag ( ">" ) and replace that, but I can't seem to get the syntax right.

I'm trying to do this using sed, if that's any help.

like image 873
Daniel Avatar asked Dec 18 '22 08:12

Daniel


2 Answers

/property name=\"foo\" value=\"([^\"]*)\"/

Then just replace the first submatch with the new value of your wishing.

like image 67
Tommi Forsström Avatar answered Mar 21 '23 09:03

Tommi Forsström


You probably don't want to use a regex for manipulating an xml file. Please instead consider xslt, which is aware of xml rules and won't cause your transformed document to become malformed.

like image 40
SingleNegationElimination Avatar answered Mar 21 '23 09:03

SingleNegationElimination