Issues with an exact match using regex in perl!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues with an exact match using regex in perl!
# 1  
Old 06-16-2010
Issues with an exact match using regex in perl!

Hello Guys,

I am trying to make an exact match for an email address entered as an argument, using perl, however, it's not working if I put a "$" in the email address. See the below outputs,

Correct Match :

Code:
bash-2.03$ echo sandy@test.com | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'
sandy@test.com

Incorrect Match :

Code:
bash-2.03$ echo $sandy@test.com | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'
@test.com

Ideally it should not give any output for the second command. Any help in this regard would be greatly appreciated.

---------- Post updated at 05:28 AM ---------- Previous update was at 05:25 AM ----------

See the below entry,

Code:
bash-2.03$ echo SInGH$$$$sand@TEST.com
SInGH78497849sand@TEST.com


Last edited by pludi; 06-16-2010 at 06:59 AM.. Reason: code tags, please...
# 2  
Old 06-16-2010
How about this:
Code:
perl -wln -e 'print if /(^[a-zA-Z0-9\w+\._-]+\@test.com$)/i'

It is best to put "" around a variable reference to avoid interpretation by the shell, but in the case of a string the $ characters would still be interpreted so you have to use single quotes.
Code:
echo 'SInGH$$$$sand@TEST.com'

( $sandy got interpretated as a variable which is empty )
# 3  
Old 06-16-2010
Further to Scrutinizer's points, you may want to simplify the regex. The backreference is not used, so the round brackets may be discarded. "\w" takes care of "a-zA-Z0-9_". The dot (".") loses its special meaning inside a character class. The dash ("-") need not be escaped inside the brackets, rather, putting it at the end is enough to avoid it from being expressed as a range character. And finally, add the "$" inside the character class if you want to match it.

Here's the updated regex -

Code:
$
$
$ echo 'sandy@TEST.com' | perl -wlne 'print if /^[\w.\$-]+\@test.com$/i'
sandy@TEST.com
$
$ echo '$sandy@TEST.com' | perl -wlne 'print if /^[\w.\$-]+\@test.com$/i'
$sandy@TEST.com
$
$ echo 'SInGH$$$$sand@TEST.com' | perl -wlne 'print if /^[\w.\$-]+\@test.com$/i'
SInGH$$$$sand@TEST.com
$
$ echo 'Jan99Der$.Goyvaerts_burton-weiss@TEST.com' | perl -wlne 'print if /^[\w.\$-]+\@test.com$/i'
Jan99Der$.Goyvaerts_burton-weiss@TEST.com
$
$

tyler_durden

Last edited by durden_tyler; 06-16-2010 at 09:50 AM..
# 4  
Old 06-18-2010
That worked, however, now script is not working

using single quotes instead of double has worked, however, now the output of that command is not being assigned to a variable, see the below command,

Code:
ADDCHECK=`echo -n 'sandy@test.com' | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'`

Now ADDCHECK is getting null value, please help me out !!!

Moderator's Comments:
Mod Comment Please use code tags
# 5  
Old 06-18-2010
Quote:
Originally Posted by suffisandy
...
Code:
ADDCHECK=`echo -n 'sandy@test.com' | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'`

Now ADDCHECK is getting null value, please help me out !!!

...
Are you sure ? It works fine for me.

What's your shell and OS ?

Code:
$
$
$ echo $SHELL
/bin/bash
$
$
$ echo -n 'sandy@test.com' | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'
sandy@test.com
$
$
$ echo $ADDCHECK
$
$
$ ADDCHECK=`echo -n 'sandy@test.com' | perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'`
$
$
$ echo $ADDCHECK
sandy@test.com
$
$

tyler_durden
# 6  
Old 06-18-2010
am using bash on solaris

Code:
bash-2.03$ uname -a
SunOS romulus 5.8 Generic_117350-62 sun4u sparc SUNW,Ultra-4



---------- Post updated at 02:48 PM ---------- Previous update was at 02:37 PM ----------

If I use double quotes, it works fine, see below

Code:
bash-2.03$ bash -x ./six.sh sandy@test.com
++ echo -n sandy@test.com
++ perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'
+ ADDCHECK=sandy@test.com
+ echo sandy@test.com
sandy@test.com
bash-2.03$

If I use single quotes, than it doesnot works,

Code:
bash-2.03$ bash -x ./six.sh sandy@test.com
++ echo -n '$1'
++ perl -wln -e 'print if /(^[a-zA-Z0-9\w+\.\_\-]*\@test.com$)/i'
+ ADDCHECK=
+ echo ''


Last edited by Scott; 06-18-2010 at 03:52 PM.. Reason: Code tags, PLEASE!
# 7  
Old 06-18-2010
Can you paste the script "six.sh" (or at least the relevant part of it) over here ?

Code:
cat -n six.sh

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Advanced & Expert Users

Regex to match Exact port number (netstat command)

Hi All, We have this regex:\\*.*?(.600).*?.(LISTEN|ESTABLISHED) OS = Solaris 10 The purpose of this regex is to match the ports in output of "netstat -an" and report if any ports between 6000-6009 are getting used. The only problem is if I have something like this (sample output as... (6 Replies)
Discussion started by: sk2code
6 Replies

3. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

4. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

5. Shell Programming and Scripting

perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially... IN text file: {**************************** {test : test...test } Script: while (<$fh>) { push ( @data, $_); } if ( $data =~ m/(^{\*+$)/ ){ } With the above match i am... (5 Replies)
Discussion started by: avskrm
5 Replies

6. Shell Programming and Scripting

PERL: Help required exact match

Hello, My requirement is to iterate over all the lines of a file and compare them with a word and perform some operations if exact match is found. For the snippet below, it works even if contents of line include "diff" and "diff:". I want it to work only if it is exactly "diff" and is not... (2 Replies)
Discussion started by: sarbjit
2 Replies

7. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

8. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

9. Shell Programming and Scripting

exact match in Perl

Hi By using select clause I'm trying to pull out the rows to a variable. If the variable has 0 row(s) selected then i'm printing some text message else printing some other text message if($xyz =~ m/0 row/) { print "0 rows "; } else { print " There are rows"; } By my problem... (4 Replies)
Discussion started by: pdreddy34
4 Replies

10. Shell Programming and Scripting

perl exact match

How to emulate grep -o option in perl. I mean to print not all line, only the exact match. echo "2A2 BB" | perl -ne 'print if /2A2/' 2A2 BB I want to print only 2A2. (2 Replies)
Discussion started by: mirusnet
2 Replies
Login or Register to Ask a Question