正则表达式白名单 url 让被阻止的 url 在消息的同一行上通过

问题描述 投票:0回答:1

所以我有一个正则表达式来阻止消息中的 URL,但我想将网站的 URL 列入白名单。

目前它适用于任何前缀,如 HTTP://www.example.com 和 www.example.com/support/how-do-i-setup-this 但如果我在这后面加上另一个 URL,它就会通过我不想要的过滤器(只有当我将新 URL 放在新行时,它才会根据需要被阻止)

“转到 http://example.com/support/how-do-i www.badurl.com”这不会阻止我想要发生的 badurl

此字符串也会导致两者都被阻止“www.badurl.comexample.com”,但理想情况下我也想在这里将 example.com URL 列入白名单

[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,24}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?(?<!\bexample.com(/.*)?)

当前python函数代码

import re

def link_remover(message):
   #remove any links that aren't in whitelist
   message = re.sub(r"[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,24}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?(?<!\bexample.com)", "[URL Removed]", message)
   return message

所以我只是想知道如何编辑它来修复这两个失败的例子?

感谢任何回复或为我指明正确的方向:)

python regex regex-lookarounds regexp-replace
1个回答
1
投票

更新:
这是一个可选匹配白名单项目的示例。
每个网址都匹配。

lambda 回调是检查白名单 url 中的any是否匹配所需要的。
如果是这样,请将它们原样写回。
如果没有匹配,则写回 Removed 字符串。

这不会解决嵌入的坏 url 的问题,不能像这样解决。

修改逻辑:
更改为只需要一个捕获组。
group 用作标志。

如果组是None,则没有找到匹配的白名单项。
在回调中返回

return {Empty}
并覆盖错误的url。

否则找到白名单项。匹配返回不变。

return m.group(0)
.

备注:
所有网址都匹配。单个捕获组。无限数量的白名单项目。
按照下面的模板添加白名单项目。

(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))((?<=example\.com)|(?<=example1\.com)|(?<=example2\.com))?))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?

https://regex101.com/r/11hL5d/1

Python代码示例:

import re

def ConvertURL_func(input_text):
  #
  def repl(m):
    if m.group(1) == None: return "{Removed}"
    return m.group(0)
  #
  input_text = re.sub(r"(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))((?<=example\.com)|(?<=example1\.com)|(?<=example2\.com))?))|localhost)(?::\d{2,5})?(?:\/[^\s]*)?",repl,input_text)
  return input_text

# input URL strings example:
input_text = '''
bad.com
example.com
www.badurl.com example2.com
www.badurl.com example1.com
'''

input_text = ConvertURL_func(input_text)
print(input_text)

输出:

>>> print(input_text)

{Removed}
example.com
{Removed} example2.com
{Removed} example1.com

>>>

正则表达式扩展:

 (?! mailto: )
 (?:
    (?: https? | ftp )
    :\/\/
 )?
 (?:
    \S+ 
    (?: : \S* )?
    @
 )?
 (?:
    (?:
       (?:
          [1-9] \d? 
        | 1 \d\d 
        | 2 [01] \d 
        | 22 [0-3] 
       )
       (?:
          \.
          (?: 1? \d{1,2} | 2 [0-4] \d | 25 [0-5] )
       ){2}
       (?:
          \.
          (?:
             [1-9] \d? 
           | 1 \d\d 
           | 2 [0-4] \d 
           | 25 [0-4] 
          )
       )
     | 
       (?:
          
          (?:
             (?: [a-z\u00a1-\uffff0-9]+ -? )*
             [a-z\u00a1-\uffff0-9]+ 
          )
          (?:
             \.
             (?: [a-z\u00a1-\uffff0-9]+ -? )*
             [a-z\u00a1-\uffff0-9]+ 
          )*
          (?:
             \.
             (?: [a-z\u00a1-\uffff]{2,} )
          )
          (                           # (1 start)
             # Whitelist Start
             
             (?<= example  \.com )
           | (?<= example1  \.com )
           | (?<= example2  \.com )
             
             # add more whitelist url's here
             
          )?                          # (1 end)
       )
    )
  | localhost
 )
 (?: : \d{2,5} )?
 (?: \/ [^\s]* )?
© www.soinside.com 2019 - 2024. All rights reserved.