Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python string.find()函数在字符串中向后遍历_Python_Python 2.7 - Fatal编程技术网

使用python string.find()函数在字符串中向后遍历

使用python string.find()函数在字符串中向后遍历,python,python-2.7,Python,Python 2.7,我有一根绳子 mystr = "My mail id is abcd1234@account.com and xzy1234@mailinglist.com" index = mymail.find('@') #gives me index using which i can traverse further with the mail id 如何只获取@之前的单词(即abcd1234和xzy1234)和@之后的单词(即account.com和mailinglist.com),而不使用list

我有一根绳子

mystr = "My mail id is abcd1234@account.com and xzy1234@mailinglist.com"
index = mymail.find('@') #gives me index using which i can traverse further with the mail id
如何只获取@之前的单词(即abcd1234和xzy1234)和@之后的单词(即account.com和mailinglist.com),而不使用list和find

例如:

index = mymail.find('@')
res = mymail.find(' ',index)
mystr[index+1:res] gives me --> account.com

您可以使用下面的
re.findall
函数

>>> s = 'My mail id is abcd1234@account.com and xzy1234@mailinglist.com'
要获取
@
之前的单词

>>> re.findall(r'\S+(?=@)', s)
['abcd1234', 'xzy1234']
>>> re.findall(r'(?<=@)\S+', s)
['account.com', 'mailinglist.com']
要获取
@
后面的单词

>>> re.findall(r'\S+(?=@)', s)
['abcd1234', 'xzy1234']
>>> re.findall(r'(?<=@)\S+', s)
['account.com', 'mailinglist.com']

>>关于findall(r'(?要直接回答您的问题:

S.rfind(sub [,start [,end]]) -> int

rfind查找字符串中的“最后一个”空格,而不是第一个。

谢谢Avinash,但是有没有其他方法可以回答这个问题而不必重新使用find?我只是在检查可能性。