How do I match a list of things on regex?

2019-07-29 09:30发布

I'm parsing an file, and parts of it is a records thing, the format is like:

CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB

I want to move it to a hash that looks like:

{ UserA => { CategoryA => 5, CategoryB => 4, }, 
  UserB => { CategoryA => 6, CategoryB => 5, },
  UserC => { CategoryA => 7, },
}

How do I do regex on this?

Edit: It does not have to be purely only regex - just in perl and loops would be good too.

标签: regex perl
5条回答
做自己的国王
2楼-- · 2019-07-29 09:45

You need two regexes, one to identify new categories and one to parse user records.

#!/usr/bin/perl

use strict;
use warnings;

my %users;
my $cur;
while (<DATA>) {
    if (my ($category) = /^(.*)--$/) {
        $cur = $category;
        next;
    }
    next unless my ($id, $user) = /([0-9]+): (\w+)/;
    die "no category found" unless defined $cur;
    $users{$user}{$cur} = $id;
}

use Data::Dumper;
print Dumper \%users;

__DATA__
CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB

Or, if you have Perl 5.10 or later, you can use named captures with one regex:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

my %users;
my $cur;
while (<DATA>) {
    next unless /^(?:(?<category>.*)--|(?<id>[0-9]+): (?<user>\w+))$/;
    if (exists $+{category}) {
        $cur = $+{category};
        next;
    }
    die "no category found" unless defined $cur;
    $users{$+{user}}{$cur} = $+{id};
}

use Data::Dumper;
print Dumper \%users;

__DATA__
CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB
查看更多
做个烂人
3楼-- · 2019-07-29 09:46

This perl code seems to do what your looking for (mostly with one change). I laied out the data structure a bit differently but not much.

#!/usr/bin/perl

use strict;

my @array = (
    "CategoryA--",
    "5: UserA",
    "6: UserB",
    "7: UserC",
    "CategoryB--",
    "4: UserA",
    "5: UserB"
);

my ($dataFileContents, $currentCategory);

for (@array) {
    $currentCategory = $1 if (/(Category[A-Z])--/);
    if (/(\d+): (User[A-Z])/) {
        $dataFileContents->{$2}->{$currentCategory} = $1
    }

}
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-07-29 09:46

Not exactly trying to golf here, but it can be done in a single alternation:

my ( %data, $category );
while ( <DATA> ) { 
    next unless /^(?:(Category\w+)|(\d+):\s*(User\w+))/;
    ( $1 ? $category = $1 : 0 ) or $data{$3}{$category} = $2;    
}

Data::Dumper (actually Smart::Comments) shows the output:

{
  UserA => {
             CategoryA => '5',
             CategoryB => '4'
           },
  UserB => {
             CategoryA => '6',
             CategoryB => '5'
           },
  UserC => {
             CategoryA => '7'
           }
}    
查看更多
Emotional °昔
5楼-- · 2019-07-29 10:06
#!/usr/bin/perl

use strict;
use Data::Dumper;

print "Content-type: text/html\n\n";

my ($x,%data);
do {
    if (/^(Category\w+)/) {
        $x=$1;
    } elsif (/^([0-9]+):\s*(User\w)/) {
        if (!defined($data{$2})) {
            $data{$2} = {$x,int($1)};
        } else {
            $data{$2}{$x} = int($1);
        }
    }   
} while (<DATA>);

print Dumper \%data;


__DATA__
CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB

RESULT:

$VAR1 = {
    'UserC' => {
        'CategoryA' => 7
                 },
    'UserA' => {
        'CategoryA' => 5,
        'CategoryB' => 4
                 },
    'UserB' => {
         'CategoryA' => 6,
         'CategoryB' => 5
     }
};
查看更多
等我变得足够好
6楼-- · 2019-07-29 10:08

This will split it up for you.

prompt> ruby e.rb 
[["CategoryA--", nil, nil], [nil, "5", "UserA"], [nil, "6", "UserB"], [nil, "7", "UserC"], ["CategoryB--", nil, nil], [nil, "4", "UserA"], [nil, "5", "UserB"]]
prompt> cat e.rb 
s = <<TXT
CategoryA--
5: UserA
6: UserB
7: UserC
CategoryB--
4: UserA
5: UserB
TXT
p s.scan(/(^.*--$)|(\d+): (.*$)/)
prompt> 
查看更多
登录 后发表回答