我有一个字符串~~40~~ Celsius Temp: 33 Celsius Temp:~~50~~
我想用另一个字符串 '**' 替换子字符串 '~~' 的奇数次出现,即第1个、第3个等。
我的输出应该是 **40~~ Celsius Temp: 33 Celsius Temp:**50~~
如何在Java中使用正则表达式实现这一功能?
我有一个字符串~~40~~ Celsius Temp: 33 Celsius Temp:~~50~~
我想用另一个字符串 '**' 替换子字符串 '~~' 的奇数次出现,即第1个、第3个等。
我的输出应该是 **40~~ Celsius Temp: 33 Celsius Temp:**50~~
如何在Java中使用正则表达式实现这一功能?
~~
时,我们会执行以下两种操作之一。如果它是奇数次出现,则我们将空字符串添加到替换内容中,否则我们重新添加我们匹配到的 ~~
。String input = "~~40~~ Celsius Temp: 33 Celsius Temp:~~50~~";
Pattern p = Pattern.compile("~~");
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer(input.length());
int i = 0;
while (m.find()) {
if (i % 2 == 0) {
m.appendReplacement(sb, "**");
}
else {
m.appendReplacement(sb, m.group(0));
}
++i;
}
m.appendTail(sb);
System.out.println(sb.toString());
**40~~ Celsius Temp: 33 Celsius Temp:**50~~
"**"
以符合OP的要求,但如果只需要替换成对,那么没有真正的需要计数出现次数。 - David Conrad~~
的_成对_。实际上,我们想要的替换逻辑是迭代,用或不用一对,替换所有奇数次出现的情况,所以... - Tim Biegeleisen我认为针对你的问题陈述,你无需搜索奇数次出现的情况。从示例中可以看出,你需要将~~(数字)替换为**(数字),并忽略其他格式的~~。
虽然有些人已经提出了类似的解决方案,但仍然:
String org = "~~40~~ Celsius Temp: 33 Celsius Temp:~~50~~";
String rep = org.replaceAll("~~(\\d)","**$1");
~~(\d)
将搜索以 ~~
开头的数字,并用 **
替换,以保留第一个数字,使用 $1
。如果~~
成对出现,你可以使用捕获组的替换。
private final static Pattern pattern = Pattern.compile("(?:~~([^~]+~~))");
public static String replaceOddTildes(String value) {
return pattern.matcher(test).replaceAll("**$1");
}
并且:
String result = replaceOddTildes("~~40~~ Celsius Temp: 33 Celsius Temp:~~50~~"));
replaceOddTildes("An ~~un~~ paired ~~example!").equals("An **un~~ paired ~~example!")`
如果您希望按照这种方式处理不匹配的一对,那当然可以。
模式详细信息:
(?: a non-capturing group, consisting of
~~ a pair of tildes, followed by
( a capturing group, consisting of
[^~]+ one or more characters that is not a tilde, followed by
~~ a pair of tildes
) end of the capturing group
) end of the non-capturing group
匹配替换的方式是用一对星号代替捕获组中匹配到的内容。