简体   繁体   中英

RegEx for replacing specific words not between quotation marks

I'm trying to replace Hello in the string s with another word if the word is NOT between quotations marks such as " " or ' '. Let us pretend the replacement word is Matt so,

This is the input:

s = 'Hello How Are you, "hey Hello", \'ney Hello\'. Hello I\'m great'

Desired output:

s = 'Matt How are you, "hey Hello", \'ney Hello\'. Matt I\'m great '

I have searched around and come across this code and with little modification I manage to replace the word successfully but it only works with ' ' and not " " included

import re

def replace_method(match):

    if match.group(1) is None:
        return match.group()

    return match.group().replace("Hello", "Matt")

s = 'Hello How Are you, "hey Hello", \'ney Hello\'. Hello I\'m great'

output = re.sub(r"'[^']*'|([^']*)", replace_method, s)
print(output)

Edit:

Thanks for the answers, but I missed to explain something important (which I first noticed, in my defense, after executing the successful code), "obviously" I don't want this sentence:

s = "Hellona, how are you"

to become

s = "Markna, how are you"

So, the regex should include that the word I'm trying to replace is not surronded by NUMBERS or LETTERS .

The replacement callback looks fine.

The regex though, needs to be this

r"('[^']*'|\\"[^\\"]*\\")|\\b[Hh]ello\\b"

Readable version

   (                             # (1 start)
        ' [^']* '
     |  
        " [^"]* "
   )                             # (1 end)
|  
   \b [Hh]ello \b

Note that I think the group 1 check in the callback
has to be true if group 1 matched.

Not a Python programmer, but should it be something like

if match.group(1) :
    return match.group()
return "Matt"
import re


def replace_word(input, search, replace):
    def replace_method(match):
        if match.group(2) is None:
            return match.group()
        return match.group(2).replace(search, replace)
    expr = re.compile("('[^']*'|\"[^\"]*\")|({})".format(search))
    return re.sub(expr, replace_method, s)

s = 'Hello How Are you, "hey Hello", \'ney Hello\'. Hello I\'m great'

output = replace_word(s, "Hello", "Matt")
print(output)

You can match everything between single or double quotes in group 1( ('[^']*'|\\"[^\\"]*\\") ), then your word in group 2 ( {} , formatted with the search term), then replace group 2 with whatever you want.

Here, we might be able to solve this problem with:

([^'"]?)(Hello)([^'"])

which we can replace it with:

在此输入图像描述

RegEx

If this expression wasn't desired, you can modify/change your expressions in regex101.com .

RegEx Circuit

You can also visualize your expressions in jex.im :

在此输入图像描述

JavaScript Demo

This snippet shows that we might likely have a valid expression:

 const regex = /([^'"]?)(Hello)([^'"])/gm; const str = `Hello How Are you, "hey Hello", 'ney Hello'. Hello I'm great. "Hello' I'm great`; const subst = `$1Matt$3`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

Python Test

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"([^'\"]?)(Hello)([^'\"])"

test_str = "Hello How Are you, \"hey Hello\", 'ney Hello'. Hello I'm great. \"Hello' I'm great"

subst = "\1Matt\3"

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

For excluding Hellona , we can add a word boundary:

([^'"]?)(\bHello\b)([^'"])

在此输入图像描述

Demo

 const regex = /([^'"]?)(\\bHello\\b)([^'"])/gm; const str = `Hello How Are you, "hey Hello", 'ney Hello'. Hello I'm great. "Hello' I'm great. Hellona how are you? `; const subst = `$1Matt$3`; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log('Substitution result: ', result); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM