SitePoint Sponsor

User Tag List

Page 4 of 9 FirstFirst 12345678 ... LastLast
Results 76 to 100 of 219

Thread: PHP Quiz

  1. #76
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To keep things moving...
    I'm not the curent question answerer, but its been a few hours and...
    Look back at the regular expression question earlier in this thread.
    Quote Originally Posted by cyborg from dh
    i need a regex to match any numerical number, (including a decimal)
    Here was the answer:
    Quote Originally Posted by Paul S
    Code:
    #^[0-9]+(\.[0-9]+)?$#
    A good answer! However, I have a challenge for it. Consider this a time passer, while we wait for an official trivia question:

    Under what circumstance would that expression fail to match a valid number?

    How could the regular expression be modified to include those numbers?

    Bonus (I'm just guessing this is possible): How could you check to see if you had a valid number or not without using regex?

    Aside from the first of those questions, I don't know the answers. So, after awhile, I will attempt to answer as well.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  2. #77
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a LAMP guy for servers, WAMP for localhost

  3. #78
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe it would fail on numbers using , seperators for 3-digit sets, as well as numbers using spaces for seperators (or any other seperator for that matter).

    Although I didn't want to take that into consideration with that regex.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  4. #79
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Negative numbers, no clue, and is_numeric().
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  5. #80
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    I believe it would fail on numbers using , seperators for 3-digit sets, as well as numbers using spaces for seperators (or any other seperator for that matter).

    Although I didn't want to take that into consideration with that regex.
    I should have been more clear... I wasn't thinking about any sort of thousands or millions separator, I was thinking of a number entered with just digits and a period signifying a decimal point.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #81
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    I should have been more clear... I wasn't thinking about any sort of thousands or millions separator, I was thinking of a number entered with just digits and a period signifying a decimal point.
    Great observation!!

    Here's the improved version
    Code:
    #^[0-9]+(\.[0-9]+)?|\.[0-9]+$#
    Enjoy it
    Paul

  7. #82
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both of 'em match "111." period followed by no digits. I'm not sure, but would that qualify it as "failing"?
    Nasir
    nasir.us

  8. #83
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anode
    Negative numbers, no clue, and is_numeric().
    Yes! Negative numbers and numbers beginning with a decimal are the two instances I had in mind. The latter makes the regex problem quite a bit harder.

    And yes, is_numeric should circumvent the need for a regular expression.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  9. #84
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, is_numeric() would work fine for just checking a value, I was looking for every number in a string.

    Good point on the .123 and the -.332 though
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  10. #85
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm, though public
    Ok, a simpler version
    Code:
    #^\-?([0-9]+)?(\.[0-9]+)?$#
    Matches negative/positive, integer or decimal numbers. Integers with a decimal point would not match.

    Paul

  11. #86
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #^\-?(\d+)?(\.\d+)?$#
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  12. #87
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    much better! who's next then?

  13. #88
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Paul S
    much better! who's next then?
    You!

    I like regex questions (hint! )

  14. #89
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Joel, you are getting your wish. In spades.

    There's another whole can of worms that's been opened:
    Quote Originally Posted by cyborg from dh
    yeah, is_numeric() would work fine for just checking a value, I was looking for every number in a string.
    If you are looking for every number inside a string that may contain other data mixed in, then this business will not work: #^stuff$#
    The ^ and $ indicate beginning and end of the string so you will only match if the number is the entire length of the string not to mention that you would not match several numbers in a string separated by other data.

    Also, if you are testing for a number inside a string, then you are going to have to account for:
    The 7 of us are going to the concert at 9. The price of each ticket is $9.50.

    Very tricky.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  15. #90
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a relatively simple regex question but I have to answer a question first.
    Aaron Brazell
    Technosailor



  16. #91
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forget we haven't had a question from sojomy yet

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  17. #92
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    Joel, you are getting your wish. In spades.

    There's another whole can of worms that's been opened:

    If you are looking for every number inside a string that may contain other data mixed in, then this business will not work: #^stuff$#
    The ^ and $ indicate beginning and end of the string so you will only match if the number is the entire length of the string not to mention that you would not match several numbers in a string separated by other data.

    Also, if you are testing for a number inside a string, then you are going to have to account for:
    The 7 of us are going to the concert at 9. The price of each ticket is $9.50.

    Very tricky.
    Yep capturing every number will be a major task. How do you propose that be done (with a regex - it's not that difficult going by it programmatically).

    But the "testing for a number inside a string" issue I don't quite get - isn't it as simple as removing the ^ and $ anchors? I must have misunderstood your point

  18. #93
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by redemption
    But the "testing for a number inside a string" issue I don't quite get - isn't it as simple as removing the ^ and $ anchors? I must have misunderstood your point
    Pretty much... I just meant that everything presented so far using /^ $/ style would be inefective at grabbing anything except an entire string. The real problems added are the number at the end of the sentence.

    Quote Originally Posted by redemtion
    Yep capturing every number will be a major task. How do you propose that be done (with a regex - it's not that difficult going by it programmatically).
    I actually thought this was the original question way back when and struggled with it for about 10 minutes, futile efforts involving lookbacks and such.

    After looking at it again for 30 seconds, this popped into my head:
    #-?\d+\.\d+|-?\.?\d+#
    I think that covers everything mentioned so far. I think.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  19. #94
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind. Heh, just opened my eyes and looked back at the the newest regexs, and they are functional equivalents. Not sure what I was thinking was wrong with them before. First signs of madness, I'm sure.

    Edit:
    Heh, just looked again and found a reason why mine (assuming it works) is better. This:
    Code:
    #\-?(\d+)?(\.\d+)?#
    ...would match any hyphen, regardless if a number followed it.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  20. #95
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I meant capturing (as in regex terminology) every number.

    EDIT: just to make it clearer, what I meant was: is it possible to find out exactly what numbers are there in a string, by capturing them in a regex?

  21. #96
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by redemption
    I meant capturing (as in regex terminology) every number.

    EDIT: just to make it clearer, what I meant was: is it possible to find out exactly what numbers are there in a string, by capturing them in a regex?
    In the one I've posted the entire match would be the number, so it would take a loop in Perl or a preg_match_all($regex, $source, $matches) in PHP to get all numbers (there is an automatic capture on the entire match, I believe). Is that what you meant by programatic?

    Strictly in regex, would this work?
    #/(.*?(-?\d+\.\d+|-?\.?\d+))+#s
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  22. #97
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by samsm
    In the one I've posted the entire match would be the number, so it would take a loop in Perl or a preg_match_all($regex, $source, $matches) in PHP to get all numbers (there is an automatic capture on the entire match, I believe). Is that what you meant by programatic?

    Strictly in regex, would this work?
    #/(.*?(-?\d+\.\d+|-?\.?\d+))+#s
    Yep something like what I meant by programmatic (preg_match_all() cheats! ).

    I think it only captures the substring that was actually last matched? Not sure, but that's what I've always thought. It doesn't capture as many substrings as the '+' allows does it? Just once.

  23. #98
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct, #/(.*?(-?\d+\.\d+|-?\.?\d+))+#s just doesn't work, for the reason you mentioned as well as some others. Oh well, that's what loops are for I suppose.

    Anyway, that's probably enough on that little extension of question number two.

    When do we suppose sojomy will arrive bearing the next question?
    Last edited by samsm; Apr 12, 2003 at 10:55.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  24. #99
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hopefully soon, otherwise we can go with Aaron's regex question

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  25. #100
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about e ? very valid number format 12.6e34 - would that work?

    another non-regex validatory technique would be

    echo (($numba*1) == $numba) ? 'pass' : 'fail' ;

    though knowing how most languages handle larger numbers that would probably fail in certain instances.
    teckis - that's news to me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •