简体   繁体   English

如果用引号引起来,如何删除字符串中的特定字符

[英]How to remove specific character in string if it is in quotation mark Python

I have a string: 我有一个字符串:

"Yes, We do. This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing."

What is the best way to remove dot mark (.) if it in between the quotation marks using Python? 如果使用Python删除点号(。)之间的引号,最好的方法是什么? the expected result is like: 预期的结果是:

"Yes, We do This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing"

Thank you 谢谢

You can capture all quotes "..." with regex (\\"[\\s\\S]*?\\") and handle the replace in a loop. 您可以使用正则表达式(\\"[\\s\\S]*?\\")捕获所有引号“ ...”并循环处理替换。 First replace the . 首先更换. and store into newquote . 并存储到newquote Then replace the old quote with the new one. 然后用新的报价替换旧的报价。

import re;

s = '"Yes, We do. This is a complex issue that deserves more than a one word answer," Zuckerberg said. Pallone called the response "disappointing."';

pattern = re.compile(r'(\"[\s\S]*?\")');

for (quote) in re.findall(pattern, s):
    newquote = quote.replace('.', '');
    s = s.replace(quote, newquote);

print s;

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

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