Search code examples
regexreplacetextwrangler

TextWrangler FInd/Replace with


I have long lists of location data, which is in XML format.

<location><city>London</city><name>Zoo</name><latitude>...</location>

Unfortunately the XML is corrupt and I have to replace/fix a lot of invalid close XML tag occurances. Example, here </name> has to be replaced with </city>:

<location><city>London</name><latitude>...</location>

Using TextWrangler Find+Replace I can search for:<city>[A-Za-z]*</name> That statement searches and finds all the invalid lines. So far so good.

Now I want to replace them automatically (Replace All). Normally in regular expressions this is done by defining: <city>%1</city> but that is not working. It overwrites the found text, not interpreting the search result nor inserting the value in the %1 tag.


Solution

  • You need to capture the in-between characters using capturing group, so that you can reference the captured chars in the replacement part by specifying it's index number like %1 or \1 or $1

    <city>([A-Za-z]*)</name>