如何使用正则表达式将所有重复的标点符号替换为单个标点符号?

时间:2019-07-09 22:54:06

标签: python regex python-3.x

我想更改将文本中所有重复的(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

2 个答案:

答案 0 :(得分:1)

修改@Emma的代码

def duplicate_punctuation(text):
    pattern_regex = "([?@])\\1+"
    regex_req = re.sub(pattern_regex, "\\1", text)
    return regex_req

LINK

中的参考代码

答案 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)