Wednesday, 23 June 2021

How to add the characters between the strings with conditions

  • if %20 there in the string it has to replace with OR, abc %20 def. Expected out -- > '*abc* OR *def*'
  • if , there in the string it has to replace with OR, abc,def.: Expected out -- > '*abc* OR *def*'
  • string = 'abc def': Need to update beginning each string and end string with * replace space with OR.: Expected out --> '*abc* OR *def*'
  • string = 'abc or def', 'abc+def','abc + def', 'abc OR def': If OR,+ is in the string then we need to update.: Expected out --> '*abc* OR *def*'
  • string = 'abc&def','abc & def', 'abc and def' abc AND def': If AND,& is in the string then we need to update.: Expected out --> '*abc* AND *def*'
  • All the punctuations has to replace

Code is below

import re
def format_search_value(search_value_1):
    punctuations = '''!()[]{};:'"\,<>./?@#%^*~'''
    search_value_1 = search_value_1.replace('+', ' ')
    #if %20 there in the string it has to replace with OR, abc %20 def
    search_value_1 = re.sub('^(%20)+$', '%20', search_value_1)
    search_value = ""
    for char in search_value_1:
        if char not in punctuations:
            search_value = search_value + char
    search_expression = ','.join([f'*{word.strip()}*' for word in search_value.split(',')])
    search_expression = re.sub(' +', ' ', search_expression.replace('%20', ' '))
    search_expression = ','.join([f'*{word}*' for word in search_expression.split(' ')])
    search_parameter = search_expression.replace('%20', ' OR ').replace(',', ' OR ') \
        .replace('and', 'AND').replace('+', 'OR').replace('or', 'OR').strip()
    search_parameter = search_parameter.replace('**', '*')
    return search_parameter
format_search_value('abc or def')

I am getting proper output only for ('abc def') which is '*abc* OR *def*'



from How to add the characters between the strings with conditions

No comments:

Post a Comment