Search code examples
regexc++11qregularexpression

Match any char inside two quotation pairs, including nested quotations


I have data that will appear as dual quotation pairs like this, per line.

"Key" "Value"

Inside of these pairs there can be any character, and sometimes there comes the dreaded "" nested pair:

"Key "superkey"" ""Space" Value"

Previously I've found: "([^"]*)"\s*"([^"]*)" And this matches Key and Value to two groups:

$1 = Key
$2 = Value

But, with the nested pairs, it will only output:

$1 = superkey

Is there a way to match all characters between the pairs? Example output wanted:

$1 = Key "superkey"
$2 = "Space" Value

Regular expression processing from QRegularExpression and c++11 Literal string:

QRegularExpression(R"D("([^"]*)"\s*"([^"]*)")D");

I know it matches Pearl and PHP regex.


Solution

  • "(.*?)"[\t\r ]+"(.*?)"(?=[ ]*$)
    

    Try this.See demo.

    https://regex101.com/r/hR7tH4/2