Posted by Anonymous on Tue 13 May 05:34
report abuse | download | new post
- def replace(astr, regex, repl, case=0, count=0):
- import re
- if case == 0:
- p = re.compile(regex, re.IGNORECASE)
- else:
- p = re.compile(regex)
- return p.sub(repl, astr, count)
- """
- In shell:
- >>> def replace(astr, regex, repl, case=0, count=0):
- import re
- if case == 0:
- p = re.compile(regex, re.IGNORECASE)
- else:
- p = re.compile(regex)
- return p.sub(repl, astr, count)
- >>> astr = "Now is the time 4 all good men."
- >>> regex = r"[ohen0-9]"
- >>> repl = "X"
- >>> replace(astr, regex, repl, case=0, count=0)
- 'XXw is tXX timX X all gXXd mXX.'
- >>> replace(astr, regex, repl)
- 'XXw is tXX timX X all gXXd mXX.'
- >>> replace(astr, regex, repl, case=1)
- 'NXw is tXX timX X all gXXd mXX.'
- >>> replace(astr, regex, repl, count=4)
- 'XXw is tXX time 4 all good men.'
- >>>
- """
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.