## User Tag List

1. 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.
Originally Posted by cyborg from dh
i need a regex to match any numerical number, (including a decimal)
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.

2. I'm a LAMP guy for servers, WAMP for localhost

3. 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.

4. Negative numbers, no clue, and is_numeric().

5. 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.

6. 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. Both of 'em match "111." period followed by no digits. I'm not sure, but would that qualify it as "failing"?

8. 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.

9. 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

10. 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. #^\-?(\d+)?(\.\d+)?\$#

12. much better! who's next then?

13. Originally Posted by Paul S
much better! who's next then?
You!

I like regex questions (hint! )

There's another whole can of worms that's been opened:
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.

15. I have a relatively simple regex question but I have to answer a question first.

16. Don't forget we haven't had a question from sojomy yet

Sean

17. Originally Posted by samsm

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. 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.

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.

19. 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.

20. 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. 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

22. 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. 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?

24. Hopefully soon, otherwise we can go with Aaron's regex question

Sean

25. 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.

#### Posting Permissions

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