【发布时间】:2016-04-20 04:38:18
【问题描述】:
对正则表达式不太熟悉,但我有一段代码似乎没有按预期工作,我想我知道原因,但正在寻找解决方案。
这里是字符串“whereClause”
where filter_2_id = 20 and acceptable_flag is true
String whereClause = report.getWhereClause();
String[] tokens = whereClause.split("filter_1_id");
Pattern p = Pattern.compile("(\\d{3})\\d+");
Matcher m = p.matcher(tokens[0]);
List<Integer> filterList = new ArrayList<Integer>();
if (m.find()) {
do {
String local = m.group();
filterList.add(Integer.parseInt(local));
} while (m.find());
}
当我调试时,它看起来像是到达 if (m.find()){ 但它只是完全跳过它。是因为正则表达式模式 (\d{3}\d+) 只查找大于 3 位的数字吗?我实际上需要它来扫描任何一组数字,所以我应该将它作为 0-9 包含在里面吗?
请帮助/建议
【问题讨论】:
-
但是,如果您查看字符串,其中有一个数字...我不需要那个,只要 = 符号后的任何数字即可
-
您没有提供样本输入和预期输出
-
\d将匹配 1 个数字。与[0-9]相同。表达式{3}表示与前面的模式完全匹配 3 次。 -
我认为你应该用“filter_2_id”而不是“filter_1_id”来分割。然后您应该将该模式应用于令牌[1] 而不是令牌[0]。也许你应该去掉标记的 and 子句[1] ...