[英]Regex pattern with 3 groups in Python
I am trying to understand regex in Python. 我试图了解Python中的正则表达式。 I'm trying to trying this pattern: 我正在尝试这种模式:
string = re.sub(r'(\".+?\")(: )', '\"here!\": ', string)
for this kind of string: 对于这种字符串:
{ some text "data": { "first": "xyz" some text}
I expected this to replace "first": "xyz" but to my surprise the string which is replaced is: "data": { "first": "xyz" . 我希望这可以代替“ first”:“ xyz”,但是令我惊讶的是,被替换的字符串是: “ data”:{“ first”:“ xyz” 。 Why is that? 这是为什么? I have no idea why is it working in such way. 我不知道为什么会这样工作。
Your regular expression is matching the first portion of the input string surrounded by double quotes and followed by a colon, so it hits "data"
before "first"
. 您的正则表达式匹配输入字符串的第一部分,双引号将其引起来,然后是冒号,因此它会在"first"
之前命中"data"
"first"
。
I would go for a regular expression of: 我会去找一个正则表达式:
'{\s*("\w+"):'
This will match the opening brace followed by some optional whitespace, then select the alphanumeric text in double quotes followed by a colon. 这将匹配左括号,然后是一些可选的空格,然后选择双引号中的字母数字文本,后跟冒号。
Also, you don't need to escape your double quotes as you are using a single quote as the delimiter for your regular expression. 同样,您不需要转义双引号,因为您将单引号用作正则表达式的定界符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.