Python Regex Sub:将字典与Regex表达式配合使用 [英] Python Regex Sub: Using Dictionary with Regex Expressions

查看:151
本文介绍了Python Regex Sub:将字典与Regex表达式配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用包含正则表达式的字典来替换不同字符串的部分,如 @ roippi提出的前一个SO问题。第一个 re.sub表达效果很好。但是,每当我的代码实际涉及到正则表达式(第二个 re.sub表达式)时,替换就不起作用。

I am using a dictionary that contain regular expressions to substitute portions of different strings, as elegantly described in a previous SO question by @roippi. The first 're.sub' expression works perfectly. However, whenever my code actually involves regex expressions (the second 're.sub' expression), the substitutions don't work.

我很困惑为什么这样做是这种情况。我尝试过使用和删除'r'以及合并超前/后向表达式,但似乎没有任何效果。

I am very confused as to why this is the case. I have tried both using and taking out the 'r' as well as incorporating the lookahead/lookbehind expressions, nothing seems to work. Any help would be greatly appreciated!

test_dict = {r'(\d+)': 'THIS IS A NUMBER', 'john_doe':'THIS IS A NAME'}

re.sub('(john_doe)', lambda x: test_dict.get(x.group(1),x.group(1)),'john_doe_jr')

re.sub(r'(\d+)', lambda x: test_dict.get(x.group(1), x.group(1)), '999la')


推荐答案

match.group (n)不返回用于匹配第n个组的正则表达式,而是第n个组本身。

match.group(n) does not return the regular expression that was used to match the nth group, but the nth group itself.

因此,lambda返回 test_dict.get('999','999'),它返回'999',因为'999'不是词典中的键。

The lambda therefore returns test_dict.get('999', '999'), which returns '999', because '999' is not a key in your dictionary.

您可以遍历词典的键并检查是否有键匹配您的捕获组,然后然后替换它,但是时间复杂度为O(n)(以字典的大小为单位)。

You could iterate over the keys of the dictionary and check if any key matches your capture group, and then replace it, but that has O(n) time complexity (in the size of the dictionary).

def replacement(match, d, group=1):
    for key in d:
        if re.match(key, match.group(group)):
            return d[key]
    return match.group(group)

re.sub(r'(\d+)', lambda x: replacement(x, test_dict), '999la')

这篇关于Python Regex Sub:将字典与Regex表达式配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆