Как насчет использования классов с отрицательными символами:
/^S(\d),F(\d)\s+([^()]*?)\s+\(([^()]+)\)\s+([^,]*),([^,]*)(?:,(.*?))?$/
При включении в этот скрипт:
#!/bin/perl
use strict;
use warnings;
while (<>)
{
chomp;
my($s,$f,$title,$abbr,$single,$here,$reply) =
$_ =~ m/^S(\d),F(\d)\s+([^()]*?)\s+\(([^()]+)\)\s+([^,]*),([^,]*)(?:,(.*?))?$/;
$reply ||= "<no reply>";
print "S$s F$f <$title> ($abbr) $single : $here : $reply\n";
}
И запуск на исходном файле данных, он производит:
S1 F2 <title including several white spaces> (abbr) single : Here<->There : reply
S1 F2 <title including several white spaces> (abbr) single : Here<->There : <no reply>
S1 F2 <title including several white spaces> (abbr) single : Here<->There : [reply]
Вы, вероятно, должны также использовать суффикс 'xms' к выражению, чтобы вам было проще документировать его:
#!/bin/perl
use strict;
use warnings;
while (<>)
{
chomp;
my($s,$f,$title,$abbr,$single,$here,$reply) =
$_ =~ m/^
S(\d) , # S1
F(\d) \s+ # F2
([^()]*?) \s+ # Title
\(([^()]+)\) \s+ # (abbreviation)
([^,]*) , # Single
([^,]*) # Here or There
(?: , (.*?) )? # Optional reply
$
/xms;
$reply ||= "<no reply>";
print "S$s F$f <$title> ($abbr) $single : $here : $reply\n";
}
Признаюсь, я все еще могу писать однострочных монстров - яЯ пытаюсь исправить мои пути.