【问题标题】:Regex - search a word and replace it with same word using Case INSENSITIVE [duplicate]正则表达式 - 使用 Case INSENSITIVE 搜索一个单词并用相同的单词替换它[重复]
【发布时间】:2012-10-17 06:17:57
【问题描述】:

可能重复:
regex replace all ignore case

如何获得所需的输出?

输入字符串-

"Software Industry. software Industry. SOFTWARE INDUSTRY. software industry. "

要搜索的词 -

"software industry"

输出 -

"*Software Industry*. *software Industry*. *SOFTWARE INDUSTRY*. *software industry*. "

简而言之,我必须找到一个短语并将相同的短语替换为起始和结束星号 (*),忽略大小写 ..

【问题讨论】:

    标签: java string search replace case-insensitive


    【解决方案1】:

    您可以将replaceAll(?i) 一起使用,这将替换case-insensitive

    String str = "Software Industry. software Industry. SOFTWARE INDUSTRY. "+
                                                        "software industry. "
    str = str.replaceAll("(?i)(software industry)", "\\*$1\\*");
    

    【讨论】:

      【解决方案2】:

      您可以为此使用模式匹配 (Regex)。

      首先使用组提取短语并存储在字符串或任何其他数据结构中。 然后使用 Replace 函数来实现你的输出。

      你可以参考这篇文章:http://www.vogella.com/articles/JavaRegularExpressions/article.html

      【讨论】:

        【解决方案3】:

        你也可以试试:

        String str=new String("Software Industry. software Industry. SOFTWARE INDUSTRY. software industry.");
        str=Pattern.compile("(software industry)",Pattern.CASE_INSENSITIVE).matcher(str).replaceAll("*$1*");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-21
          • 1970-01-01
          • 2012-03-10
          • 1970-01-01
          • 2018-03-27
          • 1970-01-01
          • 1970-01-01
          • 2020-01-06
          相关资源
          最近更新 更多