【问题标题】: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\\*");
【解决方案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*");