LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-18-2009, 08:23 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
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....
 
Old 06-18-2009, 08:33 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put a $ after the \.
 
Old 06-18-2009, 08:52 AM   #3
crs_zxf
Member
 
Registered: Oct 2008
Distribution: Arch Linux
Posts: 37

Rep: Reputation: 17
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.
 
Old 06-18-2009, 09:07 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by ghostdog74 View Post
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.......
 
Old 06-18-2009, 09:11 AM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by crs_zxf View Post
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
 
Old 06-18-2009, 09:41 AM   #6
crs_zxf
Member
 
Registered: Oct 2008
Distribution: Arch Linux
Posts: 37

Rep: Reputation: 17
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

Last edited by crs_zxf; 06-18-2009 at 09:47 AM.
 
Old 06-18-2009, 10:30 AM   #7
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by crs_zxf View Post
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......
 
Old 06-18-2009, 10:36 AM   #8
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
Just call perldoc perlrequick and perldoc perlretut - it's all there.
 
Old 06-18-2009, 08:10 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
http://perldoc.perl.org/index-tutorials.html
http://www.perlmonks.org/?node=Tutorials
 
Old 06-19-2009, 09:02 AM   #10
crs_zxf
Member
 
Registered: Oct 2008
Distribution: Arch Linux
Posts: 37

Rep: Reputation: 17
Quote:
Originally Posted by vinaytp View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
need perl help...perl module problem john83reuben Programming 2 03-22-2008 10:08 PM
s/// problem : perl linuxlover1 Programming 3 01-03-2006 08:20 AM
Problem with perl module for w3c validator to work on my local Apache+PHP+perl instal tbamt Linux - Software 0 12-16-2004 06:37 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
perl problem? apache problem? cgi problem? WorldBuilder Linux - Software 1 09-17-2003 08:45 PM


All times are GMT -5. The time now is 08:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration