Solved

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

Posted on 2011-02-28
2
362 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
Comment Utility
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
Comment Utility
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

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

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
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 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…

763 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

10 Experts available now in Live!

Get 1:1 Help Now