【发布时间】:2016-02-12 19:25:07
【问题描述】:
我有一个String,我需要搜索并从中提取所有大于 3 位的数字,并在某个点进行拆分,并且仅从拆分前获取这些值。
这是字符串
String str = "where filter_2_id = 20003 and (acceptable_flag is true or acceptable_flag is null) and filter_2_id IN (20003) AND filter_5_id IN (50053, 50014) AND filter_1_id IN ( 10000 ) AND filter_2_id IN ( 20000, 20001, 20002, 20003, 20004 )";
基本上String 可能有也可能没有“AND filter_1_id...”以及它后面的所有内容,但我需要搜索并查看String 是否包含它。如果是这样,我想删除它以及之后的任何内容。我正在使用正则表达式来解析数字,但我不需要 1 位或 2 位数字。这是我做的一个示例,但它没有考虑拆分或删除 1-2 位数字。
public class FilterTest {
public static void main(String args[]){
doMagic();
}
public static void doMagic(){
String str = "where filter_2_id = 20003 and (acceptable_flag is true or acceptable_flag is null) and filter_2_id IN (20003) AND filter_5_id IN (50053, 50014) AND filter_1_id IN ( 10000 ) AND filter_2_id IN ( 20000, 20001, 20002, 20003, 20004 )";
//String parsedString = StringUtils.trimWhitespace(str);
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(str);
List<String> numberList = new ArrayList<String>();
if( m.find() ){
do {
String local = m.group();
System.out.println(local);
numberList.add(local);
} while(m.find());
}
}
}
这是我现在得到的输出: 2 20003 2 20003 5 50053 50014 1 10000 2 20000 20001 20002 20003 20004
我需要这个: 20003 20003 50053 50014
【问题讨论】:
-
你想从你的例子中得到什么结果?
-
好吧,我基本上需要将它们放入一个列表中,但我还需要将它们全部转换为 Integer。你问的是这个吗?
-
请编辑您的问题并添加附加信息。请准确说明您期望的输出以及获得的输出。
-
你正危险地接近应该解析这个“表达式”的地步。
-
如果以下答案之一回答了您的问题,本网站的工作方式,您将“接受”答案,更多信息请点击此处:当有人回答我的问题时我该怎么办?(stackoverflow.com/help/someone-answers )。但前提是你的问题真的得到了回答。如果没有,请考虑在问题中添加更多详细信息。
标签: java regex parsing substring