Solved

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

Posted on 2011-02-28
2
363 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create exe using python? 2 46
Merge XML files using a unique key? 8 90
Python On Mouseover Save URL/Link 4 59
How to use pyenv 4 15
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…
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 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 …
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now