Connecting Tech Pros Worldwide Help | Site Map

One liner for extracting a number

D. Alvarado
Guest
 
Posts: n/a
#1: Jul 17 '05
Hello,
Can regular expressions help me here? I want to extract a number
from a string which will always be of the form "parameter###" where
"###" is an arbitrary number of numeric digits. So if my string
contained

parameter24
parameter8
parameter90210

I would want to extract "24", "8", and "90210" respectively.

Thanks for any help, -
kingofkolt
Guest
 
Posts: n/a
#2: Jul 17 '05

re: One liner for extracting a number


"D. Alvarado" <laredotornado@zipmail.com> wrote in message
news:9fe1f2ad.0408141900.21ebda81@posting.google.c om...[color=blue]
> Hello,
> Can regular expressions help me here? I want to extract a number
> from a string which will always be of the form "parameter###" where
> "###" is an arbitrary number of numeric digits. So if my string
> contained
>
> parameter24
> parameter8
> parameter90210
>
> I would want to extract "24", "8", and "90210" respectively.
>
> Thanks for any help, -[/color]

If the string will always contain only "parameter###" and nothing before or
after "parameter###", then this should work:

$str = "parameter24";
preg_match( "/parameter(\d+)/", $str, $matches );
print_r( $matches );

/* OUTPUT:

Array
(
[0] => parameter24
[1] => 24
)

*/


Chris Hope
Guest
 
Posts: n/a
#3: Jul 17 '05

re: One liner for extracting a number


D. Alvarado wrote:
[color=blue]
> Hello,
> Can regular expressions help me here? I want to extract a number
> from a string which will always be of the form "parameter###" where
> "###" is an arbitrary number of numeric digits. So if my string
> contained
>
> parameter24
> parameter8
> parameter90210
>
> I would want to extract "24", "8", and "90210" respectively.[/color]

In this example $string is your string like "parameter90210". $matches is an
array containing the matches where $matches[1] is the stuff matched
inbetween ()

ereg("parameter([0-9]*)", $string, $matches);

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Ian.H
Guest
 
Posts: n/a
#4: Jul 17 '05

re: One liner for extracting a number


On Sat, 14 Aug 2004 20:00:56 -0700, D. Alvarado wrote:
[color=blue]
> Hello,
> Can regular expressions help me here? I want to extract a number
> from a string which will always be of the form "parameter###" where
> "###" is an arbitrary number of numeric digits. So if my string
> contained
>
> parameter24
> parameter8
> parameter90210
>
> I would want to extract "24", "8", and "90210" respectively.
>
> Thanks for any help, -[/color]


preg_match('/([0-9]+)$/', $your_string, $matches);
$digits = $matches[1];



Regards,

Ian

Note: preg_* is faster and much more flexible than ereg* functions =)

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Chung Leong
Guest
 
Posts: n/a
#5: Jul 17 '05

re: One liner for extracting a number


"D. Alvarado" <laredotornado@zipmail.com> wrote in message
news:9fe1f2ad.0408141900.21ebda81@posting.google.c om...[color=blue]
> Hello,
> Can regular expressions help me here? I want to extract a number
> from a string which will always be of the form "parameter###" where
> "###" is an arbitrary number of numeric digits. So if my string
> contained
>
> parameter24
> parameter8
> parameter90210
>
> I would want to extract "24", "8", and "90210" respectively.
>
> Thanks for any help, -[/color]

Well, if it will always be "parameter###", then you can simply trim off the
first 9 characters of the text.

$num = (int) substr($s, 9);


Christopher Finke
Guest
 
Posts: n/a
#6: Jul 17 '05

re: One liner for extracting a number


"D. Alvarado" <laredotornado@zipmail.com> wrote in message
news:9fe1f2ad.0408141900.21ebda81@posting.google.c om...[color=blue]
> Can regular expressions help me here? I want to extract a number
> from a string which will always be of the form "parameter###" where
> "###" is an arbitrary number of numeric digits. So if my string
> contained
>
> parameter24
> parameter8
> parameter90210
>
> I would want to extract "24", "8", and "90210" respectively.[/color]

I can't believe no one has mentioned this:

$string = "parameter1234";
$number = str_replace("parameter","",$string);

Chris Finke


Gary L. Burnore
Guest
 
Posts: n/a
#7: Jul 17 '05

re: One liner for extracting a number


On Sun, 15 Aug 2004 01:17:43 -0500, "Christopher Finke"
<chris@efinke.com> wrote:
[color=blue]
>"D. Alvarado" <laredotornado@zipmail.com> wrote in message
>news:9fe1f2ad.0408141900.21ebda81@posting.google. com...[color=green]
>> Can regular expressions help me here? I want to extract a number
>> from a string which will always be of the form "parameter###" where
>> "###" is an arbitrary number of numeric digits. So if my string
>> contained
>>
>> parameter24
>> parameter8
>> parameter90210
>>
>> I would want to extract "24", "8", and "90210" respectively.[/color]
>
>I can't believe no one has mentioned this:
>
>$string = "parameter1234";
>$number = str_replace("parameter","",$string);[/color]

If I would have seen his post before yours, I would have. :)

Wonder why his isn't in the spool. Hmmmmm...... Better go look.


--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
D. Alvarado
Guest
 
Posts: n/a
#8: Jul 17 '05

re: One liner for extracting a number


These are all excellent solutions. You guys rock!

"Chung Leong" <chernyshevsky@hotmail.com> wrote in message news:<ZZKdndAboOQLYIPcRVn-qw@comcast.com>...[color=blue]
> "D. Alvarado" <laredotornado@zipmail.com> wrote in message
> news:9fe1f2ad.0408141900.21ebda81@posting.google.c om...[color=green]
> > Hello,
> > Can regular expressions help me here? I want to extract a number
> > from a string which will always be of the form "parameter###" where
> > "###" is an arbitrary number of numeric digits. So if my string
> > contained
> >
> > parameter24
> > parameter8
> > parameter90210
> >
> > I would want to extract "24", "8", and "90210" respectively.
> >
> > Thanks for any help, -[/color]
>
> Well, if it will always be "parameter###", then you can simply trim off the
> first 9 characters of the text.
>
> $num = (int) substr($s, 9);[/color]
Closed Thread