Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> import re
- >>> myString = "HI there. You should higher that person for the job. Hi hi."
- >>> keyword = "hi"
- >>> search = re.compile(r'b(%s)b' % keyword, re.I)
- >>> search.sub('<b>\1</b>', myString)
- '<b>HI</b> there. You should higher that person for the job. <b>Hi</b> <b>hi</b>.'
- import re
- def SurroundWith(text, keyword, before, after):
- regex = re.compile(r'b%sb' % keyword, re.IGNORECASE)
- return regex.sub(r'%s %s' % (before, after), text)
- >>> SurroundWith('HI there. You should hire that person for the job. '
- ... 'Hi hi.', 'hi', '<b>', '</b>')
- '<b>HI</b> there. You should hire that person for the job. <b>Hi</b> <b>hi</b>.'
- def SurroundWith2(text, keyword, before, after):
- regex = re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % keyword,
- re.IGNORECASE)
- return regex.sub(r'1%s2%s3' % (before, after), text)
- import re
- def reg(keyword, myString) :
- regx = re.compile(r'b(' + keyword + r')b', re.IGNORECASE)
- return regx.sub(r'<b>1</b>', myString)
Add Comment
Please, Sign In to add comment