Gossamer Forum
Home : General : Perl Programming :

Simple pattern match

Quote Reply
Simple pattern match
I need to match a number that can be in the following format (and still be correct)

2.2
2.20
2.200
2.2000
2.20000

$bob = "2.201"; #this should test wrong

if ($bob =~ /2\.20.*/) {
print "right";
}
else {
print "wrong";
}

This returns right (because my code isn't correct) What is the syntax for "followed by 'none or more' Zero's" (being zero's only)?

I tried [0.*] but it still didn't work. I'm having a brain fart.
Quote Reply
Re: [Watts] Simple pattern match In reply to
You need to match to the end of the string, so something like /^2\.20*$/ should do the trick.

Adrian
Quote Reply
Re: [Watts] Simple pattern match In reply to
Quote:
I need to match a number
The operator you are looking for is ==.
Code:
print 2.2==2.20,2.200==2.20000,"\n";
print 2.2==2.21,2.201==2.20001,"\n";
11
(and of course the second line won't print anything)

Last edited by:

mkp: Nov 17, 2006, 5:18 AM
Quote Reply
Re: [mkp] Simple pattern match In reply to
Thanks for the input. The developer who wrote the software has a field that holds a number (a percentage actually) but the way the program is set up any number up to 5 decimal places can be entered. So, a user can put in anything from x to x.xxxxx

What I'm trying to do is test the field to see if it contains any of the following (and only the following):
0.00
0.50
1.00
1.25
1.50
1.75
2.15
2.40
3.35

however, it could be 3.35 or 3.350 or 3.3500 etc. So I'm trying to match a specific number followed by "None" or "More" Zeros to return true.

3.34x = false
3.35 = true
3.350 = true
3.3500 = true
3.351 = false
3.36x = false

The /3\.35.*/ should match "3.35 followed by none or more items" but I don't know how to tell it to match the number Zero ("none or more times").
Quote Reply
Re: [Watts] Simple pattern match In reply to
I don't understand why using == doesn't suffice. It does exactly what you said you need, with the extra inherent work on your part.

That said,
0*
will match 0 or more zeros. Likewise,
0{0,5}
will match between 0 and 5 zeros.

Last edited by:

mkp: Nov 19, 2006, 3:58 AM
Quote Reply
Re: [mkp] Simple pattern match In reply to
== does work, I must have been smoking crack or something. I really appreciate the help!