Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What am I missing in perlre

Reply
Thread Tools

What am I missing in perlre

 
 
janfure@gmail.com
Guest
Posts: n/a
 
      06-15-2006
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

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      06-15-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> 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


The 'x' modifier ignores whitespace and comments *in the pattern*, not in
the target string. It allows you to write stuff like this:

m/
\w # One alphanumeric character
\d* # Followed by zero or more digits
/x;

> What am I missing, is there a better way to match alphabetical letters
> than /\D/x?


m/[[:alpha:]]/;

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-15-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:

> #!/usr/local/bin/perl -w


use warnings; # the modern way, much better than -w



> if ($_ =~ m/\D/x){


> This script does not do what I expected, the presence of the space in
> the string seems to cause a match with /\D/x,



Yes, because space _is_ a non-digit character.


> despite the 'x' modifier,
> which causes the regex engine to ignore MOST whitespace,



*in the pattern*. (not in the string that the pattern is to match against)

spaces are ignored in the _pattern_

m/ \D /x

is equivalent to writing the pattern the way you originally did.

The m//x modifier does not change the meaning of any of
the regex backslash escape shortcuts.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
janfure@gmail.com
Guest
Posts: n/a
 
      06-20-2006

Sherm Pendley wrote:

> m/[[:alpha:]]/;
>
> sherm--
>

-----------------------<SNIP>---------------------------
Thanks, that did the job.

Jan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Crystal Reports - Visual Basic UFL that implements this function is missing (or U2lcom.dll is missing) Les Caudle ASP .Net 3 09-03-2007 02:27 AM
Boolean Regexp with perlre Kevin Crosbie Perl Misc 12 09-14-2006 05:18 PM
Re: missing feature classes and missing fields Gary Herron Python 2 07-04-2006 10:29 PM
Missing Websites and no mail =?Utf-8?B?U2hpZnR3b3JrZXI0Mw==?= Wireless Networking 0 09-11-2004 12:31 PM
perldoc incompetence: perlre /s Xah Lee Perl Misc 18 04-15-2004 02:47 AM



Advertisments