RegEx用于替换引号之间的特定单词

时间:2019-05-16 00:12:50

标签: python regex string regex-negation regex-lookarounds

如果字符串Hello中的s不在引号之间,例如“”或'',我正尝试用另一个单词替换。让我们假装替换词是Matt,

这是输入:

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

所需的输出:

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

我四处搜寻并遇到了这段代码,几乎没有修改,我设法成功替换了该词,但它只能与''一起使用,而不能与“”一起使用

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)

编辑:

感谢您的回答,但我错过了解释一些重要的事情(在执行成功的代码后,我首先为自己辩护的意思),“显然”,我不需要这句话:

s = "Hellona, how are you"

成为

s = "Markna, how are you"

因此,正则表达式应包括我要替换的单词不会被 NUMBERS LETTERS 所替代。

3 个答案:

答案 0 :(得分:3)

替换回调看起来不错。

不过正则表达式必须是

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

可读版本

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

请注意,我认为第1组在回调中签入
如果第1组匹配,则必须为true。

不是Python程序员,但应该是

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

答案 1 :(得分:1)

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)

您可以匹配组1(('[^']*'|\"[^\"]*\"))中单引号或双引号之间的所有内容,然后匹配组2({},以search格式设置的单词),然后替换组2与您想要的任何东西。

答案 2 :(得分:1)

在这里,我们也许可以通过以下方法解决此问题:

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

我们可以将其替换为:

enter image description here

RegEx

如果不需要此表达式,则可以在regex101.com中修改/更改表达式。

RegEx电路

您还可以在jex.im中可视化您的表达式:

enter image description here

JavaScript演示

此代码段表明我们可能有一个有效的表达式:

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测试

# 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.

要排除Hellona,我们可以添加单词边界:

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

enter image description here

演示

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);