473,406 Members | 2,281 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,406 software developers and data experts.

pattern match and extract problem

Hi All,
I expect someone can crack this one in no time.

Ok, what I'd like to do is to match a pattern in a string and extract a
portion of it. For example if I had a string like:

'The store had apples(20) and pears(30)'

how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)', how
do I expect the '??'.
Ta in advance,
Dave
Feb 7 '06 #1
3 1762
easy enough. you can use a string tokeniser and use the ( as the
delimeter. it depends on how random the string will be to how you will
harvest the number.

for example:

$string = strtok($longstring,'(');

will give you:

20) and pears

then from then you can go...

$num1 = strtok($string,')');

$num1 would be 20.

its basic but would work.now its up to you how you work it you can run
more than one tokeniser at once so tokenise at the ' ' to get each word
or look into a function called explode()

i suppose to say that if the strings will be pretty regular then you
can use this and if not you would have to make it a little more dynamic
hope this helps

Sean Barton

Feb 7 '06 #2
On Tue, 07 Feb 2006 16:05:32 -0500, David Moore
<da**********@post2me.freeserve.co.uk> wrote:
Hi All,
I expect someone can crack this one in no time.

Ok, what I'd like to do is to match a pattern in a string and extract a
portion of it. For example if I had a string like:


I prefer using the preg functions to extract parts of strings, but that
requires at least a basic understanding of regular expressions. At any
rate, if you want to capture all instances of word(number) you could do
this:
preg_match_all('/[a-z]+\(([0-9]+)\)/im',$string,$matches);

$matches[0] would be an array of apples(20), pears(30) etc.,
$matches[1] would be an array of all the numbers (20, 30, etc.)

regex match result -> http://tinyurl.com/9yrmc

A.

--
http://RReplace.com
A Web based regular expressions powered find/replace utility
Feb 7 '06 #3

"David Moore" <da**********@post2me.freeserve.co.uk> wrote in message
news:ds**********@news7.svr.pol.co.uk...
Hi All,
I expect someone can crack this one in no time.

Ok, what I'd like to do is to match a pattern in a string and extract a
portion of it. For example if I had a string like:

'The store had apples(20) and pears(30)'

how do I extract the '20' and '30'? ie. if the pattern is 'apples(??)',
how do I expect the '??'.
sounds like you only want the numbers. easy.
preg_match_all("/\((\d\d)\)/", $string, $matches);
or if you want to handle any number of digits,
preg_match_all("/\((\d+)\)/", $string, $matches);


Ta in advance,
Dave

Feb 17 '06 #4

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

Similar topics

5
by: Kelmen Wong | last post by:
Greeting, I want to extract all "" from a string, what pattern should I used? eg. = - return array or test1, or test2
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
2
by: Craig Buchanan | last post by:
I have a HTML fragment that looks like this: <tr> <td valign="top" nowrap><span class="textBold">Property ID: </span></td> <td valign="top" nowrap colspan="4"...
6
by: hari.siri74 | last post by:
Extract the application name with version from an RPM string like hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0 from above string. Sometimes the RPM string may be...
19
by: konrad Krupa | last post by:
I'm not expert in Pattern Matching and it would take me a while to come up with the syntax for what I'm trying to do. I hope there are some experts that can help me. I'm trying to match...
3
by: konrad Krupa | last post by:
This message is a continuation of my previous post "Pattern Match" Doug - Thank you for your help. Doug Semler was able to solve my problem to some point but I still need some help. Doug's...
6
by: pramodkh | last post by:
Hi All Today only i joined this group. need ur help. Here goes my question: Is there any simple way to match for a particular pattern ( with start and end delimitters) and extract the matched...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.