473,671 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1781
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($longstr ing,'(');

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**********@p ost2me.freeserv e.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,$ma tches);

$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**********@p ost2me.freeserv e.co.uk> wrote in message
news:ds******** **@news7.svr.po l.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
3317
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
3141
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 from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
4
9746
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 numeric value according to an arbitrary regular expression.
4
3603
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 "LOT:". Source text may contain multiple "SUB: ... LOT:" blocks. For example this is my source text:
2
6057
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" bgcolor="#F0F0F0"><b>&nbsp;01-068-24-64-1024</b></td> </tr> I am trying to extract the '' part of it.
6
1774
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 hpsmh-1.1.1.2-RHEL3- Linux.RPM.
19
3169
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 /d/d/d/s/d/d in any text. There could be spaces in front or after the pattern (the nnn nn could be without spaces also) but it shouldn't pick it up in case like this 1234 56768
3
1626
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 pattern is going to make sure that I don't get match of ddd dd embeded in number. For example
6
4494
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 section? I am doing it reading the contents line by line and setting the flags. But this logic is becoming complex, as i need to match and extract based on certain conditions. for ex:
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8597
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4402
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.