Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Regex help \b & \Q

by Not_a_Number (Prior)
on Apr 14, 2016 at 18:45 UTC ( [id://1160440]=note: print w/replies, xml ) Need Help??


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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-29 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found