简体   繁体   English

Python-查找子字符串,然后替换其中的所有字符

[英]Python- find substring and then replace all characters within it

Let's say I have this string : 假设我有这个字符串:

<div>Object</div><img src=#/><p> In order to be successful...</p>

I want to substitute every letter between < and > with a # . 我想用#代替<>之间的每个字母。

So, after some operation, I want my string to look like: 因此,经过一些操作,我希望我的字符串看起来像:

<###>Object<####><##########><#> In order to be successful...<##>

Notice that every character between the two symbols were replaced with # ( including whitespace). 请注意,两个符号之间的每个字符都被替换为# (包括空格)。

This is the closest I could get: 这是我能得到的最接近的:

   r = re.sub('<.*?>', '<#>', string)

The problem with my code is that all characters between < and > are replaced by a single # , whereas I would like every individual character to be replaced by a # . 我的代码的问题是<>之间的所有字符都被单个#代替,而我希望每个单独的字符都被#代替。

I tried a mixture of various back references, but to no avail. 我尝试了各种反向引用的混合,但无济于事。 Could someone point me in the right direction? 有人可以指出我正确的方向吗?

What about...: 关于什么...:

def hashes(mo):
    replacing = mo.group(1)
    return '<{}>'.format('#' * len(replacing))

and then 接着

r = re.sub(r'<(.*?)>', hashes, string)

The ability to use a function as the second argument to re.sub gives you huge flexibility in building up your substitutions (and, as usual, a named def results in much more readable code than any cramped lambda -- you can use meaningful names, normal layouts, etc, etc). 使用函数作为re.sub的第二个参数的功能为您建立替换提供了极大的灵活性(通常,命名的def比任何狭窄的lambda可读得多的代码-您可以使用有意义的名称,常规布局等)。

The re.sub function can be called with a function as the replacement, rather than a new string. re.sub函数可以用替换函数而不是新字符串来调用。 Each time the pattern is matched, the function will be called with a match object, just like you'd get using re.search or re.finditer . 每次匹配模式时,都会使用一个match对象调用该函数,就像使用re.searchre.finditer

So try this: 所以试试这个:

re.sub(r'<(.*?)>', lambda m: "<{}>".format("#" * len(m.group(1))), string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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