Solved

Help with Regular Expressions

Posted on 2004-03-31
1
228 Views
Last Modified: 2010-04-16
Hi,

I have a string

        s ='''740446505        "O'HANLON, Benjamin"'''

i need to use ONE RE pattern to convert it into
 
       (740446505, "O'HANLN", "Benjamin"),
including the brackets and the comma at the end.

I am using python 2.3 and have been stuydin the various tutotrials and the
howto websites for the past 3 days and haven't managed to find a solution.

Any help to get to the solution will be very very helpful.

Thanks,

Kunal
0
Comment
Question by:warturtle
1 Comment
 
LVL 24

Accepted Solution

by:
fridom earned 20 total points
Comment Utility
def transform (str):
     pat = re.compile("([0-9]+)  *([^,]*), *(.*)")
    m = pat.search(str)
    return "(" + m.group(1) + "," + m.group(2) + "," + m.group(3) + "),"

transform (s) yields
'(740446505,"O\'HANLON,Benjamin),'

Regards
Friedrich
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 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…

762 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

5 Experts available now in Live!

Get 1:1 Help Now