python - 基于匹配对象的字符串替换 (Python)

标签 python regex python-3.x

我很难理解 Python 正则表达式库中的 group 方法。在这种情况下,我尝试根据匹配对象对字符串进行替换。

也就是说,我想用 my_dict 中的特定字符串替换匹配的对象(本例中的 +\n)字典(分别带有 rep1rep2)。

由此可见questionanswer , 我已经尝试过:

content = '''
Blah - blah \n blah * blah + blah.
'''

regex = r'[+\-*/]'

for mobj in re.finditer(regex, content):
    t = mobj.lastgroup
    v = mobj.group(t)

    new_content = re.sub(regex, repl_func(mobj), content)

def repl_func(mobj):
    my_dict = { '+': 'rep1', '\n': 'rep2'}
    try:
        match = mobj.group(0)
    except AttributeError:
        match = ''
    else:
        return my_dict.get(match, '')

print(new_content)

但是在计算 v 时,我得到 tNone,后跟 IndexError

任何解释和示例代码将不胜感激。

最佳答案

尽管 Wiktor 的答案确实是 Python 式的,但仍然存在一个问题,为什么 OP 的原始算法不起作用。 基本上有两个问题:

调用 new_content = re.sub(regex, repl_func(mobj), content) 会将 regex所有匹配替换为第一个匹配的替换值

正确的调用必须是new_content = re.sub(regex, repl_func, content)。 据记录here , repl_func 使用当前匹配对象动态调用!

repl_func(mobj) 做了一些不必要的异常处理,可以简化:

my_dict = {'\n': '', '+':'rep1', '*':'rep2', '/':'rep3', '-':'rep4'}
def repl_func(mobj):
    global my_dict
    return my_dict.get(mobj.group(0), '')

这相当于 Wiktor 的解决方案 - 他只是通过使用 lambda 表达式摆脱了函数定义本身。

通过此修改,for mobj in re.finditer(regex, content): 循环已变得多余,因为它多次执行相同的计算。

为了完整起见,这里提供了一个使用 re.finditer() 的可行解决方案。它根据 content匹配切片构建结果字符串:

my_regx = r'[\n+*/-]'
my_dict = {'\n': '', '+':'rep1'     , '*':'rep2', '/':'rep3', '-':'rep4'}
content = "A*B+C-D/E"
res = ""
cbeg = 0
for mobj in re.finditer(my_regx, content):
    # get matched string and its slice indexes
    mstr = mobj.group(0)
    mbeg = mobj.start()
    mend = mobj.end()

    # replace matched string
    mrep = my_dict.get(mstr, '')

    # append non-matched part of content plus replacement
    res += content[cbeg:mbeg] + mrep

    # set new start index of remaining slice
    cbeg = mend

# finally add remaining non-matched slice
res += content[cbeg:]
print (res)

关于python - 基于匹配对象的字符串替换 (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40789447/

相关文章:

Python 2/3 subprocess.Popen和非ascii字符

python:从二维网格中无替换地采样

python - 如何在Python中从json填充QTableWidget

Java:从 URL 获取整数值

python - Python 中的正则表达式未捕获所有信息

python - 使用 Python 替换或交换文件中的子字符串

javascript - 将 Javascript 正则表达式转换为 PHP 正则表达式?

python - 如何在 Pandas 数据框中以分组格式创建字典?

python - 试图在数据集中找到最优价格点

python - 如何通过命令行将多个列表参数传递给多个pytest装置