Jan 27 2008

Regular Expressions in Notepad++

Tag: Notepad++, Regular ExpressionsTibby @ 12:15 am

Like a lot of people, before I got to know regular expressions they seemed like strange and untameable beasts. Most of the people I’ve spoken to agree (granted, they are undergraduates) but it doesn’t take long while getting to know regular expressions to realise that they are insanely useful.

Notepad++ comes with the capability to search as well as replace using regular expressions. I was knocking together an XML file which was part of some coursework I was working on when I found out just how useful this capability can be. The file had already grown fairly large when I realised that I wanted to change the structure of it. There were some attributes that I wanted to convert to child elements, but changing each one manually would be a long and tedious job. Of course, tracking down the way that Notepad++ handled regular expressions, including back referencing, probably took as long, but it was a whole lot more fun.

How to do it

First, here’s an example piece of XML to work from.

<?xml version="1.0"?>
<library>
	<book title="The Light Fantastic" author="Terry Pratchett"/>
	<book title="The Subtle Knife" author="Phillip Pullman"/>
	<book title="Dune" author="Frank Herbert"/>
</library>

Here is what I want the end result to be.

<?xml version="1.0"?>
<library>
	<book>
		<title>The Light Fantastic</title>
		<author>Terry Pratchett</author>
	</book>
	<book>
		<title>The Subtle Knife</title>
		<author>Phillip Pullman</author>
	</book>
	<book>
		<title>Dune</title>
		<author>Frank Herbert</author>
	</book>
</library>

Of course, this is just for illustration - the real thing might be hundreds or thousands of lines long.

The first thing to do is to bring up the Replace dialogue - Ctrl + H in Notepad++ and make sure that the “Regular expression” box is checked. The regular expression to actually find each element is relatively simple. The following can be typed into the “Find” field.

<book title="([^"]*)” author=”([^"]*)”/>

([^"]*) just means a sequence of any characters that are not quote characters - so basically all the text that is the value of the attributes title and author up to the respective end quotes. We will want to get these two pieces of text later when we make the string that constructs the child elements in the “Replace” field. The parentheses that can be seen in the above code are what actually allow this.

<book>
	<title>{title}</title>
	<author>{author}</author>
</book>

The string required is only slightly more complex than what is seen above. Notepad++ allows \n and \t escape characters to denote newlines and tab characters respectively in the “Replace” field, so you can even specify the code to be correctly formatted. The variables {title} and {author} are specified in the replace string with what are known in regular expression land as back references. Back referencing is very simple in Notepad++ - \1 refers to the first parenthesised piece of text, \2 refers to the second and so on.

<book>\n\t\t<title>\1</title>\n\t\t<author>\2</author>\n\t</book>

Using regular expressions to search and replace - or even just search - is immensely useful in many situations. Reformatting XML is not the application of this, although it’s certainly a very useful one.

Useful links




Abbie Gale mp3 song Akshara Weave review Alexi Delano and Jesper Dahlback dowland Ben Folds mp3 music Derek Bailey - Noel Akchote albums Eric Alexander downloadable music Fedde Le Grand Pres Flamingo Project mp3 search Gurdjieff and De Hartmann By Alain Kremski albums Hypnos 69 music download Imhotep download songs John Flomer pop Long John Hunter mp3 songs Loop Guru dowland Los Acosta mp3 music Louise Attaque dowland Luis Eduardo Aute songs Martyr A.D. mp3 song Noyau Dur download mp3 Peter, Paul and Mary mp3 downloads Ray Brown Trio With Gene Harris song downloads Stamatis Spanoudakis music download 8Ball and MJG dowland A.Harlow and L.Harlow download songs Alter ego best mp3 Artillery music downloading Bill Bruford dowland Biopsy review Candeia song downloads Crime in Stereo mp3s De Dijk instrumental Dj Noni mp3 songs Ellegarden mp3 music Freddie Foxxx Aka Bumpy Knuckl all mp3 Georges Rodi download Global Deejays Feat. Technotronic music to download Gregorian Chant new mp3 Larry Carlton mp3s Martin Carthy and Dave Swarbrick all mp3 Monica Zetterlund and Bill Evans downloads Ojoruido mp3 Papa San and Tippa Irie mp3 songs Puddle of Mudd mp3 online Richard Stoltzman With Judy Collins mp3s Silver Jews music downloading Terry Numan vs Dario Nunez album Thursday top mp3 Anal Nosorog mp3s Blitz get mp3 Daddy Yankee review De Luna top mp3 Deborah Coleman albums Deerhunter music download DJ Noizer and DJ Berry Morgana new mp3 Donny Hathaway download songs Fastkill mp3 songs Gazpacho mp3 search Get Him Eat Him music Giora Feidman english mp3 Hertz song downloads Hieroglyphix downloadable music Lee Haslam and Mearns all mp3 Link Protrudi and the Jaymen pop Locke album Loona downloadable music Los burros downloadable music Murder Corporation and Grind Buto top mp3 Planet Alliance mp3 search Richie Hawtin and Troy Pierce dowland Ron Korb music Sammy Hagar music to download Twilight Glimmer best mp3 Varttina best mp3 Vidna Obmana and PBK album Warrel Dane new mp3 Adamski download mp3 Aeon mp3 song Agoria albums Angelzero mp3 songs Blink-182 new mp3 Coctaeu Twins pop Devadip Carlos Santana and Turiya Alice Coltrane mp3 downloads Dracul mp3 songs Ewun mp3 search Fat Ray and Black Milk mp3 songs Gabrielle Roth mp3 songs Hound Dog Taylor and the Houserockers mp3 song In Gowan Ring music downloading Jark Prongo instrumental Mana mp3s Nando Reis music to download Otis Redding buy mp3 Paul Desmond Quartet with Jim Hall mp3 online Ronan Hardiman english mp3 Special A downloadable music Sussan Deyhim mp3 online Texas Tornados downloadable music Vandals mp3 Vopli Vidopliasova mp3 song Whitechapel dowland Wiseguys mp3 online