用相同长度的字符串替换正则表达式 [英] Replacing a RegEx with a string of characters with the same length

查看:63
本文介绍了用相同长度的字符串替换正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一系列重复字符替换 XML 标记,这些字符与标记的字符数相同.

例如:

2013-01-21T21:15:00Z

我想将其替换为:

#############2013-01-21T21:15:00Z##############

我们如何为此使用 RegEx?

解决方案

re.sub 接受一个函数作为替代:

<块引用>

re.sub(pattern, repl, string, count=0, flags=0)

如果 repl 是一个函数,它会在每次不重叠的模式出现时被调用.该函数采用单个匹配对象参数,并返回替换字符串.

这是一个例子:

在[1]中:导入re在 [2]: def repl(m):...: 返回 '#' * len(m.group())...:在 [3] 中:re.sub(r'<[^<>]*?>', repl,...: '<o:LastSaved>2013-01-21T21:15:00Z</o:LastSaved>')输出[3]:'#############2013-01-21T21:15:00Z##############'

我使用的模式可能需要一些改进,我不确定匹配 XML 标签的规范解决方案是什么.但你懂的.

I want to replace XML tags, with a sequence of repeated characters that has the same number of characters of the tag.

For example:

<o:LastSaved>2013-01-21T21:15:00Z</o:LastSaved>

I want to replace it with:

#############2013-01-21T21:15:00Z##############

How can we use RegEx for this?

解决方案

re.sub accepts a function as replacement:

re.sub(pattern, repl, string, count=0, flags=0)

If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string.

Here's an example:

In [1]: import re

In [2]: def repl(m):
   ...:     return '#' * len(m.group())
   ...: 

In [3]: re.sub(r'<[^<>]*?>', repl,
   ...:     '<o:LastSaved>2013-01-21T21:15:00Z</o:LastSaved>')
Out[3]: '#############2013-01-21T21:15:00Z##############'

The pattern I used may need some polishing, I'm not sure what's the canonical solution to matching XML tags is. But you get the idea.

这篇关于用相同长度的字符串替换正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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