简体   繁体   English

python:使用正则表达式和findall搜索

[英]python: search with regex and findall

I have a very long string, which contains in addition to other characters even email addresses. 我的字符串很长,除了其他字符外,还包含电子邮件地址。

As that: 那样:

>>> s= “fedcantona datto, ernest william, pasquale <b>lops</b>, till plbaum, ...pasqale.  <b>email</b>: pasquale@gmail.com pagina web personale: http://.www. do. aggigi .il ...fanei ana tel: +34-54285, e-<b>mail</b>: fanli@gmail.com. .impedovo ... <b>lops</b> pale, tel: +9-54285, e-<b>mail</b>:  <b>lops</b>, g semo, p .bile ... b mehta, c niederee, a stewart, m demm”

I want to have as output the first email address of the string, the only thing I know is that all the email addresses ending with "@gmail.com". 我想将字符串的第一个电子邮件地址作为输出,我唯一知道的是所有以“ @ gmail.com”结尾的电子邮件地址。 I wrote this: 我这样写:

>>> print  re.findall("(%s)(@gmail.it)", s)[0]

But it does not work, what I did wrong? 但这不起作用,我做错了什么?

you can use the search() function to find only the first occurrence 您可以使用search()函数仅查找第一个匹配项

>>> rex=re.compile(r"\S+@gmail\.\w+")
>>> rex.search(s).group()
'pasquale@gmail.com'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM