Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中验证查询参数_Java_Regex - Fatal编程技术网

在Java中验证查询参数

在Java中验证查询参数,java,regex,Java,Regex,我需要验证这个查询URL one=1&two=2 但我还需要允许键和值都为空。所以这也应该是有效的 one=&two=2 =1&two=2 尝试模式:\w*=\w+\&\w*=\w+* Pattern pattern = Pattern.compile("\\w*=\\w+(\\&\\w*=\\w+)*"); Matcher matcher = pattern.match("=1&two=2&three=345"); if (matcher.m

我需要验证这个查询URL

one=1&two=2
但我还需要允许键和值都为空。所以这也应该是有效的

one=&two=2
=1&two=2

尝试模式:\w*=\w+\&\w*=\w+*

Pattern pattern = Pattern.compile("\\w*=\\w+(\\&\\w*=\\w+)*");
Matcher matcher = pattern.match("=1&two=2&three=345");
if (matcher.matches()) {
    // TODO:
} 已更新以匹配两个键/值的值为空:\w*=\w*\&\w*=\w**

\w* means ZERO OR MORE word character
\w+ means ONE OR MORE word character
\w? means ZERO or ONE word character

你的意思是这样的吗:

String regex = "\\w*=\\d*&\\w*=\\d*";
System.out.println("one=1&two=2".matches(regex));//true
System.out.println("=1&two=2".matches(regex));//true
System.out.println("one=&=2".matches(regex));//true

我不确定,如果只有两个参数是有效的。这个正则表达式扫描多个参数。参数名称和值可以为零。没有名称和值的条目有效吗

\w*=\d*(?:&\w*=\d*)*

是的,对于这个wo案例,它对我有效,但我还需要考虑两个合并的so。1=&=2应该可以工作。这个如何匹配1=&。。。值部分始终为非空。因此,这不是正确答案。\w*=\w*\&\w*=\w**-使用此匹配项,两个键/值都为空。请在询问之前尝试共享一些内容