Deano Deano - 2 years ago 79
Perl Question

How to extract unix path in perl


[security] [client] [domain] [200] [/apache/20160503/20160503-0636/20160503-063628-Vyh-LH8AAAEAAE6zC@AAAAAD] (null)

Desired output


here is what I have so far


My Perl code.

use feature 'say';

$txt='[modsecurity] [client] [domain] [200] [/apache/20160503/20160503-0636/20160503-063628-Vyh-LH8AAAEAAE6zC@AAAAAD] (null)';


if ($txt =~ m/$re/is)
say $1;



I think my regex is messy? maybe another way?


Answer Source

I would use a split too... or a more general regex than the one you are using:

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $data = '[security] [client] [domain] [200] [/apache/20160503/20160503-0636/20160503-063628-Vyh-LH8AAAEAAE6zC@AAAAAD] (null)';

my @fields = $data =~ /(?:\[(.*?)\])+/g;

print Dumper(\@fields);

The output you get is:

$VAR1 = [

So the fifth element of the returned array is what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download