in reply to Multiline match for empty string

"(?!.*[^\n])" or "(?!.*\\S)" depending on how strict you want to be.

Replies are listed 'Best First'.
Re^2: Multiline match for empty string
by kennethk (Abbot) on Aug 05, 2010 at 22:47 UTC
    Either I misunderstand the spec, or your solution fails:

    #!/usr/bin/perl use strict; use warnings; my @tests = ("\% only 1 profile is allowed\n\n", # Should fail "\n\n", # Should pass "", # Should pass ); my $regex = "(?!.*\\S)"; for (@tests) { print $_ =~ qr{$regex}ms ? "$_\n REGEX MATCHED\n" : "$_\n FAILED!\ +n"; }

    Like with the bad OP proposal, your regex passes for all cases.

    Update: Fixed mistake in posted test, though it does not change the result. See below for corrected regular expression.

      I forgot to anchor. Should be

      "\\A(?!.*[^\n])" or "\\A(?!.*\\S)"

      Of course, the following are much simpler:

      "\\A[^\n]*\\z" or "\\A\\s*\\z"

      By the way, your test is wrong. You never actually attempt to match.