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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -