Solved

What is the best way to get multiple substrings from one string?

Posted on 2011-02-28
2
364 Views
Last Modified: 2012-05-11
Hi,
What is the best way to get multiple substrings from one string?
the text has replies using '@'. I want to get all the usernames,
substring of '@' and ' '
i.e.
Hello world @jax @toby @tom what is going on?

What is the best way to parse the string above to get a list of ['jax', 'toby', 'tom'] ?

Thanks
Jamie
0
Comment
Question by:jamie_lynn
2 Comments
 
LVL 9

Assisted Solution

by:TvMpt
TvMpt earned 100 total points
ID: 34995762
Hi.
Use the findall function with RegEx Patterns.

if you want an email for example its something like this

emails = re.findall('[a-zA-Z0-9+_\-\.]+@[0-9a-zA-Z][.-0-9a-zA-Z]*.[a-zA-Z]+', yourstring)

just adapt to your needs

0
 
LVL 28

Accepted Solution

by:
pepr earned 400 total points
ID: 34996540
Hi Jamie,

TvMpt is roughly right.  Probably the regular expression is the right answer here.  Try the following sample:

import re

rex = re.compile(r'@(\w+)\b')

msg = 'Hello world @jax @toby @tom what is going on?'

lst = rex.findall(msg)
print lst

Open in new window


Notice that the regular expression pattern literal must be prefixed by 'r' as 'raw string'.  Alternatively, you had to double the backslashes.

The regular expression can define a group (or more groups). If it defines a single group, the resulting list will contain only the groups.  The \w stands for the "word characters", the \b stands for "boundary between word and non-word characters".
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

776 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