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}&amp;language={R:1}" />
</rule>

我一直在这里进行测试:https://regex101.com/ 但是我在这两个URL上都没有匹配,我希望它们都匹配:https://www.example.com/buy/apartmenthttps://www.example.com/de/kauf/appartement

网址不匹配:https://www.example.com/en/all-objectshttps://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的答案,我现在有了这个,但是我无法使用{R:1}{R:2}

获取lang参数。
    <rule name=""test rewrite">
      <match url="^(([a-z]{0,2})\/)?(buy|kauf)\/([^\/]+)$" />
      <action type="Rewrite" url="homes.aspx?type={R:4}&amp;language={R:1}" />
    </rule>

解决方法

使用

fig,ax = plt.subplots()
ax.plot(dates,avg_temp,label='T avg',color='r')
ax.set_xlabel('date')
ax.set_ylabel('Average Temperature and Pressure for Date and Range')
ax2 = ax.twinx()
ax2.plot(dates,avg_pressure,label='P avg',color='b')
ax2.set_ylabel('Average Pressure,inHg')
plt.legend(loc="lower left")
plt.show()

请参见proof

说明

^https:\/\/([^\/]+)\/(?:([a-z]{1,2})\/)?(buy|kauf)\/([^\/]+)$