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

  • Home
  • SEARCH
  • 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 145205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:22:56+00:00 2026-05-11T08:22:56+00:00

I’m trying to extract email addresses from plain text transcripts of emails. I’ve cobbled

  • 0

I’m trying to extract email addresses from plain text transcripts of emails. I’ve cobbled together a bit of code to find the addresses themselves, but I don’t know how to make it discriminate between them; right now it just spits out all email addresses in the file. I’d like to make it so it only spits out addresses that are preceeded by ‘From:’ and a few wildcard characters, and ending with ‘>’ (because the emails are set up as From [name]<[email]>).

Here’s the code now:

import re #allows program to use regular expressions foundemail = [] #this is an empty list  mailsrch = re.compile(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}')  #do not currently know exact meaning of this expression but assuming  #it means something like '[stuff]@[stuff][stuff1-4 letters]'          # 'line' is a variable is set to a single line read from the file # ('text.txt'): for line in open('text.txt'):      foundemail.extend(mailsrch.findall(line))      # this extends the previously named list via the 'mailsrch' variable       #which was named before  print foundemail 
  • 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. 2026-05-11T08:22:57+00:00Added an answer on May 11, 2026 at 8:22 am

    If your goal is actually to extract email addresses from text, you should use a library built for that purpose. Regular expressions are not well suited to match arbitrary email addresses.

    But if you’re doing this as an exercise to understand regular expressions better, I’d take the approach of expanding the expression you’re using to include the extra text you want to match. So first, let me explain what that regex does:

    [\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4} 
    • [\w\-] matches any ‘word’ character (letter, number, or underscore), or a hyphen
    • [\w\-\.]+ matches (any word character or hyphen or period) one or more times
    • @ matches a literal ‘@’
    • [\w\-] matches any word character or hyphen
    • [\w\-\.]+ matches (any word character or hyphen or period) one or more times
    • [a-zA-Z]{1,4} matches 1, 2, 3, or 4 lowercase or uppercase letters

    So this matches a sequence of a ‘word’ that may contain hyphens or periods but doesn’t start with a period, followed by an @ sign, followed by another ‘word’ (same sense as before) that ends with a letter.

    Now, to modify this for your purposes, let’s add regex parts to match ‘From’, the name, and the angle brackets:

    From: [\w\s]+?<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})> 
    • From: matches the literal text ‘From: ‘
    • [\w\s]+? matches one or more consecutive word characters or space characters. The question mark makes the match non-greedy, so it will match as few characters as possible while still allowing the whole regular expression to match (in this case, it’s probably not necessary, but it does make the match more efficient since the thing that comes immediately afterwards is not a word character or space character).
    • < matches a literal less-than sign (opening angle bracket)
    • The same regular expression you had before is now surrounded by parentheses. This makes it a capturing group, so you can call m.group(1) to get the text matched by that part of the regex.
    • > matches a literal greater-than sign

    Since the regex now uses capturing groups, your code will need to change a little as well:

    import re foundemail = []  mailsrch = re.compile(r'From: [\w\s]+?<([\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4})>')  for line in open('text.txt'):     foundemail.extend([m.group(1) for m in mailsrch.finditer(line)])  print foundemail 

    The code [m.group(1) for m in mailsrch.finditer(line)] produces a list out of the first capturing group (remember, that was the part in parentheses) from each match found by the regular expression.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't get the progress info like that through grep;… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer SELECT Avg(Value), Max(Value), Min(Value) FROM tableName See also MySQL Aggregate… May 12, 2026 at 1:04 am
  • Editorial Team
    Editorial Team added an answer DELETE FROM mytable FROM mytable mto WHERE instant < (… May 12, 2026 at 1:04 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.