The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with grep and regex raichlea UNIX for Dummies Questions & Answers 14 04-16-2008 08:25 AM
regex in variable alias47 UNIX for Dummies Questions & Answers 4 08-08-2007 05:37 AM
regex question xiamin Shell Programming and Scripting 2 07-16-2007 04:40 AM
Regex?? Please help lunac UNIX for Dummies Questions & Answers 7 01-30-2007 09:13 AM
sed regex Shakey21 UNIX for Dummies Questions & Answers 4 01-31-2002 05:16 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-27-2007
Registered User
 

Join Date: Feb 2007
Posts: 9
Regex

Hi,
i want to match a string using perl that has got 5 pluses(+++++).
i am using a function for this.
$str1="+++++";
check($str1,"\\+");
sub check{
$str1=$_[0];
$str2=$_[1];
if($str1=~m/^$str2{5}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}

But i am getting the output as wrong.

if i use if($str1=~m/^\+{5}$/) this i will get correct output.

Please help me.

Thanks,
Deepak
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-27-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code is working fine.
Code:
$ cat spt
#!/usr/bin/perl
$str1="+++++";
check($str1,"\\+");
sub check{
$str1=$_[0];
$str2=$_[1];
if($str1=~m/^$str2{5}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
$ ./spt
Correct.
Reply With Quote
  #3 (permalink)  
Old 03-27-2007
Registered User
 

Join Date: Feb 2007
Posts: 9
ya this is working fine.
but if i change the code like using another variable $no.

#!/usr/bin/perl
$str1="+++++";
$no=5;
check($str1,"\\+",$no);
sub check{
$str1=$_[0];
$str2=$_[1];
$str3=$_[2];

if($str1=~m/^$str2{$str3}$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}


perl test1.pl
Wrong..

Help me please...
Thanks,
Deepak
Reply With Quote
  #4 (permalink)  
Old 03-27-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
#!/usr/bin/perl
$str1="+++++";
$no=5;
check($str1,"\\+",$no);
sub check{
$str1=$_[0];
$str2=$_[1]."\{".$_[2]."\}";

if($str1=~m/^$str2$/){
print "Correct.\n";
}else{
print "Wrong..\n";
}
}
Reply With Quote
  #5 (permalink)  
Old 03-28-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,471
Quote:
$str2=$_[1]."\{".$_[2]."\}";
What is the need that ' { ' and ' } ' should be escaped as above?
Without that it would work !!!



Code:
#! /opt/third-party/bin/perl

sub check {
  $first = $_[0];
  $character = $_[1] . "{" . $_[2] . "}";
  print "yes\n" if( $first =~ m/^$character$/ );
}

$str = "+++++";
$no = 5;
check($str, "\\+", $no);

exit 0
Reply With Quote
  #6 (permalink)  
Old 03-28-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by matrixmadhan
What is the need that ' { ' and ' } ' should be escaped as above?
Without that it would work !!!



Code:
#! /opt/third-party/bin/perl

sub check {
  $first = $_[0];
  $character = $_[1] . "{" . $_[2] . "}";
  print "yes\n" if( $first =~ m/^$character$/ );
}

$str = "+++++";
$no = 5;
check($str, "\\+", $no);

exit 0
You are right
Reply With Quote
  #7 (permalink)  
Old 03-28-2007
Registered User
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
how about this

do_something if m/[+]{5}/
Reply With Quote
Google UNIX.COM
Reply

Tags
perl, perl regex, regex

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0