How Do You Effectively Use Regular Expressions To Find Alliterative Expressions?
I have an assignment that requires me to use regular expressions in python to find alliterative expressions in a file that consists of a list of names. Here are the specific instru
Solution 1:
Please consider improving your question.
Especially the question is only useful for those who want to answer to the exactly the same question, which I think is almost no chance. Please think how to improve so that it can be generallized to the point where this QA can be helpful to others.
I think your direction is about right.
- It's a good idea to check the input rightness using regular
expression.
r'[A-Z][a-z]* [A-Z][a-z]*'
is a good expression. - You can group the output by parentheses. So that you can easily get first and last name later on
- Keep in mind the difference between
re.match
andre.search
.re.search(r'[A-Z][a-z]* [A-Z][a-z]*', 'aaRob Smith')
returns a MatchObject. See this.
Also comment on general programming style
- Better to name variables
first
andlast
for readability, rather thank[0]
andk[1]
(and how is the letterk
picked!?)
Here's one way to do:
import re
FULL_NAME_RE = re.compile(r'^([A-Z][a-z]*) ([A-Z][a-z]*)$')
defis_alliterative(name):
"""Returns True if it matches the alliterative requirement otherwise False"""# If not matches the name requirement, reject
match = FULL_NAME_RE.match(name)
ifnot match:
returnFalse
first, last = match.group(1, 2)
first, last = first.lower(), last.lower() # easy to assume all lower-casesif first[0] != last[0]:
returnFalseif first[0] in'cst': # Check sh/ch/th# Do special checkreturn _is_cst_h(first) == _is_cst_h(last)
# All check passed!returnTruedef_is_cst_h(text):
"""Returns true if text is one of 'ch', 'sh', or 'th'."""# Bad (?) assumption that the first letter is c, s, or treturn text[1:].startswith('h')
names = [
'Umesh Vazirani', 'Vijay Vazirani' , 'Barbara Liskov',
'Leslie Lamport', 'Scott Shenker', 'R2D2 Rover', 'Shaq' , 'Sam Spade', 'Thomas Thing'
]
print [name for name in names if is_alliterative(name)]
# Ansprint ['Vijay Vazirani', 'Leslie Lamport', 'Sam Spade', 'Thomas Thing']
Solution 2:
Try this regular expression:
[a[0]forainre.findall('((?P<caps>[A-Z])[a-z]*\\s(?P=caps)[a-z]*)', names)]
Note: It does not handle the sh/ch/th special case.
Post a Comment for "How Do You Effectively Use Regular Expressions To Find Alliterative Expressions?"