jweinst1
10/9/2015 - 9:22 PM

simple pattern matching with strings in python

simple pattern matching with strings in python

import re

def all_numbers(string):
    #from the start to the end, there must be all numbers.
    temp = re.compile(r"^[0-9]*$")
    return bool(temp.match(string))
    
def contains_number(string):
    #a number somewhere in the string.
    temp = re.compile(r"^.*[0-9].*$")
    return bool(temp.match(string))

def has_3letterword(string):
    #has 3 letter word in middle of string
    temp = re.compile(r"^.* [a-z][a-z][a-z] .*$")
    return bool(temp.match(string))
    
def start_3letterword(string):
    #starts with 3 letter word
    temp = re.compile(r"^[a-z][a-z][a-z] .*")
    return bool(temp.match(string))

def check_for_vowelend(string):
    string = string.split()
    temp = re.compile(r"^.*[aeiou]$")
    for elem in string:
        if temp.match(elem):
            print(elem)
    return None