There are texts like:

keyOne: "Lorem ipsum" keyTwo: "Lorem ipsum" 

and:

 keyOne: "Lorem ipsum" "dolor \"sit amet\", " "consectetur adipiscing elit" keyTwo: "Lorem ipsum" 

If in the first case it is easy to get the key: value pair on the basis of \/"$\ , then in the second case there is only one way out - to find a match with one of the keys (fixed limited set). The question is how to add the restriction to the entry of keys in the pattern \(?<key>(keyOne|keyTwo))[\s*](?<value>[\"][[:print:]\s]*[\"]$)?\ to get in the second case the keyOne key keyOne

 "Lorem ipsum" "dolor sit amet, " "consectetur adipiscing elit" 

?

    1 answer 1

    In my sign of the beginning of the line before the key and the fact that the values ​​are entirely enclosed in quotes is more than enough.

     /(?<key>^\w+):\s*(?<value>"(?:[^"]+|"\n")+")/gm 

    Regex101.com example

    If you need to list the keys for some other reasons, you can enter them directly into the group <key> , instead of \w .

    Taking into account the possible escape of quotation marks with backslips and screening of the backslashes themselves, according to the rules of most programming languages, the following goes:

     /(?<key>^\w+):\s*(?<value>"(?:[^"\\]+|\\.|"\n")+")/gm 

    Hello at regex101.com

    • Yes, this is a good option, and it works absolutely correctly, but in quotes (I did not give an example) there can be any values, including escaping quotes (\ ") and in this case you should add a check for the slash before the quotation mark - Alaksander
    • @Alaksander Therefore, always describe the task as accurately as possible. You can add about screening. And while I remember how it is usually implemented. (I understand correctly that \\" should be perceived as a normal slanting and closing quotation mark) - Mike
    • Yes, this is what you need, thanks for your time - Alaksander