Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:19:19+00:00 2026-05-31T14:19:19+00:00

I’m using Python to parse a file in search for e-mail addresses, but I

  • 0

I’m using Python to parse a file in search for e-mail addresses, but I can’t figure out what the syntax for alternative regexps should be. Here’s the code:

addresses = []

pattern = '(\w+)@(\w+\.com)|(\w+)@(it.\w+\.com)'
for line in file:
    matches = re.findall(pattern,line)
    for m in matches:
        address = '%s@%s' % m
        addresses.append(address)

So I want to find addresses that look like john@company.com or john@it.company.com, but the above code doesn’t work because either the first two groups are empty or the last two groups are empty. What is the correct solution? I need to use groups to store the user name (before @) and server name (after @) separately.

EDIT: Matching email adresses is only an example. What I’m trying to find out is how to match different regexps that have only one thing in common – they match two groups.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:19:20+00:00Added an answer on May 31, 2026 at 2:19 pm

    (\w+)@((?:it\.)?\w+\.com)

    You want to capture the part after the @ whether it’s example.com or it.example.com, so you put both options inside the same capture group. But since they share a similar format, you can condense (it\.\w+\.com|\w+\.com) to just ((it\.)?\w+\.com)

    The (?: ) makes that parens a non-capturing group, so it won’t take part in your matched groups. There will be one match for the first (\w+), and one match for the whole ((?:it\.)?\w+\.com) after the @. That’s two matches total, plus the default group-0 match for the full string.

    EDIT: To answer your new question, all you have to do is follow the grouping I used, but stop before you condense it.
    If your test cases are:

    1) example@abcdef

    2) example@123456

    You could write your regex as such: (\w+)@([a-zA-Z]+|\d+), which would always have the part before the @ in group 1, and the part after in group 2. Notice that there are only two pairs of parens, and the |(“or”) operator appears inside of the second parens group.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.