John Smith said:
I want to remove the first occurance of an xml tag from a string e.g. from
"<x><y>...</y><y>...</y><x>" to "<x><y>...</y><x>" the problem I have is
remove first with "<y[ >].*</y>" goes for the right most match and gives
"<x><x>" how can I make it take the left most match.
You can't, in full generality, if I understand what you want correctly.
You don't want to replace the *tag*, but the *element* (everything
from start tag to matching end tag).
A question you should answer is what the first x element is in.
<x>AB<x>CD</x>EF</x>
The two nested matching pairs of x-start/end tags can both be seen as
the "first". One starts first, the other ends first.
In either case, finding matched pairs in a string where pairs can be
(arbitrarily) nested, is beyond the capability of regular expressions
(and into the realm of context free grammars).
If you *know* that the element you look for can't be nested, i.e., a
not as general problem, you can use non-greedy matching:
"<tagname\\b.*?</tagname>"
This matches from "<tagname" to the first followin "</tagname>" (which
would be entirely wrong for the nested example above

.
Good luck
/L