regex - Java Matcher and Pattern: Why does this go on forever -


//remove multiple        pat=pattern.compile("accept .*?\\.",pattern.dotall);        m=pat.matcher(str);               while(m.find())        {            int start=m.group().indexof("with") +1;           string part=m.group().substring(start);           part=part.replaceall("with", "");           part=m.group().substring(0, start).concat(part);            if(!m.group().equals(part))           {                str=m.replacefirst(part);            }         } 

any idea why infinite loop? m.group never equal part. don't know why. tried reset nothing.

i have no idea trying accomplish, there bug here:

if(!m.group().equals(part)) {     str=m.replacefirst(part); } 

you reassigning str, while matcher still works on original value of str. strings immutable, if reassign variable in 1 place, doesn't change reference in (see passing reference data type arguments on this page of sun java tutorial).

there more strange things going on, perhaps i'm not understanding correctly. in comment string starts accept , ends . dot. thing searching pattern.compile("accept .*?\\.",pattern.dotall);, , not capturing either. why bother searching in first place? thought knew input strings that.

what should post sample input , data want extract it. otherwise no 1 able you.


i guessing now: seem want remove multiple clauses string. should easier, this:

string test =     "accept pasta "        + "with tomatoes, parmesan cheese, olives "        + "with anchovies tuna more olives.";  system.out.println(     test.replaceall(         "(accept.*?with.*?)(?:\\s*with.*)(\\.)", "$1$2"     ) ); 

output:

accept pasta tomatoes, parmesan cheese, olives.

here's pattern, explained:

(       // start capturing group accept  // search literal accept .*?     // search shortest possible matching string         // (so no other can sneak in)    // search literal .*?     // search shortest possible matching string         // (so no other can sneak in) )       // close capturing group, we'll refer         // group $1 or matcher.group(1) (?:     // start non-capturing group \\s*    // search optional whitespace    // search literal .*      // search anything, greedily )       // close group, we'll discard 1 (       // open capturing group \\.     // search single period )       // close group, period accessible $2 

given updated requirements (remove withs keep args) here's updated solution:

final matcher matcher =     pattern.compile("with\\s*", pattern.dotall).matcher(test); final stringbuffer sb = new stringbuffer(); while(matcher.find()){     matcher.appendreplacement(sb, sb.length() == 0         ? matcher.group()         : ""); } matcher.appendtail(sb); system.out.println(sb.tostring()); 

output:

accept pasta tomatoes, parmesan cheese, olives anchovies tuna more olives.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -