正则表达式:仅当先前的组匹配时才捕获字符

I'm implementing a regular expression to match an absolute URI as defined in RFC 3986.

  • in the current state there is no capturing of path, query and fragment
  • additionaly to the specified authority there are groups capturing user, password and the 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

My current problem URI is the second one. This URI is invalid and should not be matched while the @ character should not be captured within the authority while there is no authentication provided in the URI.

所以我的问题是:

How can I exclude the @ character from the authority group if there is no authentication provided?

我敢肯定这很简单。但是我目前失去了注意力。