简体   繁体   English

Python - 删除括号和引号

[英]Python - Removing parentheses and quotation marks

I've been trying to remove pair parentheses(including text between them), unbalanced parentheses and quotation marks from the string.我一直在尝试从字符串中删除对括号(包括它们之间的文本)、不平衡的括号和引号。

What I've done so far:到目前为止我所做的:

import re

sample_text = '""sads"add"sfsfdsfds()()(0sefdAAAsfs)dasdad(asd'
res = re.sub(r'\([^)]*\)', '', sample_text))

It matches with only ()()(0sefdAAAsfs) part of the text.它仅与文本的()()(0sefdAAAsfs)部分匹配。 Unbalanced and quotation marks left unmatched.不平衡和引号不匹配。 What can be done to improve above regex?可以做些什么来改进上述正则表达式?

This isn't really something that a regular expression is suited for, so is not the right tool for the job.这不是正则表达式真正适合的东西,因此不是适合这项工作的工具。 Having said that, you can use the following pattern to see if there is an opening paren, then zero or more non-paren, and a matching closing paren:话虽如此,您可以使用以下模式来查看是否有一个开始括号,然后是零个或多个非括号,以及一个匹配的结束括号:

\([^)+]*\)

Substitute a " or ' or [ or whatever for the other types of matching components."'[或任何其他类型的匹配组件替换。

But again, this would not work with something like this:但同样,这不适用于这样的事情:

(asdf))))))))

在此处输入图像描述

Long story short: it's not a problem that a regular expression is capable of solving.长话短说:这不是正则表达式能够解决的问题。 Try testing it out here: https://regex101.com/r/bdiK5W/2 .尝试在这里测试它: https://regex101.com/r/bdiK5W/2

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

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