0

Have a string,

'ANNA BOUGHT AN APPLE AND A BANANA'

and want to replace 'AN' and get

'ANNA BOUGHT X APPLE AND A BANANA'

but simple code:

text.replace('AN', 'X') 

returns:

XNA BOUGHT X APPLE XD A BXXA

How to make it work?

yatu
  • 86,083
  • 12
  • 84
  • 139
Mark
  • 1
  • Does this answer your question? [Do regular expressions from the re module support word boundaries (\b)?](https://stackoverflow.com/questions/3995034/do-regular-expressions-from-the-re-module-support-word-boundaries-b) – sushanth Sep 04 '20 at 11:16

6 Answers6

1

This code works for every case (begging/middle/end of the string, with or without punctuation marks):

import re

your_string = 'AN ANNA BOUGHT AN APPLE AND A BANANA AN'
replaced_strig = re.sub(r'\bAN\b', 'X', your_string)
maciejwww
  • 1,067
  • 1
  • 13
  • 26
0

If you want to search for the word AN, you should use text.replace(' AN ', ' X ') with the spaces. That way you'll be replacing only the word and avoiding other occurrences

JPery
  • 69
  • 6
  • That will fail if the string is for example `'AN APPLE'` with no space at the start. – alani Sep 04 '20 at 11:22
  • 1
    In this case it works but if 'AN' was at the beggining or the end of the string, it wouldn't be replaced. – maciejwww Sep 04 '20 at 11:25
0

Let string = ANNA BOUGHT AN APPLE AND A BANANA

Then myList = string.split(' ')

It will return myList = ['ANNA', 'BOUGHT', 'AN', 'APPLE', 'AND', 'A', 'BANANA']

Then you can do the following

myList[myList.index('AN')] = 'X'

In case multiple 'AN' is present, we can do the following

for i in range(len(myList)):

    if myList[i] == 'AN':

        myList[i] =  'X'
  • What if there is not exactly one occurrence of `'AN'` (zero or more than one)? – alani Sep 04 '20 at 11:22
  • In that case, we can traverse over the whole list, replacing each `AN` with `X` –  Sep 04 '20 at 11:32
0

You can use regular expressions - note the use of \b for word boundaries:

import re
line = 'ANNA BOUGHT AN APPLE AND A BANANA'
print(re.sub(r'\bAN\b', 'X', line))

or a solution without regular expressions (does not preserve the exact amount of whitespace between words, and may not be exactly equivalent if there is punctuation also):

line = 'ANNA BOUGHT AN APPLE AND A BANANA'

print(' '.join('X' if word == 'AN' else word
               for word in line.split()))
alani
  • 12,573
  • 2
  • 13
  • 23
0

regex is the best way to have such manipulation and even more complex ones, it is a bit intimidating to learn, but once you are done with it it gets really easy

import re
text = 'ANNA BOUGHT AN APPLE AND A BANANA'
pattern = r'(AN)'
new = re.sub(pattern,'X',text)
print(new)
0

regex is the way - with lookahead and lookbehind

import re
line = 'AN ANNA BOUGHT AN APPLE AND A BANANA AN. AN'
pattern='((?<=^)|(?<=\W))AN(?=\W|$)'
p = re.compile(pattern)
print(p.sub('X', line))

input: AN ANNA BOUGHT AN APPLE AND A BANANA AN. AN
result: X ANNA BOUGHT X APPLE AND A BANANA X. X

Lutz
  • 31
  • 5