fork download
  1. def check_text_for_string(text_to_parse, string_to_find):
  2. import re
  3. matches = []
  4. pattern = r"(?<!\d\.)(?:\b|_)%s(?:\b|_)(?!\.\d)"%string_to_find
  5. return re.findall(pattern, text_to_parse)
  6.  
  7. import re
  8. word_to_match = "3423423987"
  9. possible_word_list = [
  10. "3423423987_1 the cake is a lie", #Match
  11. "3423423987sdgg call me Ishmael", #Not a match
  12. "3423423987 please sir, can I have some more?",#Match
  13. "3423423987",#Match
  14. "3423423987 ",#Match
  15. "3423423987\t",#Match
  16. "adsgsdzgxdzg adsgsdag\t3423423987\t",#Match
  17. "1233423423987", #Not a match
  18. "A3423423987", #Not a match
  19. "3423423987-1a\t",#Match
  20. "3423423987.0", #Not a match
  21. "342342398743635645" #Not a match
  22. ]
  23.  
  24. print("%d words in sample list."%len(possible_word_list))
  25. print("Only 7 should match.")
  26. matches = check_text_for_string("\n".join(possible_word_list), word_to_match)
  27. print("%d matched."%len(matches))
  28. print(matches)
Success #stdin #stdout 0.01s 9016KB
stdin
Standard input is empty
stdout
12 words in sample list.
Only 7 should match.
7 matched.
['3423423987_', '3423423987', '3423423987', '3423423987', '3423423987', '3423423987', '3423423987']