【问题标题】:Java split string and removeJava拆分字符串并删除
【发布时间】: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


【解决方案1】:

正则表达式模式

 \d{m,}   // allows numbers of having m or more digits.
 \d{m,n}  // allows numbers of between m and n digits.

对于您当前的问题,请使用

Pattern p = Pattern.compile("(\\d{3,})");

【讨论】:

    【解决方案2】:

    您使用的正则表达式 \\d+ 将查找 1 个或多个数字,这也是您获得单个数字的原因。 您需要将正则表达式更改为 \\d{3}\\d* 以查找 3 位或更多位。

    使用 split 来拆分 filter_1_id 上的字符串。然后使用结果数组中的第一个字符串。 String[] tokens = str.split("filter_1_id");

    import java.util.regex.*;
    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    class FilterTest
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            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);
            String[] tokens = str.split("filter_1_id");
            Pattern p = Pattern.compile("(\\d{3}\\d*)");
            Matcher m = p.matcher(tokens[0]);
    
            List<String> numberList = new ArrayList<String>();
    
            if( m.find() ){
                do {
                    String local = m.group();
                    System.out.println(local);
                    numberList.add(local);
                } while(m.find());
            }
        }
    }
    

    【讨论】:

    • ashutosh,如果我需要这些数字是一个整数数组,有没有一种简单的方法可以使用上面的代码来做到这一点?
    • @Buccaneer 您可以将numberList 数组更改为List&lt;Integer&gt; 类型,然后通过将其解析为整数numberList.add(Integer.parseInt(local)) 将数字添加到列表中
    猜你喜欢
    • 2021-07-17
    • 2013-01-14
    • 2014-02-11
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多