Solved

RegEx Question in Python

Posted on 2004-08-11
4
271 Views
Last Modified: 2010-04-16
Hi all,

I am beginning to dabble in some Python. I am trying to connect to a database so that I can build a table and then populate that table. Once I get this working then it will become a cron script. The purpose for this is to build citeria for searches against a database.

At any rate, here is my script so far:

                row = cursor.fetchone()
                if row==None:
                        break
                row = str(row)
                p = re.compile('[,()\']')
                row = p.sub('',row)
                p = re.compile('(PDB_[0-9])')

Now this is inside of a loop that is returning the names of databases from a query.  The database name takes the form of PDB_nnnnn(c), that's five numbers and an optional character. As you can see, I am trying to match on this pattern ( re.compile('(PDB_[0-9])') ) and this does work, except there are intermediate database files with stuff like _old, _tmp and such on the end of the PDB_nnnnn.

My question is how can I modify the re.compile('(PDB_[0-9])') to filter out those database names that have the _xxx on the end of them?

By the way, I am going against MySQL and to get all the databases I am issuing the query "SHOW DATABASES".


Thanks for your help

Paul
0
Comment
Question by:PaulS_III
  • 3
4 Comments
 
LVL 9

Expert Comment

by:rjkimble
ID: 11774806
To match the strings you describe, try this expression:

re.compile( '^PDB_\d{5}.?$' )

The ^ matches the start of the string, the \d matches any digit, the {5} modifies the \d to match exactly 5 digits, the .? matches any character 0 or 1 times, and the $ matches the end of the string.
0
 
LVL 9

Accepted Solution

by:
rjkimble earned 250 total points
ID: 11775969
I overlooked one thing in my earlier post. It's better to use raw strings to define regular expresson patterns. My expression should be this instead:

re.compile( r'^PDB_\d{5}.?$' )
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 11782588
rj,
Absolutely perfect. Thank you so much for your input.
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11784949
You're welcome -- glad to help.

.... Bob
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python script with password security? 15 105
Get IP addres information from field 3 54
Python - ImportError: No module named 'urllib2' 2 208
Modify a small python script 19 108
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question