programminglearner
programminglearner

Reputation: 542

Perl Matching Part of String

I would like to grab the date portion out of file paths so I may use them in a file name. For example, the files may be of the format:

$file1 = "/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my.text"  
$file2 = "/mypath/sd-urt7-afd-parent-79week46d-AFD-lk-my.text"  
$file3 = "/mypath/sd-urt7-ert-parent-79week50c-ERT-lk-my.text"  

Regardless of the file name formats from above, there is a portion in there that has the format "79weekxxX" where xx is a week number and X is a letter. I want to grab the portion "weekxxX" from each of these string names in order to create a final file name with just this string. For example, if my file is $file1, then I would like to store it in another variable such as week which contains week48a and create another variable just called $filename="$week.txt".

I'm thinking regex matching can be used to do this but I'm not quite sure how. Please let me know if any part of my question remains unclear so that I may further elaborate. Thank you.

Edit: The "79week" is always there. To be exact, I will not be accessing them as variables with file names. The paths are values of a nested hash for its own corresponding keys. So would it work to do something like:

 my %hash = ( 
        "rev" => {
            "first" => "$filepath1",  
            "second" => "$filepath2", 
            "third" => "$filepath3"                      
        },  
        "for" => {
            "first" => "$filepath1_2",  
            "second" => "$filepath2_2", 
            "third" => "$filepath3_2" 
        }   
    );

foreach my $inner (keys %{$hash{$outer}}){
my $file = basename($inner); //only takes the file name, and ignores the rest of the path
my ($week) = $file =~ /79week(\d\d)([a-zA-Z])/;
$filename = "$week.txt";
}

Upvotes: 2

Views: 184

Answers (1)

Booboo
Booboo

Reputation: 44313

General Observation:

If $file contains a string such as:

/mypath/sd-urt7-dfc-adfj345h-d0-79week48a-DFC-lk-my.text,

then if you execute:

$file =~ /79(week\d+[a-z])/i;

variable $1, which is set to the Group 1 match from the above regex, will contain (in the above example):

week48a

You can then assign my $week = $1;

I hope that answers you Regex-related question.

Your statement:

my ($week) = $file =~ /79week(\d\d)([a-zA-Z])/;

will only set $week to Group 1, or 48. So the other alternative, would be:

my ($week) = $file =~ /79(week\d\d[a-zA-Z])/;

Upvotes: 1

Related Questions