help with simple regular expression grouping with re

Skip Montanaro skip at mojam.com
Mon May 10 17:20:51 EDT 1999


Dan Schmidt wrote:
> 
> "Tim Peters" <tim_one at email.msn.com> writes:
> 
> | import re
> | pattern = re.compile(r"""
> |     "           # match an open quote
> |     (           # start a group so re.findall returns only this part
> |         [^"]*?  # match shortest run of non-quote characters
> |     )           # close the group
> |     "           # and match the close quote
> | """, re.VERBOSE)
> |
> | answer = re.findall(pattern, your_example)
> | for field in answer:
> |     print field

One thing to be careful of (I think) is that comma separated values
formats tend to quote " characters by doubling them.  Consequently, you
might see input like:

  "W. Shakespeare","""To be or not to be..."""

This can wreak havoc with re matching...

My caveat is that I've never actually seen a spec for CSV files.  That's
my experience, however.

-- 
Skip Montanaro	| Mojam: "Uniting the World of Music"
http://www.mojam.com/
skip at mojam.com  | Musi-Cal: http://www.musi-cal.com/
518-372-5583




More information about the Python-list mailing list