【发布时间】:2020-05-23 22:33:10
【问题描述】:
我正在实现一个正则表达式来匹配RFC 3986 中定义的绝对URI。
- 在当前状态下,没有捕获路径、查询和片段
- 除了指定的
authority之外,还有捕获user、password和authless authority的组
实际上我能够捕获以下 URI。
https://regex101.com/r/aXYPbl/2
RegEx
~^(?<scheme>.+?):(?://(?<authority>(?:(?<user>.+?)(?::(?<password>.+?))?@)+(?<authlessAuthority>.+)?))?$~
---
Examples
https://example.com
https://@example.com
https://user@example.com
https://user:password@example.com
我当前的问题 URI 是第二个。此 URI 无效且不应匹配,而在 URI 中未提供身份验证时不应在权限内捕获 @ 字符。
所以我的问题是:
如果没有提供身份验证,如何从权限组中排除@ 字符?
我相信这很简单。但我现在失去了注意力。
【问题讨论】:
-
在后面添加负面外观:
(?:(?<!/)@)。另外我认为凭据后的+允许多组凭据。我想你想要?
标签: regex