On Fri, Mar 14, 2014 at 4:26 AM, Alex Chiang
<[email protected]> wrote:
> I tried to extract string from perl script, so I wrote following script:
>
> 5# matching string
> 6 # sting start with ' or ".
> 7 # assume string is on same line.
> 8 #
> 9 my $pattern = '(\"|\').*\1';
> 10 my @array;
> 11 open (my $file, "<", "str");
> 12 while (my $line = <$file>) {.
> 13 if($line =~ /$pattern/) {
> 14 push (@array, $&);
> 15 }
> 16 }
> 17
> 18 for (@array) { print "$_\n"; }
>
>
> the str sample file :
> -----------------------------------------------
> 'okay i know now'
> "from time to time" 'let us go'
> this is awkward
> ------------------------------------------------
>
> I would expect the output match three of them, however, it only matches:
> ------------------------------
> 'okay i know now'
> "from time to time"
> -------------------------------
> leaving 'let us go' unmatched.
>
> I don't know how to describe this problem, Can anyone help me with this ?
>
Here's a safer, more efficient version:
see: perldoc perlre
* uses 'qr' for reg.exp. compilation
* avoid speed penalty of $& with /p switch
* \g{} captures instead of \1 which can be ambiguous in case of \10 eg.
my $pattern = qr/("|').*?\g{1}/;
my @array;
while (my $line = <$file>){
while($line =~ /$pattern/pg) {
push (@array, ${^MATCH});
}
}
--
Charles DeRykus
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/