Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import re
  2.  
  3. def has_n_digits(dat, n):
  4. """
  5. Determines which of the insurer IDs in 'dat' contain digit strings of
  6. length n that are surrounded by non-digits.
  7.  
  8. For example,
  9. insurers = ["ANV 123", "QBE 0123", "Bermuda", "12345", "1234/175"]
  10. print(has_n_digits(dat=insurers, n=4))
  11. >> ["QBE 0123", "1234/175"]
  12. print(has_n_digits(dat=insurers, n=3))
  13. >> ["ANV 123", "1234/175"]
  14. print(has_n_digits(dat=insurers, n=2))
  15. >> []
  16.  
  17. dat: list of insurer ID strings
  18. n: integer denoting the number of digits to look for
  19.  
  20. """
  21. regex_str = "([0-9]{%s})" % n
  22. output = []
  23. for d in dat:
  24. # Regex doesn't reuse found characters, so the string must be manually
  25. # split apart
  26. d_sub = [d[i:i+n] for i in range(len(d)-n+1)]
  27. matches = [
  28. d_sub_entry for d_sub_entry in d_sub
  29. if re.match(regex_str, d_sub_entry) is not None]
  30. if len(matches) == 0:
  31. continue
  32. # Valid matches are those without other surrounding digits
  33. valid_matches = []
  34. for match in matches:
  35. start_index = d.find(match)
  36. if start_index > 0:
  37. if d[start_index - 1] in all_digits:
  38. continue
  39. end_index = start_index + n
  40. if end_index < len(d):
  41. if d[end_index] in all_digits:
  42. continue
  43. valid_matches.append(match)
  44.  
  45. if len(valid_matches) > 0:
  46. output.append(d)
  47. return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement