开发者

How to replace all the characters with * using Regular Expression

I am having a text like

s = bluesky

I want to get it as s = ******* (equal no of * as no of ch开发者_高级运维aracters)

I am searching for a regular expression for Python.

Edit 1 :

b = '*'*len(s)

How can we do it in Django Template?


You don't need a regex for this:

s = 'bluesky'
b = '*'*len(s)
print b

output :

>>> s = 'bluesky'
>>> b = '*'*len(s)
>>> print b
*******


How can we do it in Django Template

In a Django template? Dead easy.

{% for char in s %}*{% endfor %}

Where s is the template variable whose value is bluesky.


No need for regexp, just text.replace('bluesky','*'*len('bluesky'))

e.g:

>>> text = "s = bluesky"
>>> text.replace('bluesky','*'*len('bluesky'))
's = *******'


>>> import re
>>> re.sub("(\w+)\s*=\s*(\w+)", lambda m: "%s = %s" % (m.group(1), '*'*len(m.group(2))), "s = bluesky")
's = *******'
>>> re.sub("(\w+)\s*=\s*(\w+)", lambda m: "%s = %s" % (m.group(1), '*'*len(m.group(2))), "cat=dog")
'cat = ***'

Assuming your string is literally s = bluesky

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜