java - Replace all instances in string with unique replacement -


I am trying to replace all instances of a specific string with a unique replacement.

What do I need:

If I have this string:

  string test.script = "while (true) {} While (10 <7) {} while ((10 <7)) {} ";  

I want this output:

  while (arg0 <5000 & true) {} while (arg1 <5000 & amp; Amp; 10 & lt; 7) {} while (arg2 and lt; 5000 & amp; nbsp; (10 & lt; 7)) {}  

What do I have :

However, the string is not found again in replaced all (I now think about it).

  while (arg0 and lt; 5000 & amp; true) {} while (arg0 <5000 & amp; amp; amp; amp; amp; 10 & lt; 7) {} While (arg0 <5000 & amp; amp; amp; (10 <7) She goes.  

SSCCE:

  public static zero main string [] args) {int counter = 0; String test.script = "while (true) {} while (10 <7) {} while ((10 <7)) {}"; String out = testScript.replaceAll ("while \\ s * \\ (", "while (RGR + (counter ++) +" & lt; 5000 & amp; amp;;; System.out Println (outside);}  

It looks like you're app app appendments Matcher class by and appendaltle methods.

Both of these methods require a temporary buffer, in which the new (replaced) version of the string In this case, stringbuffer is used.

Their purpose

  • App App Appetment (Stringbuffer SB, String Replacement) When match matches match to the final match (or the beginning of the string) From the beginning of the match, the text will be found, the replacement
  • the appendage (stringbuffer sb) when no match is left, but also adding text after the last match (Or if there is a complete origin In the second word if you have xxxxfooxxxxxfooxxxx text and you enter foo to bar
  • > If you want to change the Maitre then

      Xxxxfooxxxxxfooxxxx 1.Annex Replacement ^^^^^^^ will add buffer xxxxbar to 1. Append Replacement ^^^^^^^^ Add to Buffer xxxxxbar 3. AppendTail will add ^^^^ 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 (RGR + (Counter ++) +" 5000 & amp; amp; amp; amp;;}}} M. Append Tale (Sb); String result = sb.toString (); System.out.println (result);  

    output:

      while (arg0 <5000 (& amp; amp;; & quot;}} whereas while (arg1 <5000 10 lt; 7) {} (arg2 5000 nbsp ;; previous> 

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -