Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with Regular Expressions

Posted on 2004-03-31
1
Medium Priority
?
281 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 80 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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…
Suggested Courses

916 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