?
Solved

RegEx Question in Python

Posted on 2004-08-11
4
Medium Priority
?
276 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 1000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
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…
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…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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