ios - 用于匹配带有小写和破折号的特定 URL 的正则表达式
问题描述
每天早上,我都会访问这个名为 mtonews.com 的网站,并尝试使用 RegEx 构建一个 iOS 快捷方式来打开网站上的所有新闻链接。
该网站有一堆链接:
https://mtonews.com/rihanna-teams-up-with-lvmh-for-fashion-brand
https://mtonews.com/ciara-goes-naked-for-new-album-release
https://www.btserve.com/serve?t=bidt-sra&v=1&pubId=168&siteId=512&placementUid=5ae8e4105e-168%7C5&pgid=78ff2e45-8b3c-6a06-465f-2ac1a107f4f6&o=https://mtonews.com/&
https://mtonews.com/.image/t_share/MTYzOTYyODY2ODAwNTM1Mzc3/steve_marjorie.png
我希望 RegEx 打开与前两个类似的所有链接。
这是我到目前为止所拥有的:
^(?!image$|btserve$).*mtonews.com.*$
解决方案
此工具可能会帮助您根据需要设计表达式。捕获组是正则表达式最简单的功能,您可以逐步将所需的输出与它们绑定。例如,
^((https?.*)(mtonews.com\/)([A-Za-z0-9-]+))$
有四个捕获组,一个用于协议,一个用于域,一个用于前导 URL,第一个将所有这三个组都包含在其中,并且可以简单地由$1
.
正则表达式描述图
该图可视化了它的工作原理,您可能希望在此链接中测试其他表达式:
基本性能测试
此 JavaScript 片段返回 100 万次for
循环的运行时以提高性能。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = 'https://mtonews.com/rihanna-teams-up-with-lvmh-for-fashion-brand';
const regex = /^((https?.*)(mtonews.com\/)([A-Za-z0-9-]+))$/gm;
var match = string.replace(regex, "\nGroup #1: $1\nGroup #2: $2 \nGroup #3: $3 \nGroup #4: $4 \n");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
您可以简单地修改和简化这个表达式。
推荐阅读
- sql - 确定样本是否包含多个 ID 的 0 行
- sql - 在 INSERT 中,如何使空值为零或空?
- vue.js - 访问 Label 的 nativeView
- python - 如何在 Reg Ex 中进一步缩小搜索条件?
- azure-devops - 如何从 ARM 模板使用 WinRM 创建虚拟机?
- flutter - 颤振 | Dart:URI的目标不存在
- sql - SQL:在 where 子句中调用时的情况
- spring - Jackson ObjectMapper 作为单例类中的静态字段的性能如何?
- laravel - 只有一个人在深层次的关系中工作
- c# - 从字节数组启动 C# 控制台应用程序