python使用正则表达式去除句子中的重复词
#python使用正则表达式去除句子中的重复词
# Python program to remove duplicate words# using Regular Expression or ReGex.import re# Function to validate the sentence# and remove the duplicate wordsdef removeDuplicateWords(input):# Regex to matching repeated wordsregex = r\(\w+)(?:\W+\1\b)+
eturn re.sub(regex, r\1, input, flags=re.IGNORECASE)# Driver Code# Test Case: 1str1 = "I am a big big girl in the big big world"print(removeDuplicateWords(str1))# Test Case: 2str2 = "old macdonald had a farm e i e i o"print(removeDuplicateWords(str2))# Test Case: 3str3 = "see ya ya"print(removeDuplicateWords(str3))# This code is contributed by yuvraj_chandra
I am a big girl in the