Guest User

Untitled

a guest
Jun 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. >>> import re
  2. >>> myString = "HI there. You should higher that person for the job. Hi hi."
  3. >>> keyword = "hi"
  4. >>> search = re.compile(r'b(%s)b' % keyword, re.I)
  5. >>> search.sub('<b>\1</b>', myString)
  6. '<b>HI</b> there. You should higher that person for the job. <b>Hi</b> <b>hi</b>.'
  7.  
  8. import re
  9.  
  10. def SurroundWith(text, keyword, before, after):
  11. regex = re.compile(r'b%sb' % keyword, re.IGNORECASE)
  12. return regex.sub(r'%s%s' % (before, after), text)
  13.  
  14. >>> SurroundWith('HI there. You should hire that person for the job. '
  15. ... 'Hi hi.', 'hi', '<b>', '</b>')
  16. '<b>HI</b> there. You should hire that person for the job. <b>Hi</b> <b>hi</b>.'
  17.  
  18. def SurroundWith2(text, keyword, before, after):
  19. regex = re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % keyword,
  20. re.IGNORECASE)
  21. return regex.sub(r'1%s2%s3' % (before, after), text)
  22.  
  23. import re
  24. def reg(keyword, myString) :
  25. regx = re.compile(r'b(' + keyword + r')b', re.IGNORECASE)
  26. return regx.sub(r'<b>1</b>', myString)
Add Comment
Please, Sign In to add comment