This can be seen as an NLP (Natural Language Processing) problem. There is a special python module called NLTK (Natural Language Toolkit) that can be best used to solve this task, easier done than with regular expressions.
1) First you need to download the NLTK (http://www.nltk.org/install.html)
2) Import NLTK:
import nltk
3) Create a small grammar, a context free grammar containing your four rules (https://en.wikipedia.org/wiki/Context-free_grammar). By means of the CFG module from NLTK, you can easily do that with one line of code:
acm_grammar = nltk.CFG.fromstring("""
ACTIVE_LIST -> ACTOR | ACTIVE_LIST 'and' ACTOR
ACTOR -> NOUN | ARTICLE NOUN
ARTICLE -> 'a' | 'the'
NOUN -> 'tom' | 'jerry' | 'goofy' | 'mickey' | 'jimmy' | 'dog' | 'cat' | 'mouse' """)
4) Create a parser that will use the acm_grammar:
parser = nltk.ChartParser(acm_grammar)
5) Test it on some input. Input sentences must be in the form of a list with comma-separated words (strings). The split() method can be used for this:
input= ["a tom", "tom and a jerry", "the tom and a jerry","the tom and a jerry and tom and dog","Tom", "the Tom and me"]
for sent in input:
split_sent = sent.split()
try:
parser.parse(split_sent)
print(sent,"-- YES I WILL")
except ValueError:
print(sent,"-- NO I WON'T")
In this last step, we check if the parser can parse a sentence according to the acm_grammar. If it cannot, the call to the parser will result in a ValueError.
Here is the output of this code:
a tom -- YES I WILL
tom and a jerry -- YES I WILL
the tom and a jerry -- YES I WILL
the tom and a jerry and tom and dog -- YES I WILL
Tom -- NO I WON'T
the Tom and me -- NO I WON'T