Regular Expressions

What will we cover?
  • What regular expressions are
  • How to use regular expressions in Python programs
  • Regex support in JavaScript and VBScript

Definition

Regular expressions are groups of characters that describe a larger group of characters. They describe a pattern of characters for which we can search in a body of text. They are very similar to the concept of wild cards used in file naming on most operating systems, whereby an asterisk(*) can be used to represent any sequence of characters in a file name. So *.py means any file ending in .py. In fact filename wild-cards are a very small subset of regular expressions.

Regular expressions are extremely powerful tools and most modern programming languages either have built in support for using regular expressions or have libraries or modules available that you can use to search for and replace text based on regular expressions. A full description of them is outside the scope of this tutor, indeed there is at least one whole book dedicated to regular expressions and if your interest is roused I recommend that you investigate the O'Reilly book.

One interesting feature of regular expressions is that they manifest similarities of structure to programs. Regular expressions are patterns constructed from smaller units. These units are:

Note that because groups are a unit, so you can have groups of groups and so on to an arbitrary level of complexity. We can combine these units in ways reminiscent of a programming language using sequences, repititions or conditional operators. We’ll look at each of these in turn. So that we can try out the examples you will need to import the re module and use it’s methods. For convenience I will assume you have already imported re in most of the examples shown.

Sequences

As ever, the simplest construct is a sequence and the simplest regular expression is just a sequence of characters:

red

This will match, or find, any occurrence of the three letters ‘r’,’e’ and ‘d’ in order, in a string. Thus the words red, lettered and credible would all be found because they contain ‘red’ within them. To provide greater control over the outcome of matches we can supply some special characters (known as metacharacters) to limit the scope of the search:

Metacharacters used in sequences
ExpressionMeaningExample
^red only at the start of a line red ribbons are good
red$ only at the end of a line I love red
\Wred only at the start of a word it’s redirected by post
red\W only at the end of a word you covered it already

The metacharacters above are known as anchors because they fix the position of the regular expression within a sentence or word. There are several other anchors defined in the re module documentation which we don’t cover in this chapter.

Sequences can also contain wildcard characters that can substitute for any character. The wildcard character is a period. Try this:

>>> import re
>>> re.match('be.t', 'best')
<re.MatchObject instance at 864460>
>>> re.match('be.t', 'bess')

The message in angle brackets tells us that the regular expression ‘be.t’, passed as the first argument matches the string ‘best’ passed as the second argument. ‘be.t’ will also match ‘beat’, ‘bent’, ‘belt’, etc. The second example did not match because 'bess' didn’t end in t, so no MatchObject was created. Try out a few more matches to see how this works. (Note that match() only matches at the front of a string, not in the middle, we can use search() for that as we will see later!)

The next unit is a range or set. This consists of a collection of letters enclosed in square brackets and the regular expression will search for any one of the enclosed letters.

>>> re.match('s[pwl]am', 'spam')
<re.MatchObject instance at 7cab40>

This would also match 'swam' or 'slam' but not 'sham' since 'h' is not included in the regular expression set.

By putting a ^ sign as the first element of the group we can say that it should look for any character except those listed, thus in this example:

>>> re.match('[^f]ool', 'cool')
<re.MatchObject instance at 864890>
>>> re.match('[^f]ool','fool')

we can match ‘cool’ and ‘pool’ but we will not match ‘fool’ since we are looking for any character except 'f' at the beginning of the pattern.

Finally we can group sequences of characters, or other units, together by enclosing them in parentheses, which is not particularly useful in isolation but is useful when combined with the repetition and conditional features we look at next.

Repetition

We can also create regular expressions which match repeated sequences of characters by using some more special characters. We can look for a repetition of a single character or group of characters using the following metacharacters:

Metacharacters used in repetition
ExpressionMeaningExample
‘?’ zero or one of the preceding character. Note the zero part there since that can trip you up if you aren’t careful. pythonl?y matches:
pythony
pythonly
‘*’ looks for zero or more of the preceding character. pythonl*y matches both of the above, plus:
pythonlly
pythonllly
etc.
‘+’ looks for one or more of the preceding character. pythonl+y matches:
pythonly
pythonlly
pythonllly
etc.
{n,m} looks for n to m repetitions of the preceding character. fo{1,2} matches:
fo or
foo

All of these repetition characters can be applied to groups of characters too. Thus:

>>> re.match('(.an){1,2}s', 'cans')

The same pattern will also match: ‘cancans’ or ‘pans’ or ‘canpans’ but not ‘bananas’ since there is no character before the second 'an' group.

There is one caveat with the {m,n} form of repetition which is that it does not limit the match to only n units. Thus the example in the table above, fo{1,2} will successfully match fooo because it matches the foo at the beginning of fooo. Thus if you want to limit how many characters are matched you need to follow the multiplying expression with an anchor or a negated range. In our case fo{1,2}[^o] would prevent fooo from matching since it says match 1 or 2 ‘o’s followed by anything other than an ‘o’.

Greedy expressions

Regular expressions are said to be greedy. What that means is that the matching and searching functions will match as much as possible of the string rather than stopping at the first complete match. Normally this doesn’t matter too much but when you combine wildcards with repetition operators you can wind up grabbing more than you expect.

Consider the following example. If we have a regular expression like a.*b that says we want to find an a followed by any number of characters up to a b then the match function will search from the first a to the last b. That is to say that if the searched string includes more than one 'b' all but the last one will be included in the .* part of the expression. Thus in this example:

re.match('a.*b',’abracadabra')

The MatchObject has matched all of abracadab. Not just the first ab. This greedy matching behaviour is one of the most common errors made by new users of regular expressions.

To prevent this ‘greedy’ behaviour simply add a ‘?’ after the repition character, like so:

re.match('a.*?b','abracadabra')

which will now only match ‘ab’.

Conditionals

The final piece in the jigsaw is to make the regular expression search for optional elements or to select one of several patterns. We’ll look at each of these options separately:

Optional elements

You can specify that a character is optional using the zero or more repetition metacharacters:

>>> re.match('computer?d?', 'computer')
<re.MatchObject instance at 864890>

will match computer or computed. However it will also match computerd, which we don’t want.

By using a range within the expression we can be more specific. Thus:

>>> re.match('compute[rd]','computer')
<re.MatchObject instance at 874390>

will select only computer and computed but reject the unwanted computerd.

Optional Expressions

In addition to matching options from a list of characters we can also match based on a choice of sub-expressions. We mentioned earlier that we could group sequences of characters in parentheses, but in fact we can group any arbitrary regular expression in parentheses and treat it as a unit. In describing the syntax I will use the notation (RE) to indicate any such regular expression grouping.

The situation we want to examine here is the case whereby we want to match a regular expression containing (RE)xxxx or (RE)yyyy where xxxx and yyyy are different patterns. Thus, for example we want to match both premature and preventative. We can do this by using a selection metacharacter:

>>> regexp = 'pre(mature|ventative)'
>>> re.match(regexp,'premature')
<re.MatchObject instance at 864890>
>>> re.match(regexp,'preventative')
<re.MatchObject instance at 864890>
>>> re.match(regexp,'prelude')

Notice that when defining the regular expression we had to include both the options inside the parentheses, otherwise the option would have been restricted to prematureentative or prematurventative. In other words only the letters e and v would have formed the options not the groups.

Using Regular Expressions in Python.

We’ve seen a little of what regular expressions look like but what can we do with them? And how do we do it in Python? To take the first point first, we can use them as very powerful search tools in text. We can look for lots of different variations of text strings in a single operation, we can even search for non printable characters such as blank lines using some of the metacharacters available. We can also replace these patterns using the methods and functions of the re module. We’ve already seen the match() function at work, there are several other functions, some of which are described below:

re Module functions and methods
Function/MethodEffect
match(RE,string) if RE matches the start of the string it returns a match object
search(RE,string) if RE is found anywhere within the string a match object is returned
split(RE, string) like string.split() but uses the RE as a separator
sub(RE, replace, string) returns a string produced by substituting replace for re at the first matching occurrence of RE. Note this function has several additional features, see the documentation for details.
findall(RE, string) Finds all occurences of RE in string, returning a list of match objects
compile(RE) produces a regular expression object which can be reused for multiple operations with the same RE. The object has all of the above methods but with an implied re and is more efficient than using the function versions.

Note that this is not a full list of re’s methods and functions and that those listed have some optional parameters that can extend their use. The listed functions are the most commonly used operations and are sufficient for most needs.

A Practical Example Using Regular Expressions

As an example of how we might use regular expressions in Python let’s create a program that will search an HTML file for an IMG tag that has no ALT section. If we find one we will add a message to the owner to create more user friendly HTML in future!

import re

# detect 'IMG' in upper/lower case allowing for
# zero or more spaces between the < and the 'I'
img = '< *[iI][mM][gG] ' 	

# allow any character up to the 'ALT' or 'alt' before >
alt = img + '.*[aA][lL][tT].*>'

# open file and read it into list
filename = raw_input('Enter a filename to search ')
inf = open(filename,'r')
lines = inf.readlines()

# if the line has an IMG tag and no ALT inside
# add our message as an HTML comment
for i in range(len(lines)):
  if re.search(img,lines[i]) and not \
     re.search(alt,lines[i]):
        lines[i] += '<!-- PROVIDE ALT TAGS ON IMAGES! -->\n'

# Now write the altered file and tidy up.
inf.close()
outf = open(filename,'w')
outf.writelines(lines)
outf.close()

Notice two points about the above code. First we use re.search instead of re.match because search finds the patterns anywhere in the string whereas match only looks at the start of the string. Secondly we use a statement continuation character ‘\’ in the if statement. This just allows us to lay the code out over two lines which looks a little neater, especially if there are many expressions to be combined.

This code is far from perfect because it doesn’t consider the case where the IMG tag may be split over several lines, but it illustrates the technique well enough for our purposes. Of course such wanton vandalism of HTML files shouldn’t really be encouraged, but then again anyone who doesn’t provide ALT tags probably deserves all they get!

We’ll see regular expressions at work again in the Grammar Counter case study, meantime experiment with them and check out the other methods in the re module. We really have just scratched the surface of what’s possible using these powerful text processing tools.

JavaScript

JavaScript has good support for regular expressions built into the language. In fact the string search operations we used earlier are actually regular expression searches, we simply used the most basic form - a simple sequence of characters. All of the rules we discussed for Python apply equally to Javascript except that regular expressions are surrounded in slashes(/) instead of quotes. Here are some examples to illustrate their use:

<Script type="text/javascript">
var str = "A lovely bunch of bananas";
document.write(str + "<BR>");
if (str.match(/^A/)) {
   document.write("Found string beginning with A<BR>");
}
if (str.match(/b[au]/)) {
   document.write("Found substring with either ba or bu<BR>");
}
if (!str.match(/zzz/)) {
   document.write("Didn't find substring zzz!<BR>");
}
</Script>

The first two succeed the third doesn't, hence the negative test.

VBScript

VBScript does not have built in regular expressions like JavaScript but it does have a Regular Expression object that can be instantiated and used for searches, replacement etc. It can also be controlled to ignore case and to search for all instances or just one. It is used like this:

<Script type="text/vbscript">
Dim regex, matches
Set regex = New RegExp

regex.Global = True
regex.Pattern = "b[au]"

Set matches = regex.Execute("A lovely bunch of bananas")
If matches.Count > 0 Then
   MsgBox "Found " & matches.Count & " substrings"
End If
</Script>

That's all I'll cover here but there is a wealth of subtle sophistication in regular expressions, we have literally just touched on their power in this short topic. Fortunately there is also a wealth of online information about their use, plus the excellent O'Reilly book mentioned at the start. My advice is to take it slowly and get accustomed to their vagaries as well as their virtues.

Points to remember
  • Regular expressions are text patterns which can improve the power and efficiency of text searches
  • Regular expressions are notoriously difficult to get right and can lead to obscure bugs - handle with care.
  • Regular Expressions are not a cure all and often a more sophisticated approach may be needed, if it doesn't work after say 3 attempts consider another approach!
Previous  Next  Contents


If you have any questions or feedback on this page send me mail at: alan.gauld@yahoo.co.uk