I have a regex that ignores the comment in a string.

$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.

$line =~ /pattern\s*=*\s*(.+?)\s+[^#]/
but it is not working.

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

In reply to Regex to ignore comment by crusty_collins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.