用唯一替换替换字符串中的所有实例 [英] Replace all instances in string with unique replacement

查看:94
本文介绍了用唯一替换替换字符串中的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用唯一替换替换特定String的所有实例。

I'm attempting to replace all instances of a particular String with a unique replacement.

我想要的是什么:

如果我有这个字符串:

String testScript = "while(true) { } while   (10 < 7) { } while((10 < 7)) { }";

我想要这个输出:

while(arg0 < 5000 && true) { } while(arg1 < 5000 && 10 < 7) { } while(arg2 < 5000 && (10 < 7)) { }

我有什么:

然而,传入 replaceAll 的字符串不会再次被查询(现在很明显我想到了它)。

However, the string passed in to replaceAll doesn't get queried again (obvious now I think about it).

while(arg0 < 5000 && true) { } while(arg0 < 5000 && 10 < 7) { } while(arg0 < 5000 && (10 < 7)){ }

我们非常感谢任何答案或评论。

Any answers or comments, as always, are greatly appreciated.

SSCCE:

public static void main(String[] args) {
     int counter = 0;
     String testScript = "while(true) { } while   (10 < 7) { } while((10 < 7)) { }";
     String out = testScript.replaceAll("while\\s*\\(", "while(arg" + (counter++) + " < 5000 && ");
     System.out.println(out);
}


推荐答案

您似乎在寻找 appendReplacement appendTail 来自 Matcher <的方法/ code> class。

It seems that you are looking for appendReplacement and appendTail methods from Matcher class.

这两种方法都需要临时缓冲区,其中将放置新的(替换)版本的字符串。在这种情况下使用了StringBuffer

Both these methods require temporary buffer in which new (replaced) version of string will be placed. In this case StringBuffer is used.

他们的目的是添加到已修改文本的缓冲区块

Their purpose is to add to buffer chunks of modified text


  • appendReplacement(StringBuffer sb,String replacement)何时匹配将从上次匹配中找到文本(或者在第一次匹配时从字符串)直到当前比赛开始+替换

  • appendTail(StringBuffer sb)当没有比赛时,我们还需要在最后一场比赛后添加文字(或i f没有匹配的整个原始字符串)。

  • appendReplacement(StringBuffer sb, String replacement) when match will be found text from last match (or in case of first match from start of the string) till start of current match + replacement
  • appendTail(StringBuffer sb) when there is no match left, but we also need to add text after last match (or if there was no match entire original string).

换句话说,如果你有文字 xxxxfooxxxxxfooxxxx 并且您想要将 foo 替换为 bar 匹配器将需要调用

In other words if you have text xxxxfooxxxxxfooxxxx and you want to replace foo to bar matcher will need to invoke

                       xxxxfooxxxxxfooxxxx
1. appendReplacement   ^^^^^^^              will add to buffer xxxxbar
1. appendReplacement          ^^^^^^^^      will add to buffer xxxxxbar
3. appendTail                         ^^^^  will add to buffer xxxx

所以此缓冲区将包含 xxxxbarxxxxxbarxxxx

演示

String testScript = "while(true) { } while   (10 < 7) { } while((10 < 7)) { }";
Pattern p = Pattern.compile("while\\s*\\(");
Matcher m = p.matcher(testScript);

int counter = 0;
StringBuffer sb = new StringBuffer();

while(m.find()){
    m.appendReplacement(sb, "while(arg"+ (counter++) + " < 5000 && ");
}
m.appendTail(sb);

String result = sb.toString();
System.out.println(result);

输出:

while(arg0 < 5000 && true) { } while(arg1 < 5000 && 10 < 7) { } while(arg2 < 5000 && (10 < 7)) { }

这篇关于用唯一替换替换字符串中的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆