0

How would I use the python "re" module to replace a word, i.e., 'co' with an empty string, i.e., '' in a given text, only if:

  • the word is at the end of the text and there's a space before the word
  • the word is not the final word in the text but there's a space at beginning, and then another space at the end of the word

I.e.,

# word is not the final word in the text but there's a space at beginning, and then another space at the end of the word
txt = 'A co is mine'
txt_after_replace = 'A is mine'
txt = 'A column is mine'
txt_ater_replace = 'A column is mine'
# word is the end of the text and there's a space before the word
txt = 'my co'
txt_after_replace = 'my'
txt = 'my column'
txt_after_replace = 'my column'

If I do: txt.replace(' co', '') these two cases will fail: txt = 'my column', txt_ater_replace = 'A column is mine'. Since it won't check for the end of text right after the word or for a space in the text right after the word.

I think the re.sub module would come to the rescue here but I'm unsure how.

This should work for any general word, i.e., 'co' in this case.

4
  • I don't think you can match both criteria at the same time. A regex for the first would be \b\w+$ (which would slightly over-match if there's punctuation before the word) and the second one could use capture groups " (\w+) .*$" Commented Jan 11, 2018 at 0:07
  • @Beefster thanks for the help, it turns out to be possible based on answer I accepted. Commented Jan 11, 2018 at 0:15
  • 1
    I realized after I said that that you meant "either" of the conditions. Commented Jan 11, 2018 at 0:17
  • @Beefster get it, thanks, not clearer Q, I know, apologies for the english... ;-) Commented Jan 11, 2018 at 0:18

2 Answers 2

1

You can use alternation to match both criteria using following regex.

Regex: (?:\sco\s|\sco$)

Explanation:

  • \sco\s matches co preceded and succeed by a space.

  • \sco$ matches co at end preceded by a space.

Regex101 Demo

In python:

import re
str = "coworker in my company are not so co operative. silly co"
res = re.sub(r'(?:\sco\s|\sco$)', ' ', str)
print(res)

Result: coworker in my company are not so operative. silly

Ideone Demo

Sign up to request clarification or add additional context in comments.

6 Comments

or even better (\s?\bco\b\s?)
@sKwa that one seems to fail in this case: re.sub('(\s?\bco\b\s?)', ' ', 'my co sa')
@sKwa: Optional space will skip even if it's present.
@Rahul lol "coworker in my company are not so co operative. silly co"
@Dnaiel, I used in Rahul's feedle and it works in feedle O_o
|
0

you can use lookahead regex

\sco(?=$|\s)

explanation:

  • space follow by co, then assert what follow by co must be either space or end of text

python code

import re
txt = 'A co is mine, A column is mine, my column, my co'
new_txt = re.sub('\sco(?=$|\s)', '', txt)
# 'A is mine, A column is mine, my column, my'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.