Home > Software design >  Java regex find all occurences of a word inside double quotes
Java regex find all occurences of a word inside double quotes

Time:11-19

this is my regex, I'm trying to find all occurences of string "variant" inside double quotes using this regex, but if there are 2 occurences inside double quotes it will take only the last one. My regex basically tries to find the string "variant" which is written inside double quotes, I tried writing it like this but it didn't work either: \\".*(variant*).*\\" This is my code:

String str_regex = "\\".*(variant).*\\"";
Matcher str_matcher = Pattern.compile(str_regex).matcher(joined);
while(str_matcher.find()) {
    System.out.println(str_matcher.group());
}

CodePudding user response:

You can match all occurrences of the strings inside double quotes and then count the variant occurrences inside the matches:

String s = "variant \"if and only if 5 divides by i without remainder, then it prints \\\"i   \\\" variant: \\\"   variant\\\"\" variant";

        
Pattern pattern = Pattern.compile("(?s)(?<!\\\\)(?:\\\\{2})*\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"");
Matcher matcher = pattern.matcher(s);
int count = 0;
while (matcher.find()){
    count  = matcher.group().split("variant", -1).length-1;
}
System.out.println(count);

See the online demo. So, the variant "if and only if 5 divides by i without remainder, then it prints \"i \" variant: \" variant\"" variant string contains two occurrences of variant.

The regex is (?<!\\)(?:\\{2})*"[^"\\]*(?:\\.[^"\\]*)*", see its demo.

Details:

  • (?<!\\) - no \ allowed immediately on the left
  • (?:\\{2})* - zero or more double backslashes
  • " - a " char
  • [^"\\]* - zero or more chars other than " and \
  • (?:\\.[^"\\]*)* - zero or more sequences of any escaped char and then zero or more chars other than \ and "
  • " - a " char.
  • Related