[Tutor] regular expressions

Magnus Lyckå magnus at thinkware.se
Sun Jul 4 10:57:43 CEST 2004

At 10:11 2004-06-24 -0700, Larry Blair wrote:
>I am trying to use regular expressions to delete a wildcard set of tables.
>My pattern is 'TEMP*' The code below is how I am implementing the delete.

That's a file name wildcard pattern, not a regular expression
pattern. They are not at all the same thing. (This has really
little to do with Python.)

I think you might want a word boundry (\b) followed by TEMP
possibly followed by any word character (\w*).

I.e. pat = re.compile(r"\bTEMP\w*")

Note that A-Z, a-z, 0-9 and _ are the word characters, so if you
want other characters included as well, you might need to change
your pattern. (This also affects \b.)

See http://docs.python.org/lib/re-syntax.html

If you don't want this to be case sensitive, you need to state that.

pat = re.compile(r"\bTEMP\w*", re.IGNORECASE)

See http://docs.python.org/lib/node106.html

The easiest way to get all matches is to simply do

listOfMatches = pat.fetchall(myString)

But remember what Jamie Zawinski said:

    Some people, when confronted with a problem, think
    "I know, I'll use regular expressions."
    Now they have two problems.

You might actually want the fnmatch module rather than
the re module. See http://docs.python.org/lib/module-fnmatch.html

Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language 

More information about the Tutor mailing list