从
string= this is, not good "type of ,question" to ask, on stackoverflow
我希望提取"type of, question"
子字符串并用' '
替换','
。
使用re.findall()
可以得到" "
之间的字符列表,而使用re.search()
则会返回类对象。
使用re.sub()
可以替换所有','
,但我需要保留那些位于双引号子串内的逗号。
有谁能帮我解决这个问题呢?
先行感谢!!
使用正则表达式捕获组:
import re
s= 'this is, not good "type of ,question" to ask, on stackoverflow'
re.sub(r'(".*?),(.*?")', r'\1\2', s)
输出:
'this is, not good "type of question" to ask, on stackoverflow'
说明:在正则表达式中,(stuff)
代表捕获组,分别用 \1
和 \2
来替换字符串中引号内逗号前后的部分。请注意,这也适用于单个字符串中的多个引号。
\1
和\2
之间添加一个空格。 - Daniel F'this is, not good", "type of ,question"
将产生this is, not good" "type of ,question
。 - Daniel F另一种更灵活的方法是可以分两步完成:
查找所有被引号包含的匹配项,
在每个匹配中查找并替换 ','
。
例子:
# define a pattern that gets you everything inside a double quote
pat = re.compile(r'"[^"]+"')
# re.sub the quote pattern and replace the , in each of those matches.
string = pat.sub(lambda x: x.group(0).replace(',',''), string)
# 'this is, not good "type of question" to ask, on stackoverflow'
这个方法的灵活性在于它允许您替换任意数量的','
,并且一旦找到所有双引号模式,您还可以执行其他更改。
split()
和 replace()
?s = 'this is, not good "type of ,question" to ask, on stackoverflow'
splitted = s.split('"')
print(s.replace(splitted[1], splitted[1].replace(',', '')))
# this is, not good "type of question" to ask, on stackoverflow
b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """
for str_match in re.findall(r"\".*?\"",b):
b = re.sub(str_match,re.sub(r","," ",str_match),b)
print(b)
我不完全确定这是否符合您的所有要求,但是在您提供的模板上,以下内容将返回您所需的内容。
result = re.sub('("(?:[^"])*),((?:[^"])*")', r"\1 \2")
re.findall
,re.search
和re.sub
了,是吗?请分享每个尝试的代码。 - Kevinsub_string = re.search(r'\""(.*?)\""', new_li1) print(sub_string)
- dumb_coderresult ='这是在stackoverflow上问的不好的“问题类型”'
。如果您认为“非常有趣,我实际上需要它能够处理各种输入”,那么这正是我询问这些澄清问题的原因 :-) - Kevin