RegEx匹配不属于我的域

时间:2018-05-31 22:40:10

标签: regex pcre netscaler

我正在尝试使用重写策略设置我的Netscaler设备。我的一个要求是用主页URL替换任何非域URL ...也就是说,我希望Netscaler用主页的URL替换设备后面的所有外部链接(例如:{ {3}})。我正在尝试配置的重写策略类型使用符合PCRE的正则表达式引擎来查找网页上的特定文本(可能多个匹配)。

良好的链接:

https://your.page.domain.edu -- won't be replaced  
http://good.domain.edu  -- also won't be replaced

错误链接(应替换为主页网址):

https://www.google.com    
http://not.the.best.example.org   
http://another.bad.example.erewhon.edu   
https://my.domain.com    

我目前有这种模式:

(https?://)(?![\w.-]+\.domain\.edu)

根据Netscaler的RegEx评估工具,这与上面的错误链接匹配,并且与良好的链接不匹配,所以它似乎正在工作......事实上,当我在测试页面上运行时,Netscaler找到所有我想要替换的URL并留下好的URL。

问题是Netscaler没有按照我想要的方式替换URL:它用主页URL替换(https?://)组,但留下了坏URL的剩余部分。例如,它将https://my.domain.edu替换为:http://www.google.com

我可以配置重写策略来替换特定的URL(例如,https://my.domain.eduwww.google.com),因此我知道该机制有效。显然,这不适用于一般情况。

我已经尝试将整个正则表达式括在括号中,但这并没有改变任何东西。

是否可以针对一般情况编写正则表达式,以匹配不属于我的所有域的整个URL?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您可以使用以下正则表达式:

^https?:\/\/[\w.-]+(?<!\.domain\.edu)$

将您的主页网址替换为:

https://my.domain.edu

TEST INPUT:

https://www.google.com
http://not.the.best.example.org
http://another.bad.example.erewhon.edu
https://my.domain.com
https://your.page.domain.edu
http://good.domain.edu

TEST OUTPUT:

https://my.domain.edu
https://my.domain.edu
https://my.domain.edu
https://my.domain.edu
https://your.page.domain.edu
http://good.domain.edu

Demo on regex101

如果http/https比使用以下正则表达式更重要:

^(https?:\/\/)[\w.-]+(?<!\.domain\.edu)$

替换:

\1my.domain.edu

<强> INPUT:

https://www.google.com
http://not.the.best.example.org
http://another.bad.example.erewhon.edu
https://my.domain.com
https://your.page.domain.edu
http://good.domain.edu

<强>输出:

https://my.domain.edu
http://my.domain.edu
http://my.domain.edu
https://my.domain.edu
https://your.page.domain.edu
http://good.domain.edu

Demo2

答案 1 :(得分:0)

查看原始的http有效负载,并确保链接如您所愿,它们位于实际有效负载中。

主机名通常是一个http标头,协议常不包含在页面内容中。等等。安装提琴手并观察原始数据。

Netscaler RegEx可以正常工作。

进一步:在尝试重写任何压缩内容之前,请确保对其压缩。如果不是,netscaler将尝试使您的重写与压缩数据/分块内容匹配。

相关问题