Solved

RegEx Question in Python

Posted on 2004-08-11
4
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

691 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