使用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?我只是在检查可能性。