regex - Java Replace double quote which is not in pair -


i have string like

"abc def" xxy"u 

i want replace double quote not in pair.

so in above example want replace xxy"u double quote not first 2 in pair.

output should in format.

"abc def" xxy\"u 

should work every non pair double quote - "111" "222" "333" "4 here " before 4 should replaced \"

thanks in advance.

it great if detect actual pair too, instead of last double quote. ex: "aaa" "bbb" "ccc "ddd" -> should replaced "aaa" "bbb" \"ccc "ddd"

this using

    int totalcountofdq = countoccurence(s, '"');     int lastindexofdq = s.lastindexof('"');     if(totalcountofdq % 2 == 1){         string start = s.substring(0, lastindexofdq);         string end = s.substring(lastindexofdq+1);         s = start + "\\\"" + end;     } 

and working example thought not working "4 "111" "222" correctly

you can try next:

private static final pattern regex_pattern =         pattern.compile("\\b\"\\w*( \\w*)*\"\\b");  private static string replacenotpairs(string input) {     stringbuffer sb = new stringbuffer();     matcher matcher = regex_pattern.matcher(input);     int start = 0;     int last = 0;     while (matcher.find()) {         start = matcher.start();         sb.append(input.substring(last, start).replace("\"", "\\\""));         last = matcher.end();         sb.append(matcher.group());     }     sb.append(input.substring(last).replace("\"", "\\\""));     return sb.tostring(); } 

e.g.:

public static void main(string[] args) {     system.out.printf("src: %s%nout: %s%n%n",             "\"abc def\" xxy\"u",             replacenotpairs("\"abc def\" xxy\"u"));     system.out.printf("src: %s%nout: %s%n%n",             "\"111\" \"222\" \"333\" \"4",             replacenotpairs("\"111\" \"222\" \"333\" \"4"));     system.out.printf("src: %s%nout: %s%n%n",             "\"aaa\" \"bbb\" \"ccc \"ddd\"",             replacenotpairs("\"aaa\" \"bbb\" \"ccc \"ddd\""));     system.out.printf("src: %s%nout: %s%n%n",             "\"4 \"111\" \"222\"",             replacenotpairs("\"4 \"111\" \"222\""));     system.out.printf("src: %s%nout: %s%n%n",             "\"11\" \"2 \"333\"",             replacenotpairs("\"11\" \"2 \"333\"")); } 

the output example input:

src: "abc def" xxy"u out: "abc def" xxy\"u  src: "111" "222" "333" "4 out: "111" "222" "333" \"4  src: "aaa" "bbb" "ccc "ddd" out: "aaa" "bbb" \"ccc "ddd"  src: "4 "111" "222" out: \"4 "111" "222"  src: "11" "2 "333" out: "11" \"2 "333" 

see explanation regex:

\b\"\w*( \w*)*\"\b 

regular expression visualization

(from http://rick.measham.id.au/paste/explain.pl?regex):

node                     explanation ----------------------------------------------------------------------------   \b                       boundary between 2 word chars (\w)                            or 2 non-word chars (\w) ----------------------------------------------------------------------------   \"                       '"' ----------------------------------------------------------------------------   \w*                      word characters (a-z, a-z, 0-9, _) (0 or                            more times (matching amount                            possible)) ----------------------------------------------------------------------------   (                        group , capture \1 (0 or more times                            (matching amount possible)): ----------------------------------------------------------------------------                              ' ' ----------------------------------------------------------------------------     \w*                      word characters (a-z, a-z, 0-9, _) (0 or                              more times (matching amount                              possible)) ----------------------------------------------------------------------------   )*                       end of \1 (note: because using                            quantifier on capture, last                            repetition of captured pattern                            stored in \1) ----------------------------------------------------------------------------   \"                       '"' ----------------------------------------------------------------------------   \b                       boundary between 2 word chars (\w)                            or 2 non-word chars (\w) 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -