-1

I want to replace '/' with whitespace in a string unless if it's between 'mg' and 'number' 'ABACAVIR/LAMIVUDINE/ZIDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé'

Becomes

'ABACAVIR LAMIVUDINE ZIDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé'

and how can i do it with an array of multiple lines

['ABACAVIR/LAMIVUDINE TEVA 600 mg/300 mg, comprimé pelliculé' 'ABACAVIR/LAMIVUDINE ZENTIVA 600 mg/300 mg, comprimé pelliculé']

3 Answers3

6

Use a regex pattern for your conditions. You can use the following constructs:

  • A negative lookbehind asserts that what precedes the slash is not mg
  • A negative lookahead asserts that what follows is not a digit.

import re
my_string = 'ABACAVIR/LAMIVUDINE/ZIDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé'
print(re.sub('(?<!\bmg\b)\/(?![0-9])',' ',my_string))
#ABACAVIR LAMIVUDINE ZIDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé
Paolo
  • 21,270
  • 6
  • 38
  • 69
3

I would like to suggest a solution simple and readable also for beginners and that doesn't use any import module.

In my code I simply parse the string character by character with a for loop and check if the current character is "/".

I use enumerate because it allows me to access both the index and the value of the current character during the iteration.

In case "/" is found I use Python's slicing to check if "mg" is coming before it and use this information to replace it with whitespace or not.

Solution

data = ['ABACAVIR/LAMIVUDINE TEVA 600 mg/300 mg, comprimé pelliculé' 'ABACAVIR/LAMIVUDINE ZENTIVA 600 mg/300 mg, comprimé pelliculé']
new_data = [] 

for line in data:
    result = "" 
    for key, value in enumerate(data):
        if value == "/" and data[key-2:key] != "mg":
            result += ' '  # replace with white space
        else:
            result += value
    new_data.append(result)

print(new_data)

Output

['ABACAVIR/LAMIVUDINE TEVA 600 mg/300 mg, comprimé pelliculéABACAVIR/LAMIVUDINE ZENTIVA 600 mg/300 mg, comprimé pelliculé']
Pitto
  • 8,229
  • 3
  • 42
  • 51
2

You can find all / occurrences except mg/number and then replace them:

import re

string = 'ABACAVIR/LAMIVUDINE/ZIDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé'
result = re.sub('\/[^mg\/\d+]', ' ', string)

Output:

ABACAVIR AMIVUDINE IDOVUDINE MYLAN 300 mg/150 mg/300 mg, comprimé pelliculé
Alderven
  • 7,569
  • 5
  • 26
  • 38