Solved

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

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
any impressive Python code for a child? 3 137
Macports Import beautifulsoup4 Problem 6 84
Difference of import usage in Python 1 84
python error - set 2 40
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…
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 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…

733 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