简体   繁体   English

Python 正则表达式忽略引号之间的内容

[英]Python regex to ignore what is between quotes

I have a string, part of which is surrounded within quotes.我有一个字符串,其中一部分用引号括起来。 Like the one at the third line of the code snippet below.就像下面代码片段第三行的那个。 I want the string to be formatted into a dict literal.我希望将字符串格式化为字典文字。 Meaning wherever the quotes are missing, they should be added.这意味着无论引号在哪里丢失,都应该添加它们。 But the part which is within the quotes has to be ignored.但是引号内的部分必须忽略。 I came up with the code below to handle this:我想出了下面的代码来处理这个问题:

from ast import literal_eval
from re import sub

str = "key1:[val1,val2,val3],key2:'val4A,val4B'"
str = sub(r"([\w\-\.]+|[\"'].*[\"'])", r"'\1'", f"{{{str}}}")
str = sub(r"[\"']{2,}(.*)[\"']{2,}", r"'\1'", str)
fin = literal_eval(str)
print(fin)

This code does the work, but I want to know if there is a way to achieve this with one time usage of sub.这段代码完成了工作,但我想知道是否有一种方法可以通过一次使用 sub 来实现。 Before you mark this as a duplicate, I tried a large number of the solutions provided on the web including positive and negative look ahead and look behind, exclusion, and simple negative match.在将其标记为重复之前,我尝试了 web 上提供的大量解决方案,包括正面和负面前瞻和后视、排除和简单的否定匹配。 Couldn't find any which would work.找不到任何可行的方法。 If there is a solution I have missed or anyone has a solutions, I would highly appreciate knowing about it.如果有我遗漏的解决方案或任何人有解决方案,我将不胜感激。

Try this ([\w\-\.]+(?=(?:[^']*'[^']*')*[^']*$)) :试试这个([\w\-\.]+(?=(?:[^']*'[^']*')*[^']*$))

Live Demo现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM