0

I am working with regex for fetching string value containing quotes. In below example I want to get value summary key as "Here is "summary". Currently I am getting only "Here is " as output of below program. I want to escape all double quotes those comes in-between first and final double quote.

    String in = "summary = \"Here is \"summary\"";  

    Pattern p = Pattern.compile("'(.*?)'|\"(.*?)[^\\\"]+\"");
    Matcher m = p.matcher(in);

    while(m.find()) {
        System.out.println(m.group());
    }

Thanks for any help.

AAjit
  • 61
  • 1
  • 10

1 Answers1

1

use this one:

/\\["']((?:[^"\\]|\\.)*)\\["']/

Demo : https://regex101.com/r/033EKx/1

Remember: When trying to build regex by " , \ should change to \\ and other special characters (" and ')

rStr = "/\\\\[\"\']((?:[^\"\\\\]|\\\\.)*)\\\\[\"\']/" ;
MohaMad
  • 2,575
  • 2
  • 14
  • 26
  • Thanks. I changed a bit to fit my solution "((?:[^"\\]|\\.)*)" – AAjit Mar 03 '17 at 06:33
  • Getting exception in Java Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 22 ["']((?:[^"\]|\.)*)["'] – AAjit Mar 08 '17 at 03:52
  • Problem is in converting string regex pattern with " string definer, some \ are missed. what is your string @AAjit – MohaMad Mar 08 '17 at 09:23
  • About un-closed character class: [this](http://stackoverflow.com/questions/21816788/unclosed-character-class-error) and [this topic](http://stackoverflow.com/questions/14390722/java-regex-pattern-unclosed-character-class) @AAjit – MohaMad Mar 08 '17 at 09:35