2

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?

  • 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. – Scott Apr 7 '15 at 23:42
  • 2
    Your regex isn't even close to correct for email address. For example, it doesn't recognize fred-smith@company.com. Many other examples. – Chris Johnson Sep 19 '15 at 18:45
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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