我正在开发一个正则表达式来解析JSON响应中的值,但是我在一个字段上遇到了麻烦,因为它包含人工编写的文本,因此,由于内容可能会变化并破坏正则表达式,因此我需要一个正则表达式将匹配以下所有潜在值:
, 'resolve_comment': "This value's comment contains an apostrophe / single quotation mark so it will be automatically enclosed in double quotation marks",
, 'resolve_comment': 'Some comments, like this one, contain a comma. If there is no comment then there will be no quotation marks as you can see below.',
, 'resolve_comment': None,
我在网上找不到的任何正则表达式都可以在所有这三种情况下使用。
我最接近的是:
'resolve_comment': (?:(?:"(?P<resolve_comment>[^"]*)")|(?:'(?P<resolve_comment>[^']*)')|(?P<resolve_comment>None)),
,但系统不允许重复的捕获组名称。'resolve_comment': (?:(?:"([^"]*)")|(?:'([^']*)')|(None)),
,但创建了3个捕获组,其中只有一个捕获组。'resolve_comment': ["|']?(.*)["|']?,
,但留下的引号不理想。答案 0 :(得分:0)
这是我的正则表达式,它与您的所有潜在值匹配,请尝试以下操作:
(?<=").*(?=")|(?<=').*(?=')|None
所以我匹配“”和“”之间的所有内容,也匹配值“无”。 它与“”之类的字符串不匹配,认为此处无效。 在这里查看:https://regex101.com/r/yV9GES/1
告诉我您是否正在寻找其他东西?