in reply to Regular Expression Help!

What have you tried? What didn't work? See How do I post a question effectively?. Also, your node title is not very useful. See How do I compose an effective node title?. In particular, monks tend not to respond well to posts containing exclamation points.

As described in perlretut (the regular expressions tutorial), you can use the character class \s to test for white space. As well, as long as you don't use the x modifier (for in lining comments), you can just use a space character in your code. In your case, however it's probably easier to test for characters that are not whitespace using \S:

for ("Hello", " ", " x " ) { print /\S/ ? "'$_': Fail\n" : "'$_': Pass\n"; }

Replies are listed 'Best First'.
Re^2: Regular Expression Help!
by Anonymous Monk on Jul 02, 2010 at 19:24 UTC
    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
      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.

      if (//g) makes no sense and it's a bug. It makes even less sense when negated. Get rid of the g.