Code
See regex in use here
"key"\s*:\s*"([^"]*)"
To match the possibility of escaped double quotes you can use the following regex:
See regex in use here
"key"\s*:\s*"((?:(?<!\\)\\(?:\\{2})*"|[^"])*)"
This method ensures that an odd number of backslashes \ precedes the double quotation character " such that \", \\\", \\\\\", etc. are valid, but \\", \\\\", \\\\\\" are not valid (this would simply output a backslash character, thus the double quotation character " preceded by an even number of backslashes would simply result in a string termination).
Matching both strings
If you're looking to match your second string as well, you can use either of the following regexes:
\bkey\b(?:"\s*:\s*|.*?)"([^"]*)"
\bkey\b(?:"\s*:\s*|.*?)"((?:(?<!\\)\\(?:\\{2})*"|[^"])*)"
Usage
See code in use here
import re
s = 'blahblah "key":"value","TargetCRS": "Target","TargetCRScode": "vertical Code","zzz": "aaaa" sadzxc "sss"'
r = re.compile(r'''"key"\s*:\s*"([^"]*)"''')
match = r.search(s)
if match:
print match.group(1)
Results
Input
blahblah "key":"value","TargetCRS": "Target","TargetCRScode": "vertical Code","zzz": "aaaa" sadzxc "sss"
blalasdl8ujd key [any_chars_here] "value", blabla asdw "alo":"ebobo", bla"www":"zzzz"
Output
String 1
- Match:
"key":"value"
- Capture group 1:
value
String 2 (when using one of the methods under Matching both strings)
- Match:
key [any_chars_here] "value"
- Capture group 1:
value
Explanation
"key" Match this literally
\s* Match any number of whitespace characters
: Match the colon character literally
\s* Match any number of whitespace characters
" Match the double quotation character literally
([^"]*) Capture any character not present in the set (any character except the double quotation character ") any number of times into capture group 1
" Match the double quotation character literally
Matching both strings
\b Assert position as a word boundary
key Match this literally
\b Assert position as a word boundary
(?:"\s*:\s*|.*?) Match either of the following
"\s*:\s*
" Match this literally
\s* Match any number of whitespace characters
: Match this literally
\s* Match any number of whitespace characters
.*? Match any character any number of times, but as few as possible
" Match this literally
([^"]*) Capture any number of any character except " into capture group 1
" Match this literally