【问题标题】:how to remove specific String from String in java如何在java中从String中删除特定的String
【发布时间】:2013-08-20 06:36:47
【问题描述】:

我正在从字符串中删除特定的字符串。首先我通过文件阅读器读取文本文件,然后我将文件的内容存储到字符串数组中并从字符串数组中删除一些特定的字符串

我的输入文本是:

    :VL
15
n
3 c

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
    Top Terms: 
    Weight : 
props 
 optional
:  Point:
    1.0:
 15th:0.068

现在我正在阅读此文本并将其存储到字符串数组中:String [] Result

我的代码:

for(String t1: Result){
Pattern = Pattern.compile("\\b:[0-9]\\b");
                matcher = pattern.matcher(t1);
                if(matcher.find()){
                    System.out.println(t1);
}

输出 我得到了:

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
    Top Terms: 
    Weight : 
 15th:0.068

但是我不想要这个输出。我的输出应该是这样的:

09:0.023
 15th:0.023
 1987:0.025
 1st:0.025
 2:0.013
 2.0:0.043
 2003:0.056
 2005:0.056
 15th:0.068

请告诉我我必须应用什么正则表达式才能获得此输出。

【问题讨论】:

  • 我认为您需要删除没有任何数字的字符串
  • 告诉我正则表达式删除没有任何数字的字符串。
  • 我怀疑"2005:0.056 Top Terms: Weight :" 实际上可能是一行。

标签: java regex file parsing


【解决方案1】:

我怀疑

2005:0.056
    Top Terms: 
    Weight :

实际上是一行......不知何故。

该正则表达式应该(仅)匹配包含单个数字的“单词”的行。


我猜你实际上知道这一点(而你“忘了提”)。

如果你想匹配这些:

 2005:0.056 
 15th:0.023
 1st:0.023
 2nd:0.023
 3rd:0.023

但不是这些:

 2005:0.056 Top Terms:  Weight :
 1.0:

那么你需要一个更严格的正则表达式,match() 而不是 find;例如

pattern = Pattern.compile(
              "\\s*[0-9]+(st|nd|rd|th|(\\.[0-9]+))?:[0-9]+\\.[0-9]+\\s*");
for (String t1: Result) {
    matcher = pattern.matcher(t1);
    if (matcher.match()) {
        System.out.println(t1);
    }
}

但在这一点上,我猜你的“有效”行的实际标准是什么。

【讨论】:

    【解决方案2】:

    这可能会对您有所帮助。

     BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt"));
        String str = null;
        while ((str = br.readLine()) != null) {
             if((str.contains(":"))){
                 String[] arr=str.split(":");
                   if(arr.length==2){                      
                           if(Pattern.compile("\\d").matcher(arr[1]).find()){
                               System.out.println(str);
                           }                       
                   }
             }
        }
    

    输出

    09:0.023
     15th:0.023
     1987:0.025
     1st:0.025
     2:0.013
     2.0:0.043
     2003:0.056
     2005:0.056
     15th:0.068
    

    【讨论】:

      【解决方案3】:
      for(String t1: Result){
                  Pattern p= Pattern.compile("\\b:[0-9]\\b");
                                  Matcher m= p.matcher(t1);
                                  if(m.find()){
                                      System.out.println(t1);
                  }
      

      这段代码工作得很好!

      【讨论】:

      • 它不起作用,因为"2005:0.056 Top Terms: Weight " 在同一行
      【解决方案4】:

      我明白了。我在使用正则表达式\\s+ 从文件读取后拆分内容之后,我将数据存储到字符串数组中,即String [] Result

      应用相同的代码

      String []result;
      String returnValue:
                          file = new FileReader(filename);
                  reader = new BufferedReader(file);
                  String line = "";
                  while ((line = reader.readLine()) != null) {
                      returnValue += line + "\n";
                  }
                  result = returnValue.split("\\s+");
          for(String t1: result){
          Pattern = Pattern.compile("\\b:[0-9]\\b");
                          matcher = pattern.matcher(t1);
                          if(matcher.find()){
                              System.out.println(t1);
          }
      

      它给出的输出是这样的:

      09:0.023
       15th:0.023
       1987:0.025
       1st:0.025
       2:0.013
       2.0:0.043
       2003:0.056
       2005:0.056
       15th:0.068
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-01
        • 2016-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 2017-06-13
        相关资源
        最近更新 更多