Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #FILE: temp.py:
- import re
- from bs4 import BeautifulSoup
- def reBIReplace(txt):
- txt = re.sub(r"</?b>","'''",txt)
- txt = re.sub(r"</?i>","''",txt)
- return txt
- def soupBIReplace(txt):
- soup = BeautifulSoup(txt)
- btag = soup.b
- itag = soup.i
- txt = txt.replace(str(btag), "'''"+str(btag.string)+"'''")
- txt = txt.replace(str(itag), "''"+str(itag.string)+"''")
- return txt
- #SHELL:
- >>> import timeit
- >>> timeit.Timer('temp.reBIReplace("foo <b>bar</b> <i>baz</i>")','import temp').timeit(100)
- 0.0029594631914433072
- >>> timeit.Timer('temp.soupBIReplace("foo <b>bar</b> <i>baz</i>")','import temp').timeit(100)
- 0.08923220030303725
- >>> print "regex is",0.08923220030303725 / 0.0029594631914433072,"times faster than soup."
- regex is 30.151481715 times faster than soup.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement