Replacing RegEx with a string of equal length characters

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

For example:

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

      

I want to replace it:

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

      

How can we use RegEx for this?

+3


source to share


1 answer


re.sub

takes a function as a replacement:

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

      

If repl

is a function, it is called for each non-overlapping occurrence of the pattern. The function takes one argument of the matching object 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 template used may require some polishing, I'm not sure what is the canonical solution for matching XML tags. But you have an idea.

+11


source







All Articles