Home > Enterprise >  Python - Adding quotes on a multi word phrase in a string
Python - Adding quotes on a multi word phrase in a string

Time:01-10

Suppose there is a string like follow:

'jon doe AND (james OR james david)'

What I want to do is to add double quotes around the multi word phrase like in jon doe and in james david as follow:

'"jon doe" AND (james OR "james david")'

I think it can be possible using regex but not sure how? As I am nooby in regex. I have tried writing my own python code without regex and was able to do so with string which do not have parenthesis. Like follow

'jon doe AND james OR james david' 

to

'"jon doe" AND james OR "james david"'

but not with the parenthesis. If anyone has done this before do let me know. Thanks

Edit 1: The method I have wrote is also not neat and clean and thus want a better solution also.

Edit 2: In case someone wants to see the code here it is. It does the job and successfully does what I want but it does not seems to be a nice way. And I am sure there are more good ways than this:

s = 'jon doe AND (james OR james david)'
new  = ''
new_list = []
splited = s.split()
for i in range(0, len(splited)):
    word = splited[i]
    if word in ['AND', 'OR', 'NOT']:
        if len(new_list) > 1:
            pharase = ' '.join(x for x in new_list)
            if pharase.endswith(")"):
                pharase = pharase.replace(")", '"')
                new  = '"{})'.format(pharase)
            elif pharase.startswith("("):
                pharase = pharase.replace("(", '"')
                new  = '({}"'.format(pharase)
            else:
                new  = '"{}"'.format(pharase)
            new  = " {} ".format(word)
        else:
            pharase = ' '.join(x for x in new_list)
            new  = '{}'.format(pharase)
            new  = " {} ".format(word)
        new_list = []
    elif i==len(splited)-1:
        new_list.append(word)
        pharase = ' '.join(x for x in new_list)
        if len(new_list) > 1:
            if pharase.endswith(")"):
                pharase = pharase.replace(")", '"')
                new  = '"{})'.format(pharase)
            elif pharase.startswith("("):
                pharase = pharase.replace("(", '"')
                new  = '({}"'.format(pharase)
            else:
                new  = '"{}"'.format(pharase)
        else:
            new  = '{}'.format(pharase)
    else:
        new_list.append(word)

Output: '"jon doe" AND (james OR "james david")'

CodePudding user response:

You can match two or more space-separated words that are not one of the keywords AND, OR and NOT:

((?=\S)(?:\s*(?!(?:AND|OR|NOT)\b)\b\w ){2,}\b)

And substitute the match with a quoted version:

"\1"

Regex demo: https://regex101.com/r/KB2Ogb/2

Python demo: https://replit.com/@blhsing/BeigeQuietMemoryallocator

  • Related