[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: regexp q.



Andrew Sackville-West wrote:
On Wed, Jul 16, 2008 at 09:45:03PM +0200, Florian Kulzer wrote:

...

The insane approach (dedicated to Andrew S-W, who is a great perl
aficionado):

#! /usr/bin/perl -w

#read file
open ( FH, "test.txt" );
$string = <FH>;
close ( FH );

# match and count
while ( $string =~ /('(\\'|[^'\\])*'|(\\'|[^'\\)])*|\)[^,\\]|\),[^(])*\),\(/g ) { $count++ }
print "$count\n"


gak my eyes bleed!

;-P

A

I agree!  And I'm a Perl and regex user.

Try this instead. I include only the portion that tests and counts, the rest would be like above, or using the diamond operator (<>). This pattern matches a simple string ('abc') as a demonstration of how to count the occurrences.

@c = /(abc)+/g;
print scalar(@c), "\n";

Now, it seems to me I remember reading somewhere that I could end up with a count, directly (in other words, not using 'scalar'), by simply assigning in a scalar context.

I can't get it to work, and can't find the reference, either.

As for the pattern to find the OP's original string, that will take more time (and thought).

Besides, Andrew's eyes need time to heal ;)

--
Bob McGowan

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Reply to: