SUPER QUICK HELP needed for short regex!

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

SUPER QUICK HELP needed for short regex!

Post by seodevhead »

Code: Select all

'([0-9]){1,2}(\.)?([0-9]){1,2}'
I just want to make sure the user types in a regular number... but this RegEx is allowing:

16..2 (two decimals)

How can I correct this to allow only 1 decimal? I thought that is what the ? is for?? Thanks!
bundyo
Forum Newbie
Posts: 12
Joined: Tue Jan 03, 2006 3:09 pm

Post by bundyo »

Umm,

as far as i can see it - ([0-9]){1,2} matches a digit one or two times, then (\.)? matches optional dot, then again digit one or two times...

However i didn't understand exactly what are you trying to match?
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

I just want to know how I can ammend this RegEx that I already have to not allow 2 dots like I example in the first post. I dont understand why 2 dots are allowed.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

That is because if you use this with preg_match it will not work how you think...

It will return true as soon as it finds that matches the regular expression, even timvw1.2 would be valid.
Untested, but i think the following would be what you need:

^([0-9]){1,2}(\.)?([0-9]){1,2}$
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Actually I am using eregi() to test the regex. So was my flaw leaving out the ^ at the start and $ at the end? It just seems odd to me that 2 dots are being accepted like this.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You are only testing if the regular expression is matched... So as soon as you had two numbers in the value, it would have been accepted... No matter where they were somewhere in the string..

With the ^ and $ you can enforce there is nothing "extra" :)
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Even using:

'^([0-9]){1,2}(\.)?([0-9]){0,2}$'

still allows 16..2 as input. (two dots).

Still can't understand why...
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If you use posix regular expressions, you should lookup the syntax yourself..

With preg_match it works as expected:

timvw@madoka:~$ cat test.php

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);

if (preg_match("#^([0-9]){1,2}(\.)?([0-9]){0,2}$#", "16..2")) {
        echo "there is a match\n";
} else {
        echo "no match\n";
}
?>

Code: Select all

timvw@madoka:~$ php test.php
no match
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Man... I must live in like a 3rd dimension or something.. cause I copied the exact code you pasted into my script and it is still accepting 16..2 (2 dots). I cannot understand why. It is NOT accepting 9..2 thought... which is correct.. but why is it accepting 2 numbers and 2 dots is unreal, because I thought the ? is what says that the dot must either not come or only come once. I really appreciate your help tim.. especially taking the time to test in on your system. Unfortunatly the regex gods don't like my server. :( Not sure what to do.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

if (strval(floatval($_POST['num'])) === $_POST['num']) {
    //number is a float..
} else {
    //number is not a float..
}

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There is always is_numeric()

Mac
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

You have to remember that is_numeric() aint foolproof because it allows things like:

"20"
'20'
4e20
etc

All are numeric because they represent numbers. Not to mention 1....4 is a number as well.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

all three examples you just gave are numbers. ;)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply