J
janfure
I wrote a script for detecting alphabetical/non numeric characters:
#!/usr/local/bin/perl -w
use strict;
$_ = '2 3';
if ($_ =~ m/\D/x){
print "$_ has non numerical character(s)!\n";
}
$_ = '2A 3';
if ($_ =~ m/\D/x){
print "$_ has non numerical character(s)!\n";
}
This script does not do what I expected, the presence of the space in
the string seems to cause a match with /\D/x, despite the 'x' modifier,
which causes the regex engine to ignore MOST whitespace, but maybe not
the one that mattered to me.
This script (stripping out spaces from $_) does what I expected the
first one to do:
#!/usr/local/bin/perl -w
use strict;
$_ = '2 3';
$_ =~ s/\s+//g;
if ($_ =~ m/\D/){
print "$_ has non numerical character(s)!\n";
}
$_ = '2A 3';
$_ =~ s/\s+//g;
if ($_ =~ m/\D/){
print "$_ has non numerical character(s)!\n";
}
What am I missing, is there a better way to match alphabetical letters
than /\D/x?
Jan
#!/usr/local/bin/perl -w
use strict;
$_ = '2 3';
if ($_ =~ m/\D/x){
print "$_ has non numerical character(s)!\n";
}
$_ = '2A 3';
if ($_ =~ m/\D/x){
print "$_ has non numerical character(s)!\n";
}
This script does not do what I expected, the presence of the space in
the string seems to cause a match with /\D/x, despite the 'x' modifier,
which causes the regex engine to ignore MOST whitespace, but maybe not
the one that mattered to me.
This script (stripping out spaces from $_) does what I expected the
first one to do:
#!/usr/local/bin/perl -w
use strict;
$_ = '2 3';
$_ =~ s/\s+//g;
if ($_ =~ m/\D/){
print "$_ has non numerical character(s)!\n";
}
$_ = '2A 3';
$_ =~ s/\s+//g;
if ($_ =~ m/\D/){
print "$_ has non numerical character(s)!\n";
}
What am I missing, is there a better way to match alphabetical letters
than /\D/x?
Jan