1

Okay, I tried some options but I am not getting it right - looks like its a problem with my regex specification but it might be other syntaxes as well. Any help / direction is very much appreciated.

I am trying to read a CSV file and processing one line at a time - discarding the Header line. I will focus on particularly two fields in that file.

Now after I read the file one line at a time, I am trying to process the two fields as such:

while ( my $line = <$data> ) {
    chomp $line;
    if ( $line !~ /^Date/ ) {
        if ( $line =~ /"/ ) { $line =~ s|"||g }

        ...;

        my $homeTeam = getTeam( $fields[5] );
        my $awayTeam = getTeam( $fields[7] );

        ...;

        my $arbiterRec = join ",", $gameDate, $gameTime, "", $season, $gameLevel,
            $homeTeam, "", $awayTeam, "", $site, $subSite, "", "";
        print "$arbiterRec\n";
    }
}

sub getTeam {
    my ($team) = trim( $_[0] ) =~ m{(R\d+-\d+B|G\d+$)}x;
    return $team;
}

sub trim {
    ( my $s = $_[0] ) =~ s/^\s+|\s+$//g;
    return $s;
}

With this, if I have an input like (fields of interests marked with ^^^):

mm/dd/yyyy, hh:mm AA, dd, Aaaaaa, aaD, R35-14G1, , U14 Girls Area Schedule R256-14G1, , AAA, , , 
                                       ^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I expect to get an output like:

mm/dd/yy, hh:mm AA, dd, Aaaaaa, aaD, R35-14G1, , R256-14G1, , AAA, , , 
                                     ^^^^^^^^    ^^^^^^^^^

In stead what I am getting is:

mm/dd/yy, hh:mm AA, dd, Aaaaaa, aaD, G1, , G1, , AAA, , , 
                                     ^^    ^^

Any idea what I might be doing wrong in the syntax or RegEx match?

1 Answer 1

1

Just change your regex to,

(R\d+-\d+(?:B|G)\d+$)

What's the actual problem is (R\d+-\d+B|G\d+$) regex first check for the words starts with R followed by one or more digits again followed by - and finally B at the last. But in your input there isn't a word like this. So this would fail. Next it goes to the second part that starts with G, finally it matches the last G and the following one or more numbers.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Avinash for explaining the problem +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.