I'm trying to make an regular expression to recognize if a given String is an array or not.
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.
试试这个它匹配所有数组类型
[a-zA-Z_]+(?:\\[\d*\\])+
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.