Perl regex question

I wrote a Perl program that reads text from a text file and prints it.

I want to print a string with a specific format.

For example, there are lines like this:

information:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.

I want to read only these three lines:

Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir

I think the meaning of the fields is:

Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file

My code is:

#!/usr/bin/perl
use strict;
use warnings;

open (my $infh, "<", $file)||die "cant open";

while(my $line = <$infh>) {
    chomp ($line);
    if ($line =~ /(what regular expression do I have to put in here?)/) {
        print "$line";
    }
}

close ($infh);
+3
source share
5 answers

If the lines you always need end in / Essay, you can use the following regular expression

/:\/Essay$/

Edit 1 : it looks like the middle parts are just numbers, you can follow this path.

/:\d+:\d+:\d+:\d+:/
+4
source
   $line =~ m{
   ^
   (\w+\.\w+) # filename stored as $1
   :
   (\d+) # size stored as $2
   :
   (\d{4}) # year stored as $3
   :
   (\d+) # month stored as $4
   :
   (\d+) # day stored as $5
   :
   (.*) # path stored as $6
   $
   }x
+1
source

Ahmad.prn: 592118: 2001: 7: 5:/Essay

Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file

/^\s*(\S+):(\d+):(\d+:\d+:\d+):(\S+)\s*$/

$1, $2, $3, $4

I added extra spaces to the beginning and end of the line if you want to allow extra spaces after or after: you can add \ s *

+1
source
#!/usr/bin/perl
use strict;

my $inputText = qq{
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.
};

my @input = split /\n/, $inputText;
my $i = 0;
while ($input[$i] !~ /^end of Information.$/) {
    if ($input[$i] !~ /:/) {
        $i++;
        next;
    }
    my ($fileName, $fileSize, $year, $month, $day, $filePath) = split /:/, $input[$i];
    print "$fileName\t $fileSize\t $month/$day/$year\t $filePath\n";
    $i++;
}
+1
source
$line =~ ([a-zA-Z.]+):(\d+):(\d+):(\d+):(\d+):([\/A-Za-z]+)

$name = $1; #Ahmad.prn
$id = $2; #592118
$year = $3; #2001
$dir = $6; #/Essay 

Note: skip it for multiple names

+1
source

Source: https://habr.com/ru/post/1796484/


All Articles