-2

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"

mozway
  • 194,879
  • 13
  • 39
  • 75
  • @TimBiegeleisen I don't think this is a duplicate. The linked question does not refer explicitly to regex, nor to trailing characters or case insensitive match – mozway Jan 27 '22 at 11:13
  • @mozway Sorry...I thought those uppercase Os were just typos. – Tim Biegeleisen Jan 27 '22 at 11:14
  • 1
    "How can I achieve this using regex?" Well, what do you know about regex? Can you think of a regex pattern that matches the part you want to replace? Do you know what you want to replace it with? Do you know how to do the replacement? *What actually is the difficulty*? – Karl Knechtel Jan 27 '22 at 11:14
  • Since you are creating a function `remove_trailing_os` you can get your result without using regex. Use `str.startswith`. eg: ` if input.startswith("no") return "no"` if the "no" is of different case in different inputs then use `re.search(r'^no', input_string, re.I)`. It will return a match object or return `None` . – anotherGatsby Jan 27 '22 at 11:15

2 Answers2

3

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
mozway
  • 194,879
  • 13
  • 39
  • 75
-1

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)]