0

I am trying to write a regex to catch email Ids . Testing since quite a few hours using regexpal.com . On the site, its able to catch all the email Ids. WHen I am substituting the same regex in Python and doing re.findall(pattern,line), it is not able to catch it.

Regex :

[a-zA-Z0-9-_]+[(.)?a-zA-Z0-9-_]*\s*(@|at)\s*[a-zA-Z0-9-_]+\s*(.|dot)\s*[a-zA-Z0-9-_]*\s*(.|dot)\s*e(\-)?d(\-)?u(\-)?(.,)?

Example :

Line =    <TR> <TD><B>E-Mail: </B> <TD><A HREF=MailTo:*[email protected]*\>*[email protected]*</A>

(Highlighted correctly on regexpal.com).

With Python :

 for line in f:
    print 'Line = ',line
        matches = re.findall(my_first_pat,line)
    print 'Matches = ',matches

Gives output:

Line =    <TR> <TD><B>E-Mail: </B> <TD><A HREF=MailTo:[email protected]>[email protected]</A>

Matches =  [('@', 'd', '.', '', '', '', ''), ('@', 'd', '.', '', '', '', '')]

What is the issue ?

2 Answers 2

1

Read the documentation for re.findall:

If one or more groups are present in the pattern, return a list of groups

Your groups only capture the at sign, dot, etc., therefore that's all that's returned by re.findall. Either use non-capturing groups, wrap the whole thing in a group, or use re.finditer.

(As noted by @Igor Chubin, your regex is also incorrectly using . instead of \., but this isn't causing the main problem.)

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain how do I use non-capturing groups or , wrap the whole thing in a group?
For info on that, check out just about any regex tutorial or reference, for instance regular-expressions.info .
0

You must use \. not . here:

(.|dot)

If you just want to say that you can have hyphens between letters in the edu part, you can do this without slashes and grouping:

e-?d-?u-?[.,]?

If you use () just to group symbols (but not for capturing), you must use (?:)instead:

(?:@|at)

1 Comment

This e(\-)? , I Used it in case an email is of the form [email protected] .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.