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

Re: regexp q.



On Tue, Jul 15, 2008 at 15:44:02 -0500, Hugo Vanwoerkom wrote:
> Hi,
>
> Not being very versatile in regular expressions I wonder if is possible  
> to express this:
>
> Find the number of ),( triplets in a string, provided they are not in a  
> substring that is enclosed in single quotes, ignoring the pair \' in all  
> cases.
>
> in a regular expression?

The sane approach:
1) remove all occurrences of \' from the string
2) remove all matches of the regex '[^']*' from the remaining string
3) match all instances of ),( in the remaining string and count them

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"

-- 
Regards,            | http://users.icfo.es/Florian.Kulzer
          Florian   |


Reply to: