LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2007, 03:03 AM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Problems with '+' in grep


I am trying to valdiate an e-mail address. There are zillions of examples out on the internet, but whatever I use, my valid e-mail address is never validated.

It appears to boil down at the '+' character. For example when I try to match zero or more alpha characters I get a match:

Code:
jlinkels@jlinkels_lt:/var/www$ echo jlin@ab.an | grep '^[:alpha:]*'
jlin@ab.an
However, when I try to match ONE or more alpha's, I get a mismatch:
Code:
jlinkels@jlinkels_lt:/var/www$ echo jlin@ab.an | grep '^[:alpha:]+'
I don't get that, what I am asking is "match one or more alpha characters at the beginning of the string". Or am I?

I am using GNU grep 2.5.1

Obviously the real matching regexp is much more complicated, but since I even don't understand this simple example, it is unthinkable that I could use those.

jlinkels
 
Old 12-03-2007, 03:29 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
use egrep

strangely grep -e doesn't work, which I always assumed was a synonym for egrep
 
Old 12-03-2007, 03:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I am not an expert about regular expressions, but from the grep man page
Quote:
In basic regular expressions the metacharacters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
also I'd use the boundary tag \b which matches the empty string at the edges of a word, as in
Code:
$ echo jlin@ab.an | grep '\b[:alpha:]\+'
jlin@ab.an
I hope this will help a little, waiting for a real regexp guru...
 
Old 12-03-2007, 03:43 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
egrep works !!!
 
Old 12-03-2007, 03:50 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# echo jlin@ab.an | grep -E "^[[:alpha:]]+"
jlin@ab.an
 
Old 12-03-2007, 03:55 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
bigearsbilly, sorry but I didn't see your post before posting mine (just for a bunch of minutes), anyway I tried the expression from the OP with egrep and it doesn't work on my system:
Code:
$ echo jlin@ab.an | egrep '^[:alpha:]+'
$ <no-output>
 
Old 12-03-2007, 03:59 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
what system?
 
Old 12-03-2007, 04:10 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Fedora Core 5, 2.6.20-1.2316.fc5, grep 2.5.1. Instead the one suggested by ghostdog74 works. But - I repeat - I am not a regexp guru, so I leave the correct explanation to you.
 
Old 12-03-2007, 04:11 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by colucix View Post
bigearsbilly, sorry but I didn't see your post before posting mine (just for a bunch of minutes), anyway I tried the expression from the OP with egrep and it doesn't work on my system:
Code:
$ echo jlin@ab.an | egrep '^[:alpha:]+'
$ <no-output>
Code:
# echo jlin@ab.an | egrep '^[[:alpha:]]+'
jlin@ab.an
 
Old 12-03-2007, 04:12 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
are you sure, it works on my solaris and cygwin
 
Old 12-03-2007, 04:31 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
ooops!


Code:
grep '^[[:alpha:]]+'
that's better
 
Old 12-03-2007, 03:50 PM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Hmmm weird. egrep works as expected,
grep doesn't,
grep -e doesn't
but grep -E does.

I think I only tried grep -e (I read the man page).

Is this a bug in grep?

I should be able to proceed from here. Thanks all.

jlinkels
 
Old 12-03-2007, 05:20 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by jlinkels View Post
...
(I read the man page).
...
Are you sure you read the right page??

Quote:
Originally Posted by man grep
egrep is the same as grep -E.
...
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see
below). (-E is specified by POSIX.)
...
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This is useful to protect patterns
beginning with hyphen-minus (-). (-e is specified by POSIX.)
 
Old 12-03-2007, 06:23 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For anyone attempting to validate an email address, I recommend reading this article (and comments/links): http://haacked.com/archive/2007/08/2...s-until-i.aspx
 
Old 12-04-2007, 02:46 AM   #15
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
ntubski: I said I read it, I did not say I understood it. But now it is clear. Thanks to the feedback here.

chrism01: Thanks, I'll study that. I was intending to use someone's else regexp anyway instead of desiging my own.

jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
problems with regular expression and grep bwreath Linux - General 6 03-18-2005 01:43 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration