2

Column 1 in my csv has the target_string (to be replaced) and column 2 is the context (text_column). I can't get it to only remove the string when it is stand alone. I have tried adding \b, but that doesn't work.

Text : "Foo, FOOBAR FOO FOOBAR FOO FOO FOOBAR."

string_column = col [1]
text_column = col [2]
string_replacement = "BAR"

text_column = re.sub(string_column, string_replacement, text_column, flags=re.IGNORECASE)

What I get is : "BARBAR BAR BARBAR BAR BAR BARBAR."

What I need is: "FOOBAR BAR FOOBAR BAR BAR FOOBAR."

EDIT I don't think this is a duplicate of the linked question. Those responses wouldn't have helped me, and are far more complicated than the simple solution provided here.

chrissmit.yam90
  • 101
  • 1
  • 8

1 Answers1

3

You (probably) need \b on both sides:

import re

string = "Foo, FOOBAR FOO FOOBAR FOO FOO FOOBAR."

your_string_here = "FOO"
string_column = r"\b{}\b".format(your_string_here)
string_replacement = "BAR"

string = re.sub(string_column, 'BAR', string)
print(string)

This yields

Foo, FOOBAR BAR FOOBAR BAR BAR FOOBAR.
Jan
  • 42,290
  • 8
  • 54
  • 79