any character matching doesn't yield expected result in PERL
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.