java - Replace consecutive characters in a string -
i need replace consecutive single quotes single double quote:
// ''hello world'' -> "hello world" // '''it's me.'''' -> "it's me." // ''''oh'' ''no''' -> "oh" "no" // ''this'''is''fine'' -> "this"is"fine" i feel can solved regex, don't know start.
here current solution:
private fix(string line) { stringbuilder builder = new stringbuilder(); (int = 0; < line.length(); ++i) { builder.append("'"); } while (builder.length() > 1) { line = line.replace(builder.tostring(), "\""); builder.deletecharat(0); } return line; }
there different so-called quantifiers define character can/must repeated given number of time.
probably known * (zero or more occurences) , + (one or more occurences). quantifier of in case {2,} means "two or more occurences". specialization of more general {min occurences,max occurences} quantifier.
so can that:
line.replaceall("'{2,}", "\""); in javadocs defined @ http://docs.oracle.com/javase/8/docs/api/java/util/regex/pattern.html#greedy
Comments
Post a Comment