我知道我很蠢,但是我无法从这个电子邮件地址中提取域名:
'blahblah@gmail.com'
我的期望输出:
'@gmail.com'
我的当前输出:
.
这只是一个句号字符。
以下是我的代码:
import re
test_string = 'blahblah@gmail.com'
domain = re.search('@*?\.', test_string)
print domain.group()
这是我认为正则表达式 ('@*?.', 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 此外,我已经搜索过了,但其他答案对我来说有点难以理解。
一如既往地感谢您的帮助。 谢谢。
我的设备信息如下:
Windows 7 Pro (64 位) Python 2.6 (64 位)
附:StackOverflow问题:我的帖子不包括换行符,除非我在它们之间按两次“return”键。例如(当我发布时,这些都在不同的行上):
@-查找以at符号(“@”)开头的所有模式 * -查找和符号后面的所有字符 ?— 查找句点之前的最后一个字符 \ -打破(不使用下一个字符作为通配符,而是将其作为字符串字符) . -查找“。”字符 ,test string-在变量“test_string”上运行前面的搜索,即'blahblah@gmail.com'
这就是为什么我在每个上面的空行之间放了一个空行。 我做错了什么? 谢谢。