我与Python和正则表达式的工作括号(还有其他括号)任何东西,我想变换像下面的字符串:的Python:正则表达式匹配里面
(1694439,805577453641105408,'\"@Bessemerband not reverse gear simply pointing out that I didn\'t say what you claim I said. I will absolutely riot if (Brexit) is blocked.\"',2887640,NULL,NULL,NULL),(1649240,805577446758158336,'\"Ugh FFS the people you use to look up to fail to use critical thinking. Smh. He did the same thing with brexit :(\"',2911510,NULL,NULL,NULL),
到像下面的列表:
[
[1694439, 805577453641105408, '\"@Bessemerband not reverse gear simply pointing out that I didn\'t say what you claim I said. I will absolutely riot if (Brexit) is blocked.\"', 2887640, NULL, NULL, NULL],
[1649240, 805577446758158336, '\"Ugh FFS the people you use to look up to fail to use critical thinking. Smh. He did the same thing with brexit :(\"', 2911510, NULL, NULL, NULL]
]
这里的主要问题在于,正如您所看到的,文本内部还有一些圆括号,我不想分割。 我已经尝试过\([^)]+\)
之类的东西,但很明显,它会在第一个)
发现它。
任何线索如何解决这个问题?
这不是正则表达式的设计目的。虽然有扩展名允许平衡括号,但没有这些扩展名,*抽象引理*指定了一个正则表达式不能这样做。 –
正则表达式无法计数,因此它们无法使用匹配的引号和括号提取文本。你需要一个解析器。请参阅PLY,PyParsing,Lark等。 – phd
尽管正如@WillemVanOnsem所述,正则表达式并不是为此设计的,但如果您知道第一列始终是一堆数字,您可以将其用作定位点。看看[正则表达式Lookbehind](http://www.rexegg.com/regex-lookarounds.html) – EndermanAPM