繁体   English   中英

在 python 中使用正则表达式删除带有排除项的双引号

[英]Using regex in python to remove double quotes with exclusions

我正在尝试使用 python 中的正则表达式从文本中删除特定的双引号。 我只想留下那些表示英寸的双引号。 所以这意味着在数字后面留下任何双引号。

txt = 'measurement 1/2" and 3" "remove" end" a " multiple"""

预期的 output: measurement 1/2" and 3" remove end a multiple

这是我得到的最接近的。

re.sub(r'[^(?,\d+/\d+")]"+', '', txt)

只需使用

(?<!\d)"+

请参阅regex101.com 上的演示


你原来的表情

[^(?!\d+/\d+")]

基本上意味着不是( , ? , !等。


或者,您可以将较新的regex模块与(*SKIP)(*FAIL)一起使用:

measurement 1/2" and 3" remove end a  multiple
ABC2DEF3

这会产生

measurement 1/2" and 3" remove end a multiple ABC2DEF3

暂无
暂无

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

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