LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 02: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, 02:32 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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, 02:47 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
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, 08: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, 01:04 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 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
Open Source Consulting | Domain Registration