Search code examples
regexperlstrawberry-perl

How to extract a subsequent word after pattern match using perl regex?


Sample line from a file:

one two three uname whirlcano four five

I want to extract the user names from multiple files

foreach $file (@file_names)
{
    open(my $fh, '<:encoding(UTF-8)', $file)
    or die "Could not open file '$file' $!";

    while (my $row = <$fh>)
    {
        if($row =~  "uname")
        {
            #here I want to extract only the immediate word after "uname", which is "whirlcano" in the above example.
        }
    }
}

Thanks in advance.


Solution

  • You can use a regular expression capturing group to capture the username:

    while (my $row = <$fh>) {
        chomp($row); # strip newline character
    
        # capture first group of word characters after uname and one or more spaces
        my ($username) = $row =~ m/uname\s+(\w+)/;
        ....
    }
    

    You can change \s+ in the above example to whatever delimiter your file has, but typically when parsing CSV type files using a real CSV parser is better than regular expressions. Text::CSV_XS is one such parser that is popular.

    For more information about capturing groups in regular expressions, please refer to the Capture Groups section of perldoc perlre