【问题标题】:Don't get the url contains: "togl" [Regex]不要让网址包含:“togl”[正则表达式]
【发布时间】:2011-08-31 15:44:52
【问题描述】:

我有一个很好的 URL 捕捉正则表达式,但我有一个问题.. 我不想从 are togl.me 捕捉 url...我的正则表达式是:

(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))

这是正则表达式模式:

(?xi)
\b
(                       # Capture 1: entire matched URL
  (?:
    https?://               # http or https protocol
    |                       #   or
    www\d{0,3}[.]           # "www.", "www1.", "www2." … "www999."
    |                           #   or
    [a-z0-9.\-]+[.][a-z]{2,4}/  # looks like domain name followed by a slash
  )
  (?:                       # One or more:
    [^\s()<>]+                  # Run of non-space, non-()<>
    |                           #   or
    \(([^\s()<>]+|(\([^\s()<>]+\)))*\)  # balanced parens, up to 2 levels
  )+
  (?:                       # End with:
    \(([^\s()<>]+|(\([^\s()<>]+\)))*\)  # balanced parens, up to 2 levels
    |                               #   or
    [^\s`!()\[\]{};:'".,<>?«»“”‘’]        # not a space or one of these punct chars
  )
)

不要捕获来自 http://togl.me 的 URL。我可以在捕获 URL 后使用 parse_url 来检查域名,但为什么需要呢?

【问题讨论】:

  • parse_url() 有什么问题?
  • NullUserException,时隔近三年,我看到你的回复完全正确。谢谢,

标签: php regex url


【解决方案1】:

匹配域后,您可以回头检查它是否不是togl.me

[a-z0-9.\-]+[.][a-z]{2,4}(?<!/togl\.me)/

编辑:因为域可以在其他地方匹配,而不是 cmets 说的,让我们移动检查 togl.me

…
    [a-z0-9.\-]+[.][a-z]{2,4}/  # looks like domain name followed by a slash
  )
  (?<!togl\.me/) 
  (?!togl\.me)
  (?:                       # One or more:
    [^\s()<>]+
…

更多帮助:http://www.regular-expressions.info/lookaround.html

【讨论】:

  • 啊,你的正则表达式糟透了! togl.me url 仍然与 http://+non-space 部分匹配。
  • 我还有一个问题,这个正则表达式不适用于 JavaScript/客户端。你能再帮我解决这个问题吗?
  • @Oral ÜNAL,不。提出一个新问题并用适当的编程语言标签对其进行标记。
猜你喜欢
  • 2015-12-23
  • 2023-01-23
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
相关资源
最近更新 更多