RegEx用于匹配URL中的电子邮件

时间:2019-05-15 12:51:37

标签: python regex django django-urls regex-group

我的django代码中有一个正则表达式,但我不知道这实际上意味着什么。这是我的正则表达式:

r'^email/(?P<email>[^@\s]+@[^@\s]+\.[^@\s]+)/$',

您能给我一些与此正则表达式匹配的示例吗?

2 个答案:

答案 0 :(得分:2)

RegEx电路

您可以在jex.im中可视化您的表情:

enter image description here

您还可以在regex101.com中测试/修改/更改表达式。

基本上,您的表达式将匹配:

email/some_alphanumeric[A-Z0-9]_special_chars_@#$*some_alphanumeric_special_chars_#$*.some_alphanumeric_special_chars_#$*

Demo


如果您想匹配:

myurl/email/blabla@blabla.com

您可以modify it to

myurl\/email\/([^@\s]+@[^@\s]+\.[^@\s]+)

Python测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"myurl\/email\/([^@\s]+@[^@\s]+\.[^@\s]+)"

test_str = "myurl/email/blabla@blabla.com"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

答案 1 :(得分:0)

另外:

r'^email/(?P<email>[^@\s]+@[^@\s]+\.[^@\s]+)/$'

此regx在Django URL中的使用

网址示例:email/test@gmail.com /

email/ = consolent value in your url 

[^@\s] = you can write any character except @ and space "/s"
@[^@\s] = you must start with @ + anything expect @character and space "/s"
\. = matches "."
[^@\s] = you can write anycharacter except @ and space "/s"
+ = you can type many character

/$ = end of url