in reply to Regex help \b & \Q

I would use a different approach to your task, rather than a complicated regex:

my @wanted = ( 'C', 'C++','.NET', ); my $str = '.net, .net; C# .NET Cobol .NET C++ .NET .NETER c# IT x.NET' +; my %split_str; $split_str{ +uc }++ for split /[ ;,]+/, $str; say "$_: " . ( $split_str{$_} || 0 ) for @wanted;

That way, if you need to add another item to your search list, there's only one change to be made in one place. For example, if you wanted to add say C# or PL/M, you would just append them to the @wanted array.

Update: Of course, this approach fails for 'Visual Basic', but as far as I can see, so do all the other replies in this thread...

Replies are listed 'Best First'.
Re^2: Regex help \b & \Q
by AnomalousMonk (Archbishop) on Apr 14, 2016 at 19:20 UTC

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = '.net,.Net; .NET;.net, Visual Basic Visual Basic; VisualBasic +x Visual Basic,Visual Basic'; ;; for my $kw (qw(.NET C C++), 'Visual Basic') { my $count = () = $s =~ m{ (?: (?<! \S) | (?<= [,;])) \Q$kw\E (?: (? +! \S) | (?= [,;])) }xmsig; print qq{'$kw' $count}; } " '.NET' 4 'C' 0 'C++' 0 'Visual Basic' 4
    But that's not to say your basic approach isn't better!


    Give a man a fish:  <%-{-{-{-<