Skip to content Skip to sidebar Skip to footer

Replace A Substring Selectively Inside A String

I have a string like this: a = '\'java jobs in delhi\' delhi' I want to replace delhi with ''. But only delhi which lies outside the double-quotes. So, the output should look like

Solution 1:

Use re.sub

>>>a = "\"java jobs in delhi\" delhi">>>re.sub(r'\bdelhi\b(?=(?:"[^"]*"|[^"])*$)', r'', a)
'"java jobs in delhi" '
>>>re.sub(r'\bdelhi\b(?=(?:"[^"]*"|[^"])*$)', r'', a).strip()
'"java jobs in delhi"'

OR

>>> re.sub(r'("[^"]*")|delhi', lambda m: m.group(1) if m.group(1) else"", a)
'"java jobs in delhi" '>>> re.sub(r'("[^"]*")|delhi', lambda m: m.group(1) if m.group(1) else"", a).strip()
'"java jobs in delhi"'

Solution 2:

As a general way you can use re.split and a list comprehension :

>>>a = "\"java jobs in delhi\" delhi \"another text\" and this">>>sp=re.split(r'(\"[^"]*?\")',a)>>>''.join([i.replace('dehli','') if'"'in i else i for i in sp])
'"java jobs in delhi" delhi "another text" and this'

The re.split() function split your text based on sub-strings that has been surrounded with " :

['', '"java jobs in delhi"', ' delhi ', '"another text"', ' and this']

Then you can replace the dehli words which doesn't surrounded with 2 double quote!

Solution 3:

Here is another alternative. This is a generic solution to remove any unquoted text:

def only_quoted_text(text):
    output = []
    in_quotes=False

    for letter in a:
        if letter == '"':
            in_quotes = not in_quotes
            output.append(letter)
        elif in_quotes:
            output.append(letter)

    return"".join(output)  


a = "list of \"java jobs in delhi\" delhi and \" python jobs in mumbai \" mumbai"print only_quoted_text(a)

The output would be:

"java jobs in delhi"" python jobs in mumbai "

It also displays text if the final quote is missing.

Post a Comment for "Replace A Substring Selectively Inside A String"