What is the email matching regex in basic regex for grep?

9,467

Be aware that matching email addresses is a LOT harder that what you have. See an excerpt from the Mastering Regular Expressions book

However, to answer your question, for a basic regular expression, your quantifiers need to be one of *, \+ or \{m,n\} (with the backslashes)

pattern='^[a-zA-Z0-9]\+@[a-zA-Z0-9]\+\.[a-z]\{2,\}'
grep "$pattern" regexfile

You need to quote the pattern variable

Share:
9,467

Related videos on Youtube

Abdul Al Hazred
Author by

Abdul Al Hazred

Updated on September 18, 2022

Comments

  • Abdul Al Hazred
    Abdul Al Hazred almost 2 years

    I created a text file and put some email addresses in it. Then I used grep to find them. Indeed it worked:

    # pattern="^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-z]{2,}"
    # grep -E $pattern regexfile
    

    but only as long I kept the -E option for an extended regular expression. How do I need to change the above regex in order to use grep without -E option?

    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      BTW, in case glenn’s O’Reilly reference elicits a TL;DR response, this regex is way short of what you need to match real-world email addresses; see Email Address Syntax at Wikipedia, for starters.
    • 1574ad6
      1574ad6 almost 9 years
      Your regex isn't even close to correct for email address. For example, it doesn't recognize [email protected]. Many other examples.
  • Abdul Al Hazred
    Abdul Al Hazred about 9 years
    why is it important to quote the variable ?
  • Angel Todorov
    Angel Todorov about 9 years
    The answer to that is spelled out in great detail here: unix.stackexchange.com/q/171346/4667