Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
    
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 at 23:42
    
Your regex isn't even close to correct for email address. For example, it doesn't recognize [email protected]. Many other examples. –  Chris Johnson 13 hours ago

1 Answer 1

up vote 1 down vote accepted

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|improve this answer
    
why is it important to quote the variable ? –  Abdul Al Hazred Apr 7 at 20:55
    
The answer to that is spelled out in great detail here: unix.stackexchange.com/q/171346/4667 –  glenn jackman Apr 7 at 21:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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