【问题标题】:Regex re.findall can only extract half of data正则表达式 re.findall 只能提取一半数据
【发布时间】:2019-05-07 03:48:56
【问题描述】:

我使用正则表达式从 str 中查找所有电子邮件地址,但是,有时电子邮件地址被剪切并且仅返回一个地址的一部分。

import re

regex=r'(\w{1,}((\.|_|-|\w)[\w]){0,}@\w{1,}((\.|_|-|\w)[\w]){0,}\.\w{1,})'

str2fetch='''


wwwr@h.com.h.ki.l =》1  #==》wwwr@h.com.h.ki

sdfsd

2@mail2.4.sdu.edu.cn.u.163.com #=>2@mail2.4.sdu.edu.cn

0@0.0
1@1.1.1
1@123434.22222.333.4444.com
AAAAAA2@p.2-t.2.3o.2.abcd4 #=>aaaaaa2@p.2-t.2.3o

AAAAAA2@p.2t.2.3o.2.abcd4   #=>aaaaaa2@p.2t

AAAAAA2@p.2-t.2p.3o.2.abcd4  #=>aaaaaa2@p.2-t.2p

DAAAAAA2@p.2p-t.2.3o.2.abcd4  #=>daaaaaa2@p.2p

3@3.3.3.3.3
4@4.4.4.4.4.4
'''
emailList=list(set(re.findall(regex,str2fetch.lower())))

print(emailList)

左边部分是假设的结果,但是给出了正确的结果。

wwwr@h.com.h.ki.l =》1  #==》wwwr@h.com.h.ki

AAAAAA2@p.2-t.2.3o.2.abcd4 #=>aaaaaa2@p.2-t.2.3o

AAAAAA2@p.2t.2.3o.2.abcd4   #=>aaaaaa2@p.2t

AAAAAA2@p.2-t.2p.3o.2.abcd4  #=>aaaaaa2@p.2-t.2p

DAAAAAA2@p.2p-t.2.3o.2.abcd4  #=>daaaaaa2@p.2p

【问题讨论】:

  • 正则表达式 = r'(\w{1,}((\.|_|-|\w)[\w]){0,}@\w{1,}((\ .|_|-|\w)[\w]){0,}\.\w{1,})' a='\w{1,}' #任意长度>1的数字或字母 b=' ((\.|_|-|\w)[\w]){0,}' #started by dot|_|-|any digital/alphabet str, +any digital/alphabet str c='@' d= '\w{1,}' #任意长度>1的数字或字母 e='((\.|_|-|\w)[\w]){0,}' #以点开头|_|- |any digital/alphabet str, +any digital/alphabet str f='\.\w{1,}' #dot+任意长度>1的数字或字母

标签: regex python-3.x findall


【解决方案1】:
regex = r'(\w{1,}((\.|_|-|\w)[\w]){0,}@\w{1,}((\.|_|-|\w)[\w]){0,}\.\w{1,})'
    a='\w{1,}'  #any digital or alphabet with length >1
    b='((\.|_|-|\w)[\w]){0,}'    #started by dot|_|-|any digital/alphabet str, +any digital/alphabet str
    c='@'
    d='\w{1,}'  #any digital or alphabet with length >1
    e='((\.|_|-|\w)[\w]){0,}'  #started by dot|_|-|any digital/alphabet str, +any digital/alphabet str
    f='\.\w{1,}' #dot+any digital or alphabet with length >1 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2021-09-30
    • 2019-05-13
    • 2018-04-16
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多