There are more than one approach to solve the problem.
OP's question mention regular expression, lets look at one variation of such regular expression.
We know that:
- first element in the line is username consisting of letters and digits -- we can apply
\w+
- then after space we have
pts/1 we can apply \S+
- next in line perhaps a name of a program or file consisting of
digits/dashes/dot/letters we can again apply
\S+
- next in line is a day of the week
\S+ will fit again nicely or \w+ also works well
- next in line is a month we can use
\S+ or \w+
- next in line is a day of the month again we can use
\S+ or '\d+`
- now we have a time which consist of hour
\d{1,2} followed by : and minutes \d{2} (perhaps start time)
- next is a separator
-
- and perhaps stop time consisting same parts as the start time, apply same pattern
- parts we would like to capture should be enclosed into
()
Description:
\d any digit
\d{min,max}, minimum and maximum following digits
\S anything but space
\S+ one or more symbols different from space
\w word character (letters, digits)
A construction of following structure can be utilized to declare and assign captured information to a list of variables
my($var1,$var2,$var3,...,$var#) = $data =~ /$re/;
In this particular case function split can assist in acquiring information of interest (first case: hour and minute as one block, second case hour and minute as separate entities). It added into the code for demonstration purpose that more than one solution possible.
use strict;
use warnings;
use feature 'say';
my $data = 'username1 pts/1 75-30-120-13.lig Wed Oct 12 19:20 - 19:25';
my $re = qr/(\w+) \S+ \S+ \S+ (\S+) \S+ (\d{1,2}):(\d{2}) - (\d{1,2}):(\d{2})/;
my($username,$month,$h1,$m1,$h2,$m2) = $data =~ /$re/;
say "$username $month, $h1:$m1 - $h2:$m2";
my($username1,$pts,$prog,$wday,$month1,$day,$start,$div,$end) = split(' ',$data);
say "$username1 $month1 $day, $start - $end";
my($username2,$month2,$day2,$h2_1,$m2_1,$h2_2,$m2_2) = (split(/[: ]/,$data))[0,4,5,6,7,9,10];
say "$username2 $month2 $day2, $h2_1:$m2_1 - $h2_2:$m2_2";
Output
username1 Oct, 19:20 - 19:25
username1 Oct 12, 19:20 - 19:25
username1 Oct 12, 19:20 - 19:25
Referense:
split,
qr//