473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

One liner for extracting a number

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, -
Jul 17 '05 #1
7 2884
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google.c om...
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, -


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
)

*/
Jul 17 '05 #2
D. Alvarado wrote:
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.


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/
Jul 17 '05 #3
On Sat, 14 Aug 2004 20:00:56 -0700, D. Alvarado wrote:
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, -

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/

Jul 17 '05 #4
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google.c om...
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, -


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);
Jul 17 '05 #5
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google.c om...
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.


I can't believe no one has mentioned this:

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

Chris Finke
Jul 17 '05 #6
On Sun, 15 Aug 2004 01:17:43 -0500, "Christopher Finke"
<ch***@efinke.com> wrote:
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google. com...
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.


I can't believe no one has mentioned this:

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


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
================================================== =========================
Jul 17 '05 #7
These are all excellent solutions. You guys rock!

"Chung Leong" <ch***********@hotmail.com> wrote in message news:<ZZ********************@comcast.com>...
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google.c om...
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, -


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);

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
1
by: D. Alvarado | last post by:
Hello, Does anyone have a PHP 4 one-liner (or two-liner) for extracing a file from a directory in which I know the word "footer" is guaranteed to be in the file name, I know the precise directory...
2
by: Shawn | last post by:
I have an XML file that contains nodes and I'm having no problem extracting the information using ASP. However I can't seem to get the ID number from the deliverrequest node from the example below....
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
8
by: Mantorok Redgormor | last post by:
The only adder I was able to come up with for incrementing by one, uses a for loop. I was trying to do this without using a for loop while emulating i++(it's for obfuscated code) Anyone know...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
6
by: Amma | last post by:
Hello Every one , Pls help me to extracting number from a text file since I am new to perl programming . I have a file and need to extract the number after semicolon in that ...
3
by: Clarisa | last post by:
Hello Folks I am working on extracting lines of data in a text file based on the string it contains. This is the text file called info.txt:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.