views:

794

answers:

4

Lets say we have this string: [18] [email protected]:pwd:

[email protected] is the email and pwd is the password.

Also, lets say we have this variable with a value

f = "[18] [email protected]:pwd:"

I would like to know if there is a way to make two other variables named var1 and var2, where the var1 variable will take the exact email info from variable f and var2 the exact password info from var2.

The result after running the app should be like:

var1 = "[email protected]"

and

var2 = "pwd"
+10  A: 
>>> var1, var2, _ = "[18] [email protected]:pwd:"[5:].split(":")
>>> var1, var2
('[email protected]', 'pwd')

Or if the "[18]" is not a fixed prefix:

>>> var1, var2, _ = "[18] [email protected]:pwd:".split("] ")[1].split(":")
>>> var1, var2
('[email protected]', 'pwd')
Aaron Maenpaa
Nice, I was going to suggest two splits. What's the [4:] syntax called?
Bill the Lizard
@Bill Slice syntax.
Aaron Maenpaa
Thanks. After Googling that term it seems like it's rather fundamental in Python. I guess I need to add "Learning Python" to my stack of books to read.
Bill the Lizard
+7  A: 
import re
var1, var2 = re.findall(r'\s(.*?):(.*):', f)[0]

If findall()[0] feels like two steps forward and one back:

var1, var2 = re.search(r'\s(.*?):(.*):', f).groups()
PEZ
+1. A good, general solution.
Brian Clapper
May also want to add in match checking. For instance, if the re.search returns None, then you'll get a nice little error for trying the groups() method on a NoneType object. All in all, a good solution though PEZ. +1
Evan Fosmark
`re.search(r'\s([^:\s]+):([^:\n]*):')`
J.F. Sebastian
Thanks, Evan. I think also the findall() needs a check for length before you try to index it if the data isn't "cleaned" beforehand.
PEZ
@J.F. It depends on the application and the data. You're expression will extract "mail.com" as the address if the string is "[18] email@e mail.com:pwd:". It might, and might not, be what's requested. We don't know. I prefer to make weaker assumptions on the data and stronger on the format.
PEZ
Second `.*` in your regexp will eat all but last ':'. It certainly doesn't respect format. I've never encountered cases where spaces in email were accepted. It is reasonable to assume that newline is not in password.
J.F. Sebastian
What's to say there are only valid e-mail addresses or passwords in the data?
PEZ
+5  A: 
var1, var2 = re.split(r'[ :]', f)[1:3]
PEZ
+1  A: 

To split on the first colon ":", you can do:

# keep all after last space
f1= f.rpartition(" ")[2]
var1, _, var2= f1.partition(":")
ΤΖΩΤΖΙΟΥ
+1 for rpartition. I didn't know about it.
PEZ