tags:

views:

68

answers:

1

I have an XML file like this:

<fruit><apple>100</apple><banana>200</banana></fruit>
<fruit><apple>150</apple><banana>250</banana></fruit>

Now I want delete all the text in the file except the words in tag apple. That is, the file should contain:

100
150

How can I achive this?

+4  A: 
:%s/.*apple>\(.*\)<\/apple.*/\1/

That should do what you need. Worked for me.

Basically just grabbing everything up to and including the tag, then backreferences everything between the apple begin and end tag, and matches to the rest of the line. Replaces it with the first backreference, which was the stuff between the apple tags.

Shawn D.
If more than one <apple> tag could appear on a line you might prefer to use a non-greedy match (using \{-\{) and replace all instances, adding newlines between individual results: :%s/.*apple>\(.\{-\}\)<\/apple.*/\1^M/g
Conspicuous Compiler