Sed non greedy match: matching first xml nodes

756 views Asked by At

Follow up to this question

$test = "sed -n '1h;1!H;\${;g;s/<item=\"".$name.".*</\item>/".trim(xml)."/g;p;}' ".$file;
exec($test,$cmdresult);

This command executes to find all xml nodes with the specified name passed it as a variable. The only problem here is that the match goes beyond the initial </item> to the next nodes, finally to the last </item>.

How do I make this non greedy?

1

There are 1 answers

0
morphles On

Sometimes its better to use negative class than non greedy match. It allows finer control and is a bit more efficient. I.E. to match everything but not tag close you can do [^>]*. Though if you can have strings that can have ">" in them this will not work. You can read about this a bit more here: http://www.regular-expressions.info/repeat.html