regular expression questions in Python

J

Jeremy

I am (very) new top regular expressions and I am having a difficult time
understanding how to do them. I have the following in my script:

zaidsearch = r'''^ {5,}([\d]{4,5})(.\d{2,2}c)'''
ZAIDSearch = re.compile(search, re.IGNORECASE)

When I do: ZAID.search(...) then this works fine. I would like to write
it as:

zaidsearch = r'''^ {5,}([\d]{4,5}) #My comments
(.\d{2,2}c) #More of my comments'''
ZAIDSearch = re.compile(zaidsearch, re.VERBOSE)

but this doesn't work. I get the following error:

raise error, v # invalid expression
sre_constants.error: nothing to repeat


So I guess my question is: how do I use the VERBOSE option to make my
regular expression easier to understand for a human? Secondly, how can
I use both the VERBOSE and IGNORECASE options?
Thanks,
Jeremy
 
R

Reinhold Birkenfeld

Jeremy said:
I am (very) new top regular expressions and I am having a difficult time
understanding how to do them. I have the following in my script:

zaidsearch = r'''^ {5,}([\d]{4,5})(.\d{2,2}c)'''
ZAIDSearch = re.compile(search, re.IGNORECASE)

When I do: ZAID.search(...) then this works fine. I would like to write
it as:

zaidsearch = r'''^ {5,}([\d]{4,5}) #My comments
^ This space will not be a space in the RE.
You have to specify a space with '[ ]' or '\ '
when using re.VERBOSE
(.\d{2,2}c) #More of my comments'''
ZAIDSearch = re.compile(zaidsearch, re.VERBOSE)

but this doesn't work. I get the following error:

raise error, v # invalid expression
sre_constants.error: nothing to repeat


Reinhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top