【发布时间】:2021-10-08 18:08:35
【问题描述】:
我使用的是 Python 3.7。我需要一个正则表达式来查找双引号内“坏值”的开始和停止位置。我的 JSON 看起来与以下内容几乎相同。
[
{
"key": "test1",
"value": "<a href="http://something.com">something.com</a>",
},
{
"key": "test2",
"value": [
"i have some "double" quotes",
"<div class="test">some html may exists as well</div>"
]
},
// repeats arbitrary number of times
]
如您所见,问题存在(据我所知)只有value 键的值。此外,value 键的值可以是字符串或字符串数组。如果我在值中转义双引号,我可以使用 Python 的 json 模块将数据/字符串加载为字典,这是我的目标。
感谢任何有关使用正则表达式或任何其他解决方案来跟踪开始和停止双引号,然后转义其中的双引号的提示。
例如,我想要一个正则表达式来查找以下值
"<a href="http://something.com">something.com</a>""i have some "double" quotes""<div class="test">some html may exists as well</div>"
并将它们替换如下
"<a href=\"http://something.com\">something.com</a>""i have some \"double\" quotes""<div class=\"test\">some html may exists as well</div>"
【问题讨论】:
-
我认为 JSON 解析可以使用 PDA 完成,其中正则表达式与 FSA 兼容。因此,你不能,或者至少你不应该……
-
@KemalKaplan PDA 和 FSA 是什么?
-
它们是下推自动机与有限状态自动机。有一个不错的 wiki 页面。 en.wikipedia.org/wiki/Chomsky_hierarchy
标签: python json regex escaping