On 1/16/07, Peter V. email@example.com wrote:
That did it for me. I know not to mix integers and floats; that was a
typing error. I know very little about regular expressions. Could you
put in words what you did?
groups = input.scan(/[.*?]/)
The forward slashes just delimit the regular expression
[ and ] are characters with special meaning in regexps; they create
‘character classes’. [a-z] means, for example, every lowercase letter
The backslashes escape them, because we mean literal brackets
.* means ‘any character at all, zero or more times’
The ? after it means, ‘match that as FEW times as possible’.
Without that, we would get everything up to the last ], when what we
want is everything up to the NEXT ].
Together that expression means ‘an opening bracket, anything
(or nothing), and then a closing bracket’
Next we take the array of strings we just built, and hand scan it again.
This time, the pattern is: \d+.?\d*
\d means “a digit”, and is shorthand for [0-9], which would be the
class of all digits, 0 to 9.
- after an expression means ‘1 or more times’. (Note the difference
between this and , which means 0 or more.)
‘.’ is a reserved character meaning ‘any single character’, so we
again escape it here with a backslash.
.? means ‘a period, zero or one times’
Finally, we have: \d, which means ‘zero or more consecutive digits’
Combined, \d+.?\d* means ‘a series of digits, optionally followed by
a decimal point and more digits’. Arguably that pattern needs to be
enhanced, because at the moment it would match “25.”
You only get so much from a free email, though. Heh
To sum up; first scan for “[ stuff ]” and then scan the contents of
those for things that look like numbers.