1. Home
  2. Computing & Technology
  3. Python

Python Regular Expressions (regex): Forming a Regular Expression in Python

From , former About.com Guide

10 of 10

An Explanation of Forming RegEx in Python

Let's look at the regex for a moment. There are some things here which have been briefly mentioned but not fully discussed.

>>> x = re.compile("^\S{3,5}.[aeiou]")

When forming a regular expression, it is important to remember that every pattern matching symbol equals exactly one character in the string to be matched. The only time this 1-to-1 relationship changes is when you put quantifiers in curly braces or bracket alternatives together in a square braces.

If you find yourself having difficulty in formulating the appropriate regex forumla, write it out long-style -- on-screen or on paper. Then, take it one character at a time. Even if you want more than one match, put each one down explicitly.

So if we want to begin with a consonant, we write:

^\S
Then, because we want three, we write out the three consonants:
^\S\S\S

Keep in mind here that these three symbols equate to three characters. If we then want the fifth character of the string to be a vowel, we need to enter a filler for the fourth position. Enter the period/full stop. Remember, the period/full stop equates to any character save the newline character ("\n").

^\S\S\S.
Then, if we want to add the vowels, we need to decide whether we want to include "y". Assuming no, we can place all the vowels together between a pair of square brackets and append them to the formula:
^\S\S\S.[aeiou]

Explore Python
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Regular Expressions
  5. Python Regular Expressions - Python Regex - An Explantaion of Forming RegEx in Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.