简体   繁体   English

需要正则表达式来验证字符串

[英]Need Regular Expression to validate string

Could you please help in creating a regular expression which can validate the following string of below format in Java?您能否帮助创建一个正则表达式,它可以在 Java 中验证以下格式的以下字符串?

The string contains key and value separated by a colon(:).该字符串包含由冒号 (:) 分隔的键和值。 These key and value pairs are further separated by either an & or |这些键值对由 & 或 | 进一步分隔。 symbol.象征。

"key:value"|"key:value"&"key:value"|"key:value"

Here key is string which can have only alphabets.这里的 key 是只能有字母的字符串。 Here value is alphanumeric.这里的值是字母数字。

("\w+:\w+"[\|\&]?)+

This will match in order:这将按顺序匹配:

"
1 or more word characters [a-zA-Z0-9]
:
1 or more word characters [a-zA-Z0-9]
"
0 or 1  | or & character

and this, 1 or more times而这,1次或多次

And I agree with J. Chomel, this is not higher level regex so with a little bit of research, you should have been able to figure it out yourself.我同意 J. Chomel,这不是更高级别的正则表达式,所以通过一些研究,你应该能够自己弄清楚。

Have a look at these websites for more info on regex:查看这些网站以获取有关正则表达式的更多信息:

http://regexone.com/ http://regexone.com/

http://regex101.com/ http://regex101.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM