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";
}
| [reply] [d/l] [select] |
|
|
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 | [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
if (//g) makes no sense and it's a bug. It makes even less sense when negated. Get rid of the g.
| [reply] [d/l] [select] |
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.
| [reply] [d/l] |
|
|
$ 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>
| [reply] [d/l] |
|
|
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.
| [reply] |
|
|
if ( ($var =~ /^\s+$/) or ($var eq "") ){
print "\n All \n";
}else{
print "\n Nothing \n";
}
| [reply] [d/l] |
|
|
|
|
|
|
I think this will do it:
<code>
if ( ($var =~ /^\s+$/) or ($var eq "") ){
print "\n All \n";
}else{
print "\n Nothing \n";
}
| [reply] |
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/
| [reply] [d/l] |