Solved

RegEx Question in Python

Posted on 2004-08-11
4
272 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
any impressive Python code for a child? 3 127
Python script details 2 81
How to scan rdp  ''only'' open port 3333? 5 131
Difference of import usage in Python 1 75
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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…
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…

856 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