用于有效URI Java的正则表达式

时间:2018-08-15 13:18:07

标签: java regex validation uri

我有以下任务:我必须使用以下语法为有效的URI创建一个正则表达式: scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]。并且必须保存符号“:”,“ /”,“?”,“#”,“ []”,“ @”(可能表示常量),并且如果它们像某些组件(路径,查询,片段),它们必须经过百分比编码(而问号必须是-%3F)。

请您帮我看看该正则表达式。现在是我的,但是我不确定它是否正确:

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

以下是一些有效的示例:

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:mypassword@localhost.com:21/somepath/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost.com:21/somepath?key=value#somefragment
ftp://myusername:mypassword@localhost.com:21?key=value#somefragment
ftp://myusername:mypassword@localhost.com:21#somefragment
ftp://myusername:mypassword@localhost.com:21

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

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

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

1 个答案:

答案 0 :(得分:1)

我试图大致写出与之匹配的RegEx。似乎可行,但当然可能缺少!我不是RegEx专家。

               Date indicator_1 indicator_2  desired_column_1  \
0  2013-04-01 03:50:00           x           w               NaN
1  2013-04-01 04:00:00           y           u              10.0
2  2013-04-01 04:15:00           z           v              25.0
3  2013-04-01 04:25:00           x           w              35.0
4  2013-04-01 04:25:00           z           u              35.0
5  2013-04-01 04:30:00           y           u               5.0
6  2013-04-01 04:35:00           y           w              10.0
7  2013-04-01 04:40:00           z           w              15.0
8  2013-04-01 04:40:00           x           u              15.0
9  2013-04-01 04:40:00           y           v              15.0
10 2013-04-01 04:50:00           x           w              10.0

    desired_column_2
0                NaN
1                NaN
2               15.0
3               25.0
4               25.0
5               30.0
6                5.0
7               10.0
8               10.0

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


添加了../../../resource.txt的捕获量。我再次警告可能会缺少!

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

Demo

答案已打开,可以进行任何编辑和引用!