CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
pattern matching

 



SKS
New User

Jul 10, 2013, 9:07 PM

Post #1 of 4 (260 views)
pattern matching Can't Post

Hi there,

I am a new perl user. I am trying to get a text in between two dots of a line, but my program returns the entire line.

For example: I have a text which looks like
My sampledata 1,2 for perl .version 1_1.

I used the following match statement
$x =~ m/(\.)(.*)(\.)/;

my output for $x should be version 1_1 but I am getting the entire line as my match.

I would greatly appreciate any help.
Thanks.


Laurent_R
Enthusiast / Moderator

Jul 10, 2013, 11:18 PM

Post #2 of 4 (251 views)
Re: [SKS] pattern matching [In reply to] Can't Post

Try something like this:


Code
my $match = $1 if $x =~ /\.(.+)\./;



(This post was edited by Laurent_R on Jul 10, 2013, 11:19 PM)


BillKSmith
Veteran

Jul 11, 2013, 5:10 AM

Post #3 of 4 (244 views)
Re: [SKS] pattern matching [In reply to] Can't Post

Change your match operator to a substitute. Match the entire string and replace the match with the part that you want.


Code
#$x =~ m/(\.)(.*)(\.)/; 
$x =~ s/^.*\.(.*)\..*$)/$1/;
/code]
Good Luck,
Bill


SKS
New User

Jul 15, 2013, 11:55 PM

Post #4 of 4 (223 views)
Re: [SKS] pattern matching [In reply to] Can't Post

Hi everyone,

The code worked when I used
$x =~ m/(\.(.*)\.)/;

Thanks for the kind suggestions.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives