Main Content

Python Regular Expressions

Archive - Originally posted on "The Horse's Mouth" - 2008-11-08 07:35:31 - Graham Ellis

Python supports string pattern matching to regular expressions, using Perl style regular expressions.

The re module - loaded via
  import re
brings the appropriate elements into your program ... ready to use.

Patterns that you want to match against are called Regular Expressions and are created as objects from strings using the re.compile method. For example
  looklike = r'([a-z]+)\.([a-z]+)\[([^\]]+)\]'
  el_matcher = re.compile(looklike,re.IGNORECASE)


And you can then use the compiled regular expressions to find parts of data strings that match the pattern you have defined:
  zz = el_matcher.findall(thing)

If you're new to regular expressions, you'll probably find the definition strings - the regular expressions - a bit overpowering at first> but as you read into them the become easier. Let's analyse the major elements of the one above:

[a-z] A letter in the range a to z
+ One or more (of the previous item)
\. Literally a full stop
[a-z] A letter in the range a to z
+ One or more (of the previous item)
\[ Literally a [
[^\] Any character which is NOT a ]
+ One or more (of the previous item)
\] Literally a ]

Read that against sphere.v[32:58] and you'll see that it does indeed meet the description ... the extra round brackets in the original regular expression show Python which particular parts of the match are to be stored into separate objects that are returned from the match so that use can be made of them in subsequent lines of the program.

The complete example that I've been quoting from above - which matches a string of the type that may be produced by Autocad's Maya, for example, may be found here ... and we cover regular expressions on many of our courses (including our Python course, and indeed we run a special Regular Expression Course for those who need detailed coverage.