Search code examples
perlcut

perl cut variable after match


I have a variable in the directory where some logs are stored. I would like to get the word after "logs" in the path (kelvin and james for case below), the "logs" will be fixed among all paths.

Example path defined in variable

Both possible:
my $path1="/path/to/abc/logs/kelvin/";
my $path2="/path/to/abc/logs/james";

Solution

  • A match in list context returns the captured substrings, so we can use a match with a simple regex pattern to extract the desired portion.

    my ( $id ) = $path =~ m{/logs/([^/]*)};