Reputation: 323
He you can see simple script for email matching using RE
# Exercise: make a regular expression that will match an email
def test_email(your_pattern):
pattern = re.compile(r"^(john|python-list|wha)")
emails = ["[email protected]", "[email protected]", "wha.t.`1an?ug{}[email protected]"]
for email in emails:
if not re.match(pattern, email):
print "You failed to match %s" % (email)
elif not your_pattern:
print "Forgot to enter a pattern!"
else:
print "%s was found in the %s" %(str(pattern),email)
pattern = r"^(john|python-list|wha)" # Your pattern here!
test_email(pattern)
As you can see here patter has been mentioned twisely both as the local and global
variables. Eventually I've obtained results like
<_sre.SRE_Pattern object at 0x223dba0> was found in the [email protected]
<_sre.SRE_Pattern object at 0x223dba0> was found in the [email protected]
<_sre.SRE_Pattern object at 0x223dba0> was found in the wha.t.`1an?ug{}ly@email.
How it possible to show real found pattern in the relusts instead of string like Pattern_object?
Why if I define a pattern like the example below, no patterns are found?
pattern = re.compile(r"$(org|com)") # find strings which end on the 'com' or 'org'
Upvotes: 1
Views: 102
Reputation: 12077
You need to print the matched groups. You're now printing the search pattern object, which is not a match. You should store the match if it exists, and print that.
# Exercise: make a regular expression that will match an email
def test_email(your_pattern):
pattern = re.compile(r"^(john|python-list|wha)")
emails = ["[email protected]", "[email protected]", "wha.t.`1an?ug{}[email protected]"]
for email in emails:
match = re.match(pattern, email)
if not match:
print "You failed to match %s" % (email)
elif not your_pattern:
print "Forgot to enter a pattern!"
else:
print "%s was found in the %s" %(match.groups(), email)
pattern = r"^(john|python-list|wha)" # Your pattern here!
test_email(pattern)
Also note that you're overwriting the pattern in the first line of your function. You might want to change that to:
def test_email(your_pattern):
pattern = your_pattern # See here.
emails = ["[email protected]", "[email protected]", "wha.t.`1an?ug{}[email protected]"]
for email in emails:
match = re.match(pattern, email)
if not match:
print "You failed to match %s" % (email)
elif not your_pattern:
print "Forgot to enter a pattern!"
else:
print "%s was found in the %s" %(match.groups(), email)
Be aware that re.match
will match from the beginning of the line, thus if you need to match from say, my email is [email protected]
, you need to use re.search
.
More information regarding the search() vs. match()
Demo:
>>> pattern = r"^(john|python-list|wha)" # Your pattern here!
>>> test_email(pattern)
('john',) was found in the [email protected]
('python-list',) was found in the [email protected]
('wha',) was found in the wha.t.`1an?ug{}[email protected]
>>>
Upvotes: 1