PDA

View Full Version : Extracting a Number



Niaad
May 31st, 2002, 15:33
Ok, here's the latest ordeal I've gotten myself into...

The numbers I'm reading from a .html file are in this format:

##(##)

That is... 15(4).

Basically, what I need, is just the first two numbers in that example [the numbers before the (]. Ok, so that'd be easy with substr, but the problem is, the numbers aren't always the same length.

Sometimes it's 4(0). Sometimes it's 104(12).

Is there any other way to do this, so it works no matter what length the two numbers are?

Dusty
May 31st, 2002, 16:02
Use a regular expression. Something like this would work:

($number)=($input=~/^(\d+)\(/);



<edit>

That's Perl, by the way. If you wanted it in something else, say what. Here it is in PHP, it's a bit more complicated:

preg_match("/^(\d+)/",$input,$out);$number=$out[0];

YUPAPA
May 31st, 2002, 17:05
($number,$stuff) = split(/\(/,$input);
print $number;

Niaad
May 31st, 2002, 17:58
Oops, I should've mentioned I'm working with PHP :).

But thanks for the responses, I'll go try 'em out.