Solved

Help with Regular Expressions

Posted on 2004-03-31
1
247 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
ID: 10746722
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visualizing tuple values in Python (or generally) 3 74
Python Assistance 7 80
how to split multiple lines delimiter : 8 99
Difference between List and Array in Python 5 65
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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…

772 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