Replace substring selectively inside string

I have a line 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 this:

"\"java jobs in delhi\""

A string is an example string. The substring does not have to be "delhi". A substring to replace can occur anywhere in the input string. The order and number of quoted and unquoted parts in a line is not fixed.

.replace()replaces both delhi substrings. I can’t use rstrip, because it doesn’t necessarily appear at the end of the line. How can i do this?

+4
source share
3 answers

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"'
+3
source

re.split :

>>> 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'

re.split() , ":

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

dehli, !

0

Here is another alternative. This is a general 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 conclusion will be:

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

It also displays text if the last quote is missing.

0
source

All Articles