LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-24-2008, 03:22 AM   #1
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
any character matching doesn't yield expected result in PERL


Hi,
i have some issues with pattern matching in PERL.

code:

$_="5.555555abcd";

m/([0-9.]*)/;

print $1;
i expect the o/p as


5.555555abcd but it gives only 5.555555

i believe "." should match any character hence any aphabets also.

There is one more question,

1) when i assign $_ = 5.555555abcd;
here it is without quotes.
It throws an error saying "opeartor" missing.
why it is so? Eitherways, when there are no spaces in between, i think quotes are not mandatory.


can somebody clear this queries of mine?

thanks,
~ gaynut
 
Old 09-24-2008, 03:32 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Dot does not match any character inside brackets. It is a literal dot.

Without quotes, perl considers this a numeric assignment, and of course your string is not a number.
 
Old 09-24-2008, 03:47 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
1. inside range operators ( [] ), a dot is a literal char.
2. You should always use quotes for strings (I believe).

If you want to know the internal Perl reasons, try www.perlmonks.org
 
Old 09-24-2008, 09:52 AM   #4
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Hi Thanks for replying.

I have 1 more doubt. Could you please explain the following code.

$\="\n";
$_=" This is a program.
/* The lines are now commented
This is to avoid the compiler getting confused on reading this
This is to save reader's time */
";

$_ =~ s {
/\*
.*?
\*/
} []gsx;

print $_;




Queries:

1) In the above one, i have never tried to match "/*".
I have just given "\*" to match *
How is that the / also got matched and substituted with blank space.
2) what is the "x" modifier here?

I don't understand these. It would be nice if u could clarify my doubts.

Thanks
~ gaynut
 
Old 09-26-2008, 02:04 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
1) The expression uses /\* and \*/ (C comment start/end markers), so /* and */ are matched. Perhaps the usage of the { and } as the substitution operator delimiters is confusing you. They are used so that / does not need to be escaped. Otherwise, you'd have to use:

s/\/\*.*?\*\// /gsx

which is more difficult to parse. Anytime / is used in an matching RE operator, consider using a different delimiter character.

2) x allows whitespace in REs, for readability. Search perldoc perlre for "legibility by permitting whitespace"
 
  


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
perl matching script saltydog4791 Programming 4 12-05-2007 05:52 PM
Perl global matching ShaqDiesel Programming 1 11-07-2006 09:54 PM
expected primary expression before... Uint character question. RHLinuxGUY Programming 3 05-12-2006 01:08 PM
"who" not giving the expected result rblampain Linux - Newbie 2 11-17-2004 05:06 AM
perl regex matching exodist Programming 2 11-15-2004 11:50 PM


All times are GMT -5. The time now is 08:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration