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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Regular Expression Help!
by kennethk (Abbot) on Jul 02, 2010 at 19:19 UTC
    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"; }
      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.
Re: Regular Expression Help!
by cdarke (Prior) on Jul 02, 2010 at 19:23 UTC
    if a variable is empty and has only spaces

    Strictly speaking a variable can be empty (value is undef) or contains only spaces:
    if (defined $var or $var =~ /^\s+$/) { # $var is empty or contains only whitespace }
    The \s actually tests for any white-space character, including tabs and new-lines.
      a variable can be empty (value is undef)

      <nit>

      The string could be undefined or defined and contain a zero-length string before we move on to containing only spaces or some other text.

      $ perl -Mstrict -wE ' > sub grok > { > my $str = shift; > say > defined $str > ? $str > ? qq{>$str<: defined and true} > : qq{>$str<: defined but not true} > : qq{>$str<: undefined}; > } > > my $str; > grok( $str ); > $str = q{}; > grok( $str ); > $str = q{ }; > grok( $str ); > $str .= q{ABC}; > grok( $str );' Use of uninitialized value $str in concatenation (.) or string at -e l +ine 5. ><: undefined ><: defined but not true > <: defined and true > ABC<: defined and true $

      </nit>

      Cheers,

      JohnGG

      but $var=""; and $var="data"; will both the in this IF, it doesn't work, I am trying to say if it is defined but empty move on.
        I think this will do it:
        if ( ($var =~ /^\s+$/) or ($var eq "") ){ print "\n All \n"; }else{ print "\n Nothing \n"; }
        I think this will do it:
        <code> if ( ($var =~ /^\s+$/) or ($var eq "") ){ print "\n All \n"; }else{ print "\n Nothing \n"; }
Re: Regular Expression Help!
by JavaFan (Canon) on Jul 05, 2010 at 09:31 UTC
    First of all, most Perl coders will not assume that "empty" means "may contain spaces". Empty is just that, void of everything. So, and "empty string" is usually understood to be a string containing no characters at all: its length is zero. This includes the undefined value, as undef in string context acts like a string of zero length.

    So, your question becomes, how do I match strings that contain nothing but spaces? Translated into regexp speak, all you want to encounter between the beginning and end of a string is spaces. Translated to code, that is:

    /\A *\z/