[Tutor] search and replace

Alan Gauld alan.gauld at freenet.co.uk
Tue Mar 7 10:33:03 CET 2006


Hi tak,

> hello, Othello. # just the hello and not Othello

One simplistic approach that does not use regex is to search for
spaces as part of the string. But that doesn't work for punctuation
nor at the start or end of lines.

So that leaves us, as you observed, with regular expressions.

regex allows us to specify certain conditions in the patterns 
such as whether the characters are digits etc, and also whether 
we are kooking for a word which is wat you want.
Specifically \W signifies a word boundary so

\Whello\W

will find hello as a word.

Take a look at my tutorial topic on regex for more detail, 
or go to the excellent regex How-To linked from the Python site.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





More information about the Tutor mailing list