Home > Software design >  Removing all r's before consonants or at the end of words (non-rhotic) in python using regex re
Removing all r's before consonants or at the end of words (non-rhotic) in python using regex re

Time:08-22

I created this function to remove all r's that meet this specific pattern but I run into a bug if the last word ends in an r and doesn't have punctuation.

def remove_r_before_consonant_or_at_end(sentence):
    without_r = re.sub(r'r([bcdfgjklmnopqstvxz\s\?\.\;\,\!\:])', '\\1', sentence)
    return without_r

r_sentence = "karl was hard to card at the far bar"

print(remove_r_before_consonant_or_at_end(r_sentence))

output: kal was had to cad at the fa bar
# so the final bar should be ba

From my research it seems that I have to use $ but I can't get it to work. Thanks for the help in advance. Also if there is a way to do this without regex I am fine with that.

CodePudding user response:

To match the terminating character, you need to add the end-of-line (or end-of-string) assertion ($) as an alternative (|), not part of the list:

r([bcdfgjklmnopqstvxz\s\?\.\;\,\!\:]|$)
#                         additions ^^

demo

  • Related