treyhunner

regex exercise answers

Mar 31st, 2016
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. # Has Vowels
  5.  
  6. def has_vowels(string):
  7.     return bool(re.search(r'[aeiou]', string))
  8.  
  9.  
  10. # Is Integer
  11.  
  12. def is_integer(string):
  13.     return bool(re.search(r'^-?[0-9]+$', string))
  14.  
  15. def is_integer(string):
  16.     return bool(re.search(r'^-?\d+$', string))
  17.  
  18.  
  19. # Is Fraction
  20.  
  21. def is_fraction(string):
  22.     return bool(re.search(r'^-?[0-9]+/0*[1-9]+[0-9]*$', string))
  23.  
  24. def is_fraction(string):
  25.     return bool(re.search(r'^-?\d+/\d*[1-9]+\d*$', string))
  26.  
  27.  
  28. # Valid Time
  29.  
  30. def is_valid_time(string):
  31.     return bool(re.search(r'([0-1]\d|2[0-3]):[0-5]\d', string))
  32.  
  33.  
  34. # Valid Date
  35.  
  36. def is_valid_date(string):
  37.     return bool(re.search(r'\d{4}-[01]\d-[0-3]\d', string))
  38.  
  39. def is_valid_date(string):
  40.     return bool(re.search(r'''
  41.        (19|20) \d \d
  42.        -
  43.        ( 0[1-9] | 1[0-2] )
  44.        -
  45.        ( 0[1-9] | [12]\d | 3[01] )
  46.    ''', string, re.VERBOSE))
  47.  
  48.  
  49. # Tetravocalic
  50.  
  51. re.findall(r'\b.*[aeiou]{4}.*\b', dictionary)
  52.  
  53.  
  54. # Hexadecimal Words
  55.  
  56. re.findall(r'\b[0-9a-f]+\b', dictionary)
  57.  
  58. re.findall(r'\b[a-f\d]+\b', dictionary)
  59.  
  60.  
  61. # Nexaconsonantal
  62.  
  63. re.findall(r'\b.*[^aeiouy\s]{6}.*\b', dictionary)
  64.  
  65. re.findall(r'\b.*[bcdfghjklmnpqrstvwxz]{6}.*\b', dictionary)
  66.  
  67.  
  68. # Crossword Helper
  69.  
  70. def possible_words(partial_word):
  71.     pattern = r'\b{}\b'.format(partial_word.replace('_', '.'))
  72.     return re.findall(pattern, dictionary, re.IGNORECASE)
  73.  
  74.  
  75. # Repeat Letter
  76.  
  77. re.findall(r'\b.*n.*n.*n.*n.*n.*\b', dictionary)
  78.  
  79. re.findall(r'\b(?:.*n.*){5}\b', dictionary)
Add Comment
Please, Sign In to add comment