954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Question on "$" usage for regular expression.

Hi,

I have been reading "Learning Perl the Hard Way" and I was trying to complete one of the exercises which involved the use of regular expressions. I could get all of the anchors working except for "$". I have my code below with errors. I was just curious if someone could take a look at it for me. Thank you for your time--rgpii

#!/usr/bin/perl
use strict;
use warnings;
my $pattern = "(g$)";
my @fileargs = @ARGV;
foreach my $argument  (@fileargs)
        {       print "File Handle--> $argument\n";
                my $fhandle = $argument;
                open FILE,$fhandle;
                while (my $line = <FILE>)
                {       print "Printing Line --> $line\n";
                        if($line =~ m/$pattern/)
                        {
                        print "Pattern Matched!--> $line\n";
                        }
                }
        }


Here are my errors:

Final $ should be \$ or $name at ./grep.plx line 4, within string
syntax error at ./grep.plx line 4, near "= "(g)$""

rgpii
Newbie Poster
22 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
my $pattern = "(g$)";

Here '$)' is perl special variable gives the effective group id of the process.
So finally $pattern gets '(g0'. It's not a parsed regular expression.
I assumed your try to match the letter 'g' at end of the line. So modify the below line in your code.

my $pattern = '(g$)';

Here the string assigned with in single quote. Now $pattern gets (g$). Its a parsed regular expression. So you able match your string end with 'g'.

For more information about single quoted string & double quoted string read the 'variable interpolation' concept.

k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

Yes ,$) is special variable.If u print "$)" you will get 0 as the output.So single quotes is used

deepakkrish
Newbie Poster
10 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You