【问题标题】:Im trying to create a html regular expression but having issues with html bit我试图创建一个 html 正则表达式,但 html 位有问题
【发布时间】:2019-12-30 03:37:02
【问题描述】:

我创建了一个电子邮件和电话号码正则表达式匹配网页上的电话号码和电子邮件(没有添加粘贴代码,因为现在可以正常工作)

你能告诉我我在 url 正则表达式中做错了什么

UrlRegex = re.compile(r'''(
(https://.)
[a-zA-Z0-9]+
[.]
[a-zA-Z0-9]+
)''',re.VERBOSE)

mo2 = UrlRegex.search(https://.facebook.com)
print(mo2.group())

【问题讨论】:

  • 到底是什么问题?你有错误吗?结果错误?
  • 您在搜索字符串吗?当您致电 UrlRegex.search 时,您需要在 'https://.facebook.com) print(mo2.group()' 周围加上引号
  • 电话号码和电子邮件地址都不会以https://开头。
  • 嗨修复它。我没有将搜索正则表达式设为字符串。早上 5 点这样做的时候,它刚刚超出了我的想象,顺便说一句,代码中没有引用电话和电子邮件正则表达式,因为它们运行良好。

标签: python regex python-3.x


【解决方案1】:

其实,如果你想匹配'.',你应该使用'.'而是。


UrlRegex = re.compile(r'''(
(https://([a-zA-Z0-9]+
[\.]){0,1})
[a-zA-Z0-9]+
[\.]
[a-zA-Z0-9]+
)''',re.VERBOSE)

mo2 = UrlRegex.search('https://.facebook.com')
if mo2:
    print(mo2.group())
else:
    print("https://.facebook.com does not match!")
mo3 = UrlRegex.search('https://www.facebook.com')
print(mo3.group())
print(mo3.group(0),mo3.group(1),mo3.group(2))```

【讨论】:

  • 点不需要在字符类中转义,{0,1} 最好写成?
  • 啊,我知道我出错的地方是在 Url.Regex 搜索部分,我没有将搜索设为字符串。大声笑,对不起,伙计们,我做的这么晚了
猜你喜欢
  • 2015-11-25
  • 2018-09-05
  • 1970-01-01
  • 2017-06-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多