0
\$\begingroup\$

I'm trying to remove email names from transcripts because it removes unnecessary info that is not needed in the transcript.

The regex below removes email names from a spoken transcript.

Examples below are completely made up. You'll note above that "dot" and "at" are the only common terms.

Format:

  • token1 tokenN at word dot word
  • before "at" remove 1-2 tokens delimited by spaces
  • remove "at"
  • remove the word after "at"
  • remove "dot"
  • remove the word after "dot"

Given the above at least the email name would be mostly removed. Prior to "at" you don't know how many words make up the email name or are part of the text.

The regex I created covers all cases above and leaves some words remaining in long email names:

import re

regExpattern = "[a-zA-Z0-9_.+-]+\s*at\s*[a-zA-Z0-9-.]+\s*[a-zA-Z0-9-]*\s*dot*\s*[a-zA-Z0-9-]{3}"

emails = ["jane 94 at g mail dot com", 
          "9 at gmail dot com",
          "jim doe at gmail dot com",
          "I am jane doe at A.B. dot com",
          "I am jane  at AB dot com",
          "just email happy jane doe seventy three at msn dot com",
          "jane doe seven to seven at hotmail dot com"
        ]

for text in emails:
    cleanText = re.sub(regExpattern, '', text)
    print(cleanText)

You can try it here: https://regex101.com/r/XV5GMT/2

Q: What spoken email names does the regex above miss (other than what I mention above)? Also, an email name that is mostly removed is good enough.

Alternatively, I tried to use POS tagging but couldn't discern any consistent patterns.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is borderline-off-topic. I consider it salvageable if you add more context about why you're trying to manipulate email address strings. \$\endgroup\$
    – Reinderien
    Commented Apr 21, 2020 at 15:10
  • 3
    \$\begingroup\$ On the other hand, if you know that this is incomplete and you're looking for advice on how to complete it, then this is off-topic for sure. \$\endgroup\$
    – Reinderien
    Commented Apr 21, 2020 at 15:12
  • \$\begingroup\$ In my original post above I added "why" I'm trying to manipulate email address strings. \$\endgroup\$
    – RandomTask
    Commented Apr 23, 2020 at 15:28
  • 1
    \$\begingroup\$ This pattern misses [email protected], which I presume would be transcribed as Firstname dot lastname at example dot com. Or at least it is missing that testcase. \$\endgroup\$
    – Graipher
    Commented May 7, 2020 at 9:47

1 Answer 1

2
\$\begingroup\$

Good Job (for the Expression)!

Your expression is pretty good for what I guess you are trying to do. My guess is that you just want to see if there is one email in a string. Clearly, I don't see that we are going to extract any email (since your expression currently doesn't do that not to mention the strings are quite unstructured).

To answer your question, I'm pretty sure it would miss lots of input strings, but to begin with, we can just add an i (insensitive) flag to the expression and remove A-Zs:

(?i)[a-z0-9_.+-]+\s*at\s*[a-z0-9.-]+\s*[a-z0-9-]*\s*dot*\s*[a-z0-9-]{2,6}\s*(?:dot*\s*[a-z0-9-]{2,6})?

Example of What is Going to Miss:

For instance "dot co dot uk" types of emails are going to be missed. For that, we would add an optional non-capturing group: (?:dot*\s*[a-z0-9-]{2,6})?

Not sure if a stand-alone regex would be the best solution!

The problem you are having is very complicated. I'm sure there are alternative methods to solve your problem, but I'm not really good at offering solutions.

Demo

import re

regex_pattern = r"(?i)[a-z0-9_.+-]+\s*at\s*[a-z0-9.-]+\s*[a-z0-9-]*\s*dot*\s*[a-z0-9-]{2,6}\s*(?:dot*\s*[a-z0-9-]{2,6})?"

emails = ["jane 94 at g mail dot com",
          "9 at gmail dot com",
          "jim doe at gmail dot com",
          "I am jane doe at A.B. dot com",
          "I am jane  at AB dot com",
          "just email happy jane doe seventy three at msn dot com",
          "jane doe seven to seven at hotmail dot com"
          "jane doe seven to seven at hotmail dot co dot uk"
          ]

for text in emails:
    clean_text = re.sub(regex_pattern, '', text)
    print(clean_text)

By the way, you can post your question on stackoverflow, where there are so many RegEx experts in there.

\$\endgroup\$
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.