'Removing repeated trailing characters from a string in Python

I have a field with comments. Some of the comments are just "no" but with varying trailing "o"s. I want to do a transformation to these comments, such that I only get "no" returned. How can I achieve this using regex?

Eg:

remove_trailing_os("noooooo") should output "no"

remove_trailing_os("nooOOoooooooo") should output "no"



Solution 1:[1]

You could use a case insensitive backreference:

import re
re.sub(r'(.)(?i:\1)+$', r'\1', "nooOOoooooooo", re.I)

output: 'no'

regex:

(.)        # match a character
(?i:\1)+$  # match trailing case insensitive repeats of the character

Solution 2:[2]

This seems similar to how can I remove all characters after the second occurrence of a ' ' (space)

but essentially you want to replace space with o. Hence

## Assuming the two instances
t = 'noooooo'
t2 = 'nooOOoooooooo'
## Trying them on the two instances
t[:t.find('o',t.find('o')+1)]
t2[:t2.find('o',t2.find('o')+1)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway
Solution 2 Denis Mwaniki