regex - IIS URL 重写正则表达式使用选项列表定义所需的字符串块
问题描述
我正在尝试构建一个与以下内容匹配的正则表达式:
这是默认的英文网站 URL:https ://www.example.com/buy/apartment
这是默认的德国网站 URL:https ://www.example.com/de/kauf/appartement
我现在的表达方式:^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$
所以,我尝试过的细分:
- 有一个语言的可选块:
([a-z]{0,2})
用于参数值language
- 转义所需的正斜杠
\/
- 只能具有值 "buy" 或 "kauf": 的必需块,
(buy|kauf)
无需将此值用作参数,因此不确定是否必须使用括号 - 转义所需的正斜杠
\/
- 捕获家庭类型所需的块:
([a-zA-Z0-9-+]+)
用作参数值type
我的 IIS 重写规则:
<rule name="test rewrite">
<match url="^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$" />
<action type="Rewrite" url="homes.aspx?type={R:3}&language={R:1}" />
</rule>
我一直在尝试在这里测试它:https ://regex101.com/ 但我没有在这两个 URL 上得到匹配,我希望它们都匹配:https ://www.example.com/购买/公寓或https://www.example.com/de/kauf/appartement
不应匹配的 URL:https ://www.example.com/en/all-objects或https://www.example.com/contact
更新 1
我现在可以使用这个表达式:https:\/\/(.*)?\/([a-z]{0,2})\/?(buy|kauf)\/([a-zA-Z0-9-+]+)$
参见https://regex101.com/r/pieJWW/1
但是,我似乎无法将第二个 URL 中的语言参数“de”作为一个组获取。
更新 2
在 IIS web.config 中,我不使用https://www.example.com/
匹配的部分,而是使用之后的部分。因此,根据@Ryszard Czech 的回答,我现在有了这个,但我无法获取 lang 参数,无论是使用{R:1}
还是{R:2}
<rule name=""test rewrite">
<match url="^(([a-z]{0,2})\/)?(buy|kauf)\/([^\/]+)$" />
<action type="Rewrite" url="homes.aspx?type={R:4}&language={R:1}" />
</rule>
解决方案
利用
^https:\/\/([^\/]+)\/(?:([a-z]{1,2})\/)?(buy|kauf)\/([^\/]+)$
见证明
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
https: 'https:'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[a-z]{1,2} any character of: 'a' to 'z' (between
1 and 2 times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
buy 'buy'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
kauf 'kauf'
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
推荐阅读
- php - 使用PHP从数组中提取字符串中特定字符的两次计数之间的文本
- android - 无法使用从 React Native App 生成的 .aar 文件运行 Android 应用程序
- mysql - MySQL的“SHOW ENGINE INNODB STATUS”输出中的“TRANSACTION ACTIVE .. sec”是什么意思?
- batch-file - 如何在批处理/cmd文件中使用Admin privs运行程序(没有runas,如果需要密码则不允许psexec)?
- r - 如何使用 mutate 和 for 循环创建新列?
- sql - 计算两条记录之间的时间差,如果差值大于 30 秒,则插入另一个表
- arrays - 数组和衰减指针的区别
- c - 为什么我的 gcc 编译器在 return 0 后使用 break 时不会发出错误?(KNK 问题)
- npm - Homebrew 无法安装 Watchman:未找到类似名称的公式 | MacOS 大苏尔
- r - R向量计数每个日期范围内的日期数