in reply to Re: Regular Expression Help!
in thread Regular Expression Help!

I tried but don't like this:
my $x = ""; if($x!~//g) { print "\n There is true data\n"; }

and this:
my $x = " "; # just spaces if($x!~/\s+/g) { print "\n There is true data\n"; }

Thanks

Replies are listed 'Best First'.
Re^3: Regular Expression Help!
by kennethk (Abbot) on Jul 02, 2010 at 19:34 UTC
    A couple of critiques for your expressions:
    • Since you are only testing once, you do not need the g modifier.
    • // will match everything. Hence $x!~// will always return false.
    • /\s+/ will match if your string contains at least one space. Hence $x!~/\s+/ will return false if your string contains at least one space.
    • Changing $x!~/\s+/ to $x!~/\S+/ will return true when the string does not contain at least one non-whitespace character, and hence will make your test work.
    • You could use anchors (^ and $) and * to do what you intend as well with $x=~/^\s*$/. This translates as requiring that between the start (^) and end ($) there is exactly 0 or more (*) whitespace characters (\s).

    I would suggest reading through perlretut to gain familiarity with this tool.

Re^3: Regular Expression Help!
by ikegami (Patriarch) on Jul 02, 2010 at 19:42 UTC
    if (//g) makes no sense and it's a bug. It makes even less sense when negated. Get rid of the g.