[英]How recognize an array in string? With regex in Java
I'm trying to make an regular expression to recognize if a given String is an array or not.我正在尝试制作一个正则表达式来识别给定的 String 是否是一个数组。
For example,例如,
String foo = "a[1]"; // true
String bar = "b[]"; // true
String tar = "test"; // false
String zoo = "b[][]" // true
How can I recognize it?我怎么能认出来?
I try something like this:我尝试这样的事情:
Pattern p = Pattern.compile("[_a-zA-Z][_a-zA-Z0-9]*");
But I don't know how represent the "[" and "]" characters.但我不知道如何表示“[”和“]”字符。
Thank's in advance.提前致谢。
Pattern p = Pattern.compile("[_a-zA-Z]+(?:\\[[_a-zA-Z0-9]*\\])+");
This should do it for you.这应该为你做。
escape
[]
.See demo. escape
[]
。见演示。
https://regex101.com/r/sS2dM8/17 https://regex101.com/r/sS2dM8/17
试试这个它匹配所有数组类型
[a-zA-Z_]+(?:\\[\d*\\])+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.