RonW has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Wise Ones

I am parsing data files which include lines like:

123/; #<
456/; #< a comment

The simplified version of my regex is:

/\s([#]\S)(.*)/

In my tests, the first line was not matched, but the second line was.

what am I overlooking?

I am using Active Perl 5.16.3 as this is what is currently approved by my employer's IT department.

Replies are listed 'Best First'.
Re: capturing optional text with a regex
by toolic (Bishop) on Mar 31, 2014 at 20:55 UTC
    They both match for me on:
    This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-li +nux-thread-multi

    Code:

    use warnings; use strict; while (<DATA>) { print if /\s([#]\S)(.*)/; } __DATA__ 123/; #< 456/; #< a comment

    Output:

    123/; #< 456/; #< a comment
      Thanks. Looking deeper in to my set up.
Re: capturing optional text with a regex
by kcott (Archbishop) on Apr 01, 2014 at 12:32 UTC

    G'day RonW,

    Welcome to the monastery.

    You really need to show your real test (and its results) or a short, self-contained example which reproduces what you're describing.

    I'm wondering whether you're testing the truth of the match or the truth of the captures. Here's an example:

    #!/usr/bin/env perl -l use strict; use warnings; while (<DATA>) { if (/\s([#]\S)(.*)/) { print 'MATCH: TRUE'; if ($1 && $2) { print '$1 && $2: TRUE'; } else { print '$1 && $2: FALSE'; } print "\$1='$1'"; print "\$2='$2'"; } else { print 'MATCH: FALSE'; } } __DATA__ 123/; #< 456/; #< a comment

    Output:

    MATCH: TRUE $1 && $2: FALSE $1='#<' $2='' MATCH: TRUE $1 && $2: TRUE $1='#<' $2=' a comment'

    -- Ken