我想更改将文本中所有重复的(2+)标点字符替换为单个字符:
text = "-alham sedang bermain-main dan makan-makan???? @@alhamsya"
result = "-alham sedang bermain-main dan makan-makan? @alhamsya"
这是我的代码(和here it is on regex reference tester):
def duplicate_punctuation(text):
pattern_regex = "([?@])\\1+"
regex_req = re.sub(pattern_regex, "\\1", text)
return regex_req
答案 0 :(得分:1)
修改@Emma的代码
def duplicate_punctuation(text):
pattern_regex = "([?@])\\1+"
regex_req = re.sub(pattern_regex, "\\1", text)
return regex_req
中的参考代码
答案 1 :(得分:-1)
您的表情似乎运作良好:
import re
regex = r"([?@])\1+"
test_str = "-alham sedang bermain-main dan makan-makan???? @@alhamsya"
subst = "\\1"
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)