?
Solved

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

Posted on 2011-02-28
2
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Assisted Solution

by:TvMpt
TvMpt earned 400 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 29

Accepted Solution

by:
pepr earned 1600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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 …
Suggested Courses

777 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