LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   perl problem (https://www.linuxquestions.org/questions/linux-newbie-8/perl-problem-733883/)

vinaytp 06-18-2009 07:23 AM

perl problem
 
hi all.......

i am trying to match a pattern like alphanumeric. example
123.
ab3.
73aae.
here . should be at the end of the pattern ....

$mystring = "hello.World";
if($mystring =~ m/[a-zA-Z0-9]+\. /)
{
print "Yes";
}


this is not working and also how to get the matched pattern in a variable...

can anyone please help me...

Thanks in advance....

ghostdog74 06-18-2009 07:33 AM

put a $ after the \.

crs_zxf 06-18-2009 07:52 AM

You just have an extra space between '.' and the final '/', delete it, and your pattern will be right, this may due to your carelessness I think.

BTW, if you use '/' as the delimiter, 'm' before the first '/' is unnecessary.

vinaytp 06-18-2009 08:07 AM

Quote:

Originally Posted by ghostdog74 (Post 3578273)
put a $ after the \.

$mystring = "hello.World";
if($mystring =~ m/[a-zA-Z0-9]+\.$ /)
{
print "Yes";
}

this is what you are telling right...

it outputs nothing ..it should print yes if the pattern matches... please help me.......

vinaytp 06-18-2009 08:11 AM

Quote:

Originally Posted by crs_zxf (Post 3578293)
You just have an extra space between '.' and the final '/', delete it, and your pattern will be right, this may due to your carelessness I think.

BTW, if you use '/' as the delimiter, 'm' before the first '/' is unnecessary.

you are correct..thanks a lot ..it works ...but i am not getting how to get the matched pattern in a seperate variable

crs_zxf 06-18-2009 08:41 AM

Oh! Sorry, I forgot to answer that question.

You can write your code like this:

$mystring = "hello.World";
if ($mystring =~ /([a-zA-Z0-9]+\.)/)
{
print $1, "\n";
print "Yes";
}

and you'll see "hello." on the screen.

Notice that I put parentheses around the patten, and every time you do this, Perl will automatically put the matched part of the string into internal variables like $1, $2, ... and there are as many of these variables as pairs of parentheses

vinaytp 06-18-2009 09:30 AM

Quote:

Originally Posted by crs_zxf (Post 3578353)
Oh! Sorry, I forgot to answer that question.

You can write your code like this:

$mystring = "hello.World";
if ($mystring =~ /([a-zA-Z0-9]+\.)/)
{
print $1, "\n";
print "Yes";
}

and you'll see "hello." on the screen.

Notice that I put parentheses around the patten, and every time you do this, Perl will automatically put the matched part of the string into internal variables like $1, $2, ... and there are as many of these variables as pairs of parentheses

Thank you very much once again....its working..can you please suggest me a good tutorial for regular expressions and parsing in perl......

Su-Shee 06-18-2009 09:36 AM

Just call perldoc perlrequick and perldoc perlretut - it's all there. :)

chrism01 06-18-2009 07:10 PM

http://perldoc.perl.org/index-tutorials.html
http://www.perlmonks.org/?node=Tutorials

crs_zxf 06-19-2009 08:02 AM

Quote:

Originally Posted by vinaytp (Post 3578420)
Thank you very much once again....its working..can you please suggest me a good tutorial for regular expressions and parsing in perl......

You can refer to a book called "learning perl" (aka Llama book), I think this is a excellent book to start, and there are 3 chaps in this book talking about regular expression from level zero.


All times are GMT -5. The time now is 11:41 AM.