【问题标题】:Removing a word that contains specific word or part of the word from a string从字符串中删除包含特定单词或部分单词的单词
【发布时间】:2019-08-08 15:59:31
【问题描述】:

我有一个字符串,我想删除其中包含特定单词的特定单词。

例如: String str = "Create_DateTime, Hello, DateTime, Before" 每个包含单词“日期”的单词都应该被删除,所以在删除后我们将得到以下字符串: Hello,Before

我有这个字符串: Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution

我已经设法删除了所有不需要的单词,所以现在我的字符串看起来像这样: ,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution

我想删除单词前后的“,”

这是我用来执行上述操作的代码:

String str1 = str.replaceAll("\\w*Date\\w*","");

原始字符串: Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution

预期:

Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,Metrics_Size_KB,,Tags_Countfrom command_execution

实际: ,Metrics_Count,Device_UID,Command_Name,Command_Interval,,Instance_UID,,Metrics_Size_KB,,Tags_Countfrom command_execution

【问题讨论】:

    标签: java regex string


    【解决方案1】:

    一种方法是使用 java 流

    String str = "Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution";
    
    String result = Stream.of(str.split(","))
                          .filter(word -> !word.contains("Date"))
                          .collect(Collectors.joining(","));
    

    【讨论】:

      【解决方案2】:

      似乎您真正想要做的是使用数组。 (因为您似乎正在处理一个实际上是一堆字符串和逗号的字符串)。完成您想要的一种方法是执行 str.split(",") 然后循环检查是否 strArray[i].contains("date")。让我知道我是否应该更清楚

      【讨论】:

      • |是的,我可以这样做,而且在性能方面会更便宜,但我真的很好奇应该如何使用正则表达式。
      • String str = "Expected_Start_DateTime,Metrics_Count,Device_UID,Command_Name,Command_Interval,Start_DateTime,Instance_UID,Execution_Date,Metrics_Size_KB,End_DateTime,Tags_Countfrom command_execution"; StringBuilder sb = new StringBuilder(); for(String st: str.split(",")){ if(st.contains("Date")){ continue; } sb.append(st+","); } 但最后我在字符串生成器的末尾得到了不需要的“,”。
      • @tupacshakur 所以,毕竟,从接受的答案来看,您对正则表达式不太感兴趣。而且很好。
      • @WiktorStribiżew 我仍然很好奇它是如何使用正则表达式制作的,但当然,我更喜欢成本较低的方法。
      • @tupacshakur Arthur 的回答是应该怎么做,单一的正则表达式方法看起来很笨拙,可读性和可维护性较差等。
      【解决方案3】:

      如上所述,使用数组和.split() 的方法可能是更好的方法,但如果您对正则表达式感兴趣,可以使用以下几个方法来实现您的目标。

      \b\w*?[dD]ate\w*?\b - 这会选择其中包含“日期”的任何单词。

      • \b 标签指定了单词边界 - 如果字符不是单词字符(不是 \w),它将中断,并且还会在字符串的开头和结尾中断,因此它会捕获字符串中的第一个词和最后一个词。
      • \w 标记指定“单词”字符:a-zA-Z0-9_。由于您的文字中没有任何奇怪的字符,因此效果很好!
      • *? 匹配尽可能多的单词字符需要,但尽可能少可以。这确保它捕获“日期”之前的所有字符,但只捕获尽可能多的字符对于这个词来说是必要的。
      • [dD]ate 按字面意思查找单词 dateDate

      Try it here!


      ^,*|,*$ - 这会选择前导和尾随逗号供您处理。

      • ^,* - 将逗号锚定在字符串的前面,尽可能多地获取。
      • | 或...
      • ,*$ - 尽可能多地使用逗号,固定在字符串的后面。

      Try it here!


      ,{2,} - 这会选择 2 个或更多逗号的所有实例,以便您用单个逗号替换。

      • ,选择逗号,只要...
      • {2,} 有 2 到无限数量的它们,彼此相邻。

      Try it here!


      祝你好运!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-20
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 2019-05-18
        相关资源
        最近更新 更多