我正在尝试匹配包含 [和后跟数字大于0 的字符串。
例如,我的正则表达式模式应匹配以下字符串:
Sample.3cb11b [1] .books.addressDetail.addrDetail2
Sample.3cb11b [28] .books.addressDetail.addrDetail2
并且不应该匹配
Sample.3cb11b [0] .books.addressDetail.addrDetail2
我尝试过类似".*\\[[1-9].*"
之类的事情。
答案 0 :(得分:2)
我已经尝试过此正则表达式,并且看来可行:
^([a-zA-Z0-9]*).([0-9a-z]*)(\[[1-9][0-9]*?\])(.[a-zA-Z0-9]*){3} // this is very exact, maybe too much
.*\[[1-9][0-9]*\].* // this seems to also work, but only cares about the 'greater than 0' part
这是regex tester的链接。
答案 1 :(得分:1)
怎么样:.*\[0*[1-9][0-9]*\].*
?
简单的测试程序:
String[] lines = {
"Sample.3cb11b[1].books.addressDetail.addrDetail2",
"Sample.3cb11b[28].books.addressDetail.addrDetail2",
"Sample.3cb11b[0].books.addressDetail.addrDetail2"};
for (String line: lines) {
System.out.println(line + ": "
+ line.matches(".*\\[0*[1-9][0-9]*\\].*"));
}