[英]python: search with regex and findall
我的字符串很長,除了其他字符外,還包含電子郵件地址。
那樣:
>>> 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”
我想將字符串的第一個電子郵件地址作為輸出,我唯一知道的是所有以“ @ gmail.com”結尾的電子郵件地址。 我這樣寫:
>>> print re.findall("(%s)(@gmail.it)", s)[0]
但這不起作用,我做錯了什么?
您可以使用search()函數僅查找第一個匹配項
>>> rex=re.compile(r"\S+@gmail\.\w+")
>>> rex.search(s).group()
'pasquale@gmail.com'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.