ocehb: (Default)
ocehb ([personal profile] ocehb) wrote2009-05-13 10:55 pm
Entry tags:

Работа с именованными группами в регулярных выражениях perl.

синтаксис: (?<name>выражение), обратная ссылка \k<name>, ссылка в коде $+{name}.


принимает данные на стандартный вход, аргументы — имена групп: addr, date, method, uri, proto, code, size, referer и agent

# perl -le 'while (<STDIN>) { m#^(?<addr>\S+) - - \[(?<date>.+?)\] "(?<method>\w+) (?<uri>\S+) (?<proto>\w+/\d+\.\d+)" (?<code>\d+) (?<size>\d+|-) "(?<referer>.+?)" "(?<agent>.+?)"# && print join "\t",@+{@ARGV}; }' addr referer < access_log
...


печатает через таб адрес клиента и реферер.

или, более красиво:

#! perl -l

my $re = qr/^(?<addr>\S+) - - \[(?<date>.+?)\] "(?<method>\w+) (?<uri>\S+) (?<proto>\w+/\d+\.\d+)" (?<code>\d+) (?<size>\d+|-) "(?<referer>.+?)" "(?<agent>.+?)"/;

while (<STDIN>) {
    $_ =~ $re && print join "\t",@+{@ARGV};
}


Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org