Python正则表达式,在地址中查找电子邮件域 [英] Python Regular Expressions, find Email Domain in Address

查看:219
本文介绍了Python正则表达式,在地址中查找电子邮件域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我是个白痴,但我无法从这个电子邮件地址中拉出这个域名:

I know I'm an idiot, but I can't pull the domain out of this email address:

'blahblah@gmail.com'

我想要的输出:

'@gmail.com'

我目前的输出:

.

(只是一个句点字符)

以下是我的代码:

import re
test_string = 'blahblah@gmail.com'
domain = re.search('@*?\.', test_string)
print domain.group()

这是我认为我的正则表达式('@ * ?.',test_string):

Here's what I think my regular expression says ('@*?.', test_string):

 ' # begin to define the pattern I'm looking for (also tell python this is a string)

  @ # find all patterns beginning with the at symbol ("@")

  * # find all characters after ampersand

  ? # find the last character before the period

  \ # breakout (don't use the next character as a wild card, us it is a string character)

  . # find the "." character

  ' # end definition of the pattern I'm looking for (also tell python this is a string)

  , test string # run the preceding search on the variable "test_string," i.e., 'blahblah@gmail.com'

我基于这里的定义:

http:/ /docs.activestate.com/komodo/4.4/regex-intro.html

此外,我搜索,但其他答案对我来说有点太难了让我的头脑。

Also, I searched but other answers were a bit too difficult for me to get my head around.

帮助是非常感谢,像往常一样。谢谢。

Help is much appreciated, as usual. Thanks.

我的东西如果重要:


Windows 7 Pro bit)

Windows 7 Pro (64 bit)

Python 2.6(64位)

Python 2.6 (64 bit)






PS。 StackOverflow quesiton:我的帖子不包括新行,除非我们之间两次返回。例如(当我发布时,这些都在不同的行):


PS. StackOverflow quesiton: My posts don't include new lines unless I hit "return" twice in between them. For example (these are all on a different line when I'm posting):

@ - 找到以at符号(@)开头的所有模式
* - 在&符号
之后找到所有字符? - 找到期限之前的最后一个字符
\ - breakout(不要使用下一个字符作为通配符,我们是一个字符串字符)
。 - 找出 。字符
,测试字符串 - 在变量test_string上运行前面的搜索,即'blahblah@gmail.com'

@ - find all patterns beginning with the at symbol ("@") * - find all characters after ampersand ? - find the last character before the period \ - breakout (don't use the next character as a wild card, us it is a string character) . - find the "." character , test string - run the preceding search on the variable "test_string," i.e., 'blahblah@gmail.com'

这就是为什么我有一个空白线b / w每行上面。我究竟做错了什么? Thx。

That's why I got a blank line b/w every line above. What am I doing wrong? Thx.

推荐答案

这是我认为可能有帮助的东西

Here's something I think might help

import re
s = 'My name is Conrad, and blahblah@gmail.com is my email.'
domain = re.search("@[\w.]+", s)
print domain.group()

输出

@gmail.com

正则表达式的工作原理:

How the regex works:

@ - 扫描直到你看到这个角色

@ - scan till you see this character

[\w。] 一组可能匹配的字符,所以 \w 是所有字母数字字符,尾随时段添加到该组字符。

[\w.] a set of characters to potentially match, so \w is all alphanumeric characters, and the trailing period . adds to that set of characters.

+ 前一集中的一个或多个。

+ one or more of the previous set.

因为这个正则表达式与<$ c $之后的句点字符和每个字母数字匹配c> @ ,即使在句子中间也会匹配电子邮件域。

Because this regex is matching the period character and every alphanumeric after an @, it'll match email domains even in the middle of sentences.

这篇关于Python正则表达式,在地址中查找电子邮件域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆