Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split Regex, return only characters, digits and underscores. Perl

Tags:

regex

perl

Attempting to break up the line

#!/usr/bin/perl -w

with the following code

use strict;
use warnings;

my %words;

while (my $line = <>)
{
foreach my $word (split /:|,\s*|\/|!|\#|-/, $line)
{
    $words{$word}++;
}
}

foreach my $word (keys %words)
{
print "$word: $words{$word}\n";
}

Is there an easier way to have the split command only split at words, numbers and underscores? Rather than setting all of these delimiters.

Attempting to get the output

usr: 1
bin: 1
perl: 1
like image 741
Conman Avatar asked Nov 19 '25 02:11

Conman


1 Answers

Don't split, extract.

++$words{$_} for $line =~ /\w+/g;
like image 183
ikegami Avatar answered Nov 20 '25 18:11

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!