Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

java - Replace all instances in string with unique replacement

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

What I would like:

If I have this String:

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

I would like this output:

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

What I have:

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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) 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).

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

so after this buffer will contain xxxxbarxxxxxbarxxxx.

Demo

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);

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...