Results 1 to 3 of 3
- 04-25-2011, 03:35 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
String.replaceAll(str, str) is leading to infinite loop. Help?
The String variable body is assigned a value by the user and then this code should be replacing any number of carriage returns with a single string "<*>".
If I run just the first replaceAll() then it works fine. If I run either of the codes blow then the program stalls.
Java Code:body = body.replaceAll("\n", "<*>");I also tried a while loopJava Code:body = body.replaceAll("<*><*>", "<*>");
Like I said, they all ended in a stalled program.Java Code:while(body.indexOf("<*><*>") != -1) body.replaceNext("<*><*>", "<*>");
Any help would be greatly appreciated!
Here is a compilable test code:
For some reason it is not stalling in this but it is also not replacing anything. Thoughts?Java Code:public class Test { public static void main(String[] args){ String body = "This is a \n\n test of a code I would like\n\n to work.. \nbut\n\n it is not working.\n"; System.err.println(body); body = body.replaceAll("\n", "<*>"); System.err.println(body); body = body.replaceAll("<*><*>", "<*>"); System.err.println(body); } }
- 04-25-2011, 04:07 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
The first argument is a regex, and * is a special regex character (zero or more times) so you have to escape it -> Geek And Poke: The Geek Joke Of The Week :D
Try body = body.replaceAll("<\\*><\\*>", "<*>");
edit: or change your first replace statement --> body = body.replaceAll("\n+", "<*>");Last edited by eRaaaa; 04-25-2011 at 04:11 PM.
- 04-25-2011, 04:13 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
infinite loop
By javapink in forum New To JavaReplies: 19Last Post: 03-06-2011, 02:28 AM -
Regular Expressions and String.replaceAll()
By meta1203 in forum New To JavaReplies: 1Last Post: 11-24-2010, 11:41 PM -
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08-13-2010, 11:45 PM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks