George
George

Reputation: 75

Regex for valid URI Java

I have the following task: I must make a regex for valid URI in these syntax: scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment] . And the symbols ":","/", "?","#","[]", "@" must be saved (maybe it means constants) and if they are used like a part of some component(path,query,fragment) they must be percent-encoded (instead question mark must be - %3F).

Could you please help me about that regex. Here is mine for now, but i'm not sure is it correct:

([.//ftp]+[://a-z.]+[@.a-z?0-9:]+[://a-zA-Z0-9]+[&?a-z=#0-9]+)

Here are some valid examples:

ftp://myusername:mypassword@localhost:21/somepath/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost:21/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost:21?key=value#somefragment
ftp://myusername:mypassword@localhost:21#somefragment
ftp://myusername:mypassword@localhost:21

ftp://myusername:mypassword@localhost/somepath/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost?key=value#somefragment
ftp://myusername:mypassword@localhost#somefragment
ftp://myusername:mypassword@localhost

ftp://myusername@localhost/somepath/somepath?key=value#somefragment
ftp://myusername@localhost/somepath?key=value#somefragment
ftp://myusername@localhost?key=value#somefragment
ftp://myusername@localhost#somefragment
ftp://myusername@localhost

ftp://localhost/somepath/somepath?key=value#somefragment
ftp://localhost/somepath?key=value#somefragment
ftp://localhost?key=value#somefragment
ftp://localhost#somefragment
ftp://localhost


ftp://myusername:mypassword@localhost:21/somepath/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost:21/somepath?key=value&abc=xyz#somefragment
ftp://myusername:mypassword@localhost:21?key=value&abc=xyz#somefragment

ftp://myusername:mypassword@localhost/somepath/somepath?key=value&abc=xyz#somefragment
ftp://myusername:mypassword@localhost/somepath?key=value&abc=xyz#somefragment
ftp://myusername:mypassword@localhost?key=value&abc=xyz#somefragment

ftp://myusername@localhost/somepath/somepath?key=value&abc=xyz#somefragment
ftp://myusername@localhost/somepath?key=value&abc=xyz#somefragment
ftp://myusername@localhost?key=value&abc=xyz#somefragment

ftp://localhost/somepath/somepath?key=value&abc=xyz#somefragment
ftp://localhost/somepath?key=value&abc=xyz#somefragment
ftp://localhost?key=value&abc=xyz#somefragment

../../../resource.txt

//myusername:mypassword@localhost:21/somepath/somepath?key=value#somefragment
//myusername:mypassword@localhost:21/somepath?key=value#somefragment
//myusername:mypassword@localhost:21?key=value#somefragment
//myusername:mypassword@localhost:21#somefragment
//myusername:mypassword@localhost:21

//myusername:mypassword@localhost/somepath/somepath?key=value#somefragment
//myusername:mypassword@localhost/somepath?key=value#somefragment
//myusername:mypassword@localhost?key=value#somefragment
//myusername:mypassword@localhost#somefragment
//myusername:mypassword@localhost

//myusername@localhost/somepath/somepath?key=value#somefragment
//myusername@localhost/somepath?key=value#somefragment
//myusername@localhost?key=value#somefragment
//myusername@localhost#somefragment
//myusername@localhost

//localhost/somepath/somepath?key=value#somefragment
//localhost/somepath?key=value#somefragment
//localhost?key=value#somefragment
//localhost#somefragment
//localhost


//myusername:mypassword@localhost:21/somepath/somepath?key=value#somefragment
//myusername:mypassword@localhost:21/somepath?key=value&abc=xyz#somefragment
//myusername:mypassword@localhost:21?key=value&abc=xyz#somefragment

//myusername:mypassword@localhost/somepath/somepath?key=value&abc=xyz#somefragment
//myusername:mypassword@localhost/somepath?key=value&abc=xyz#somefragment
//myusername:mypassword@localhost?key=value&abc=xyz#somefragment

//myusername@localhost/somepath/somepath?key=value&abc=xyz#somefragment
//myusername@localhost/somepath?key=value&abc=xyz#somefragment
//myusername@localhost?key=value&abc=xyz#somefragment

//localhost/somepath/somepath?key=value&abc=xyz#somefragment
//localhost/somepath?key=value&abc=xyz#somefragment
//localhost?key=value&abc=xyz#somefragment



ftp://myusername:[email protected]:21/somepath/somepath?key=value#somefragment
ftp://myusername:[email protected]:21/somepath?key=value#somefragment
ftp://myusername:[email protected]:21?key=value#somefragment
ftp://myusername:[email protected]:21#somefragment
ftp://myusername:[email protected]:21

ftp://myusername:[email protected]/somepath/somepath?key=value#somefragment
ftp://myusername:[email protected]/somepath?key=value#somefragment
ftp://myusername:[email protected]?key=value#somefragment
ftp://myusername:[email protected]#somefragment
ftp://myusername:[email protected]

ftp://[email protected]/somepath/somepath?key=value#somefragment
ftp://[email protected]/somepath?key=value#somefragment
ftp://[email protected]?key=value#somefragment
ftp://[email protected]#somefragment
ftp://[email protected]

ftp://localhost.com/somepath/somepath?key=value#somefragment
ftp://localhost.com/somepath?key=value#somefragment
ftp://localhost.com?key=value#somefragment

Upvotes: 1

Views: 238

Answers (1)

I've tried to write roughly its matching RegEx. It seems working but of course there may be lacks! I'm not a RegEx guru.

[ftp:]*\/\/([a-z0-9]+[\:]*[a-z0-9]+[a-z0-9]*)[@]*([\\.a-z0-9]+)([0-9a-zAz:]+)[\/?#]*([\\.\/a-z0-9]+)?([?=]([^#]*))?[#a-zA-Z]*

Demo (without ../../../resource.txt)


Added catch of ../../../resource.txt. I again warn that there may be lacks!

([ftp:]|[\.\.])*\/[\/]{0,1}([a-z0-9\.\/]*[a-z0-9]*)([\\.a-z0-9]+)([0-9a-zAz:]+)[@]*[\/?#]*([\\.\/a-z0-9:]+)?([?=]([^#]*))?[#a-zA-Z]*

Demo

The answer is open for any editing and quotation!

Upvotes: 1

Related Questions