Solved

RegEx Question in Python

Posted on 2004-08-11
4
267 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
Comment Utility
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
Comment Utility
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
Comment Utility
rj,
Absolutely perfect. Thank you so much for your input.
0
 
LVL 9

Expert Comment

by:rjkimble
Comment Utility
You're welcome -- glad to help.

.... Bob
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now