First steps in GREP: Finding a number (Part 1)


I'm going to start by using TextWrangler as my example program but most of what I'll discuss is transferable to other programs that use GREP (and Regex or Regular Expression) find and replace systems, such as those found in OpenOffice and InDesign. In this brief example we'll look at finding a number between 0 and 9 then adding some text before it.

(1) open TextWrangler (if you don't already have it installed, download and install for free from MacAppStore)
(2) type some text containing numbers in a new document
(3) press Cmd + F
(4) tick the little box at the bottom of the dialog box that reads Grep
(5) in the top box type ([0-9])
(6) in the Replace box type Number \1

The [0-9] means find a number between 0-9 and if you'd written [a-z] it would have meant find a letter between a and z. Enclosing it in brackets means we can use the text in the Replace box by referring to it as \1 and if we'd had two expressions - e.g.  ([0-9])([a-z]) - then \1 would be the result of the first expression (i.e. the number) and \2 would be the result of the second (i.e. the letter). The word Number is just what it is. Try it and see.

Note: some systems of find and replace use the $ symbol in place of the \ and in these we would have written $1 instead.

Part 1 | Part 2

Comments