crusty_collins has asked for the wisdom of the Perl Monks concerning the following question:
$line =~ /pattern\s*=*\s*([\w\d\s\\\/\.\-\@\!\$\%\^\&\*\:\;\,\<\>]+)/
I know that this is NOT the way to go about it. I have tried to get everything but after the comment like this.
but it is not working.$line =~ /pattern\s*=*\s*(.+?)\s+[^#]/
QUESTION. What is the proper regex to do this?
use strict; use warnings; my $count; my $env = []; foreach my $line ( <DATA> ) { chomp($line); next if $line =~ /^\s*\#/; next if $line =~ /^$/; # file is in the format of # [zipcode] # regex (?<Zip>\d{5})-(?<Sub>\d{4}) # pattern 95076-1234 # pattern 90210-6473 # [IP] # regex = (?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0 +-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?< +Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?) # pattern = 255.257.0.0 # invalid # # if ($line =~ /^\[([\w\s\\]+)\]/ ) { my $tag = $1; $count = scalar @{$env}; $env->[$count]->{tag} = $tag; } elsif ( $line =~ /regex\s*=*\s*(.+)|regex\s*=*\s*/ ) { my $regex = $1; if ( $env->[$count]->{'regex'} ) { $env->[$count]->{'regex'} .= $regex; }else{ $env->[$count]->{'regex'} = $regex; } } # comments are ignored elsif ( $line =~ /pattern\s*=*\s*([\w\d\s\\\/\.\-\@\!\$\%\^\&\ +*\:\;\,\<\>]+)|pattern\s*=*\s*/ ){ my $pattern = $1; push( @{$env->[$count]->{pattern}} , $pattern); } } __DATA__ [IP] regex = (?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0 +-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0 +-4]\d|25[0-5]|[01]?\d\d?) pattern = 255.257.0.0 # invalid pattern = 192.168.1.1
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regex to ignore comment
by AppleFritter (Vicar) on Oct 20, 2015 at 18:05 UTC | |
by crusty_collins (Friar) on Oct 20, 2015 at 19:32 UTC | |
by AppleFritter (Vicar) on Oct 20, 2015 at 20:19 UTC | |
by Laurent_R (Canon) on Oct 20, 2015 at 20:17 UTC | |
by Corion (Patriarch) on Oct 20, 2015 at 20:18 UTC | |
Re: Regex to ignore comment
by Corion (Patriarch) on Oct 20, 2015 at 17:24 UTC | |
by crusty_collins (Friar) on Oct 20, 2015 at 17:53 UTC | |
by Corion (Patriarch) on Oct 20, 2015 at 18:03 UTC | |
Re: Regex to ignore comment
by u65 (Chaplain) on Oct 20, 2015 at 20:47 UTC | |
by crusty_collins (Friar) on Oct 21, 2015 at 13:48 UTC | |
Re: Regex to ignore comment
by Laurent_R (Canon) on Oct 20, 2015 at 17:18 UTC |