我们已经设置并运行了 Azure 应用程序网关。我们想要添加一条与名为 X 的 header 匹配以下条件的规则。
- 如果传入请求中根本不存在 X - 则让请求通过
- 如果 X 存在但为空或包含空格 - 则阻止请求
- 如果 X 存在但包含至少 1 个非空格字符 - 则让请求通过
我使用了以下模式 .+
(任意字符 0 多次) - 这仅满足要点 1,但无法满足要点 2(如果 header 为空,它不会阻止请求或有空格)和 3(如果 header 包含非空格字符,例如 p
,则请求将无法通过)。
我需要使用什么类型的正则表达式来满足所有三个条件? 我期望 .+
至少满足要点 1 和 3。
最佳答案
使用
^ *[^ ].*$
说明
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
* ' ' (0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
[^ ] any character except: ' '
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
关于regex - Azure 应用程序网关自定义 header 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68698331/