The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-27-2007
deepakpv deepakpv is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 11
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
  #2 (permalink)  
Old 03-28-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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.
  #3 (permalink)  
Old 03-28-2007
deepakpv deepakpv is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 11
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
  #4 (permalink)  
Old 03-28-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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";
}
}
  #5 (permalink)  
Old 03-28-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
  #6 (permalink)  
Old 03-28-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #7 (permalink)  
Old 03-28-2007
bigearsbilly bigearsbilly is offline
Registered User
  
 

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

do_something if m/[+]{5}/
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl regex, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0