getUserInfo and string "https://@"

Good evening (:
When looking at the grammar of URIs in Uri.java you can have userinfo as an empty string and still have the @… so “https://@” should be a valid URI and therefore getUserInfo() should return “”?

for reference the grammar im talking about:
image

Thanks in advance!

1 Like

Yes, the @ would be matched. (I learned this the hard way by having my daily tests fail due to my tests rejecting this very thing you describe as an invalid URI)

If you are unsure about such things you can also try entering parts of the grammar as a regex into an interactive regex website, such as this:

So in your example you could enter the (a*@){0,1} as regex and @ as test string and you would get a match.

Thanks for the quick answer and the website, might be useful in the next few days! :grin:

Regarding RegEx: The project presentation is also helpful for an introduction to regular expressions in Java.
In RegEx there is a shorthand notation ? for one optional occurrence (compared to the counting notation zero or one occurrence that is not always supported):
(regex)?

Do, does this mean it will return an empty String? But isn’t userinfo separated from the "@" symbol? So, in my understanding and looking at this simplification, it should return null, since no user info is passed, only the @-symbol.

The userinfo is the substring bound by the userinfo category in the grammar:

 authority     = [ userinfo "@" ] host
 userinfo      = *( pchar / ":" )

It appears optionally in authority before an @ and might be empty.

1 Like

Note that an empty string and null (i.e. the absence of a non-terminal) are not the same thing.

URIs always contain scheme, host and path. There are URIs you can deduce from the grammar without ever using the rule for userinfo, for example a://b. Thus getUserInfo should return null. For the URI a://@b, you need to expand the userinfo non-termial to an empty string, thus getUserInfo should return an empty string.

2 Likes

Shouldn’t http://c@b return c as userinfo?

Yes, whoops, fixed.

1 Like