G'day halak77,

Welcome to the Monastery.

"/^\s*[^#].*/"

[^#] is a character class containing all characters except '#'. What you really want is a character class containing only '#'.

The first line doesn't match: (^) - line start OK; (\s*) - match zero whitespace OK; ([^#]) - '#' is a '#' FAIL

The second line does match: (^) - line start OK; (\s*) - match zero whitespace OK; ([^#]) - '' is NOT a '#' OK

I'll leave you to continue through the remaining lines of data.

I'll also point out that the '.*' (at the end of the regex) is superfluous. It will always match: either none, one or some of any character matched by '.'. You're only interested in the front of the string, anyway.

Here's my test (pm_1124511_comment_removal.pl):

#!/usr/bin/env perl use strict; use warnings; my $comment_line_re = qr{ \A \s* [#] }x; while (<DATA>) { print "@{[/$comment_line_re/ ? 'IS' : 'NOT']} Comment: $_"; } __DATA__ # This file is a comment and should not be used # This comment has one leading space # This comment has three leading spaces /dev/mapper/centos-var_log /var/log xfs defaults + 1 2 /dev/donstest /expert none 1 1 -rw,noglob # this is a fs #/dev/donstest22 /expert none 1 1 -rw,noglob # this is a + fS

Output:

$ pm_1124511_comment_removal.pl IS Comment: # This file is a comment and should not be used IS Comment: # This comment has one leading space IS Comment: # This comment has three leading spaces NOT Comment: /dev/mapper/centos-var_log /var/log xfs + defaults 1 2 NOT Comment: /dev/donstest /expert none 1 1 -rw,noglob # +this is a fs IS Comment: #/dev/donstest22 /expert none 1 1 -rw,noglob + # this is a fS

-- Ken


In reply to Re: regex whitespace quantifers by kcott
in thread regex whitespace quantifers by halak77

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.