For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2004 > Regexp match question !









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Regexp match question !
Daniel Stellwagen

2004-03-31, 3:34 am

Hi everybody,

I am a beginner of programming ( so I am a beginner of perl programming
too :-) ) and I have this very basic problem but cannot handle it.

I'm trying to match only a one single digit and wrote this code:


use strict;

my $number = 11; # two-digit number

if ( $number =~ /\d{1}\b/ ) {
print "Match\n";
} else {
print "No Match\n";
}

But I get a match !?
I also tried:

$number =~ /\d?\b/
because ? stands for no or one character

But that didn't work either.

Can someone give me a hint and maybe a source for more
basic dokumentation about reg exp.

Thanks for your time and have a nice day




Wc -Sx- Jones

2004-03-31, 6:31 am

Daniel Stellwagen wrote:
> my $number = 11; # two-digit number



my $number;

while ($number < (5*11)) {
++$number =~ /(\d)(\d)?/;
print "Seen $1 and $2 \tby looking at $number\n";
}

__END__

HTH/Sx
John W. Krahn

2004-03-31, 6:31 am

Daniel Stellwagen wrote:
>
> Hi everybody,


Hello,

> I am a beginner of programming ( so I am a beginner of perl programming
> too :-) ) and I have this very basic problem but cannot handle it.
>
> I'm trying to match only a one single digit and wrote this code:
>
> use strict;
>
> my $number = 11; # two-digit number
>
> if ( $number =~ /\d{1}\b/ ) {
> print "Match\n";
> } else {
> print "No Match\n";
> }
>
> But I get a match !?
> I also tried:
>
> $number =~ /\d?\b/
> because ? stands for no or one character
>
> But that didn't work either.
>
> Can someone give me a hint and maybe a source for more
> basic dokumentation about reg exp.


Your regular expression has a word boundary (\b) anchor after a single
digit character class (\d). That will match the digit '7' in the
following examples: ' 567 ', '567*&^%', '567', etc. You have to
anchor both sides of the expression you want to match:

if ( $number =~ /\b\d\b/ ) {

Or more probably:

if ( $number =~ /\A\d\z/ ) {


John
--
use Perl;
program
fulfillment
Wiggins D Anconia

2004-03-31, 10:32 am

> Hi everybody,
>


>
> Can someone give me a hint and maybe a source for more
> basic dokumentation about reg exp.
>


perldoc perlretut
perldoc perlre

The first is in a tutorial style and should be an easier read than the
more reference like second. You might also want to pickup "Learning
Perl" from O'Reilly, other than the excellent Perl information, it also
contains a beginners step into regexp.

Good luck,

http://danconia.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com