提取字符串

2024-06-08 17:06:43 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我只想从email id中提取alpitananad。你知道吗

import re

def extracter(text):
        reg1 = re.compile(r'(\d{3}|\(\d{3}\))?(\s|-|\.)?(\d{3})(\s|-|\.)(\d{4})')
        se1 = reg1.findall(text)
        print(se1)
        for i in range(len(se1)):
               print(''.join(se1[i]))
        reg2 = re.compile(r'([a-zA-Z0-9]+@+[a-zA-Z0-9]+\.[a-zA-Z]{2,4})')
        reg3 = re.compile(r'[a-zA-Z0-9]+@')
        se3 = reg3.findall(text)
        se2 = reg2.findall(text)
        print(se2)
        print(se3)

text1 = "123-434-2432 of may name is jsdiofhsdio fh diofh dui fjf ui834y8  fwe8fweuihwe 8f e87f y87 456-243-3434 ajsfhj alpitanand20@gamil.com"
extracter(text1)

我得到的结果是

[('123', '-', '434', '-', '2432'), ('456', '-', '243', '-', '3434')]
123-434-2432
456-243-3434
['alpitanand20@gamil.com']
['alpitanand20@']

我只需要一个输出来检查前一个字符串@。我应该在reg3中做什么更改。谢谢您。。!!!你知道吗


Tags: textreprintcompilese3zaz0findall
1条回答
网友
1楼 · 发布于 2024-06-08 17:06:43

以下应起作用:

re.findall('(\w+)@', text1)

输出:

>>> import re
>>> 
>>> text1 = "123-434-2432 of may name is jsdiofhsdio fh diofh dui fjf ui834y8  fwe8fweuihwe 8f e87f y87 456-243-3434 ajsfhj alpitanand20@gamil.com"
>>> reg3 = re.compile(r'(\w+)@')
>>> se3 = reg3.findall(text1)
>>> se3
['alpitanand20']

相关问题 更多 >