473,563 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match_all optional subpattern

Han
Using preg_match_all, I need to capture a list of first and last names plus
an optional country code proceeding them.

For example:

<tr><td>AU</td><td>Jane Smith</td></tr>
<tr><td></td><td>Bill Johnson</td></tr>
<tr><td>GB</td><td>Larry Brown</td></tr>
<tr><td>US</td><td>Mary Jordon</td></tr>
<tr><td></td><td>Peter Jones</td></tr>

The country code might exist, it might not.

I would like the array contents to look like this:

AU Jane Smith
Bill Johnson
US Larry Brown
GB Mary Jordon
Peter Jones

I know a subpattern is needed of all the possible country codes:

AU|GB|US

but how do you include this as an optional subpattern?

Thanks in advance.


Jul 17 '05 #1
5 6086
Han wrote:
Using preg_match_all, I need to capture a list of first and last names plus
an optional country code proceeding them.

For example:

<tr><td>AU</td><td>Jane Smith</td></tr>
<tr><td></td><td>Bill Johnson</td></tr>
<tr><td>GB</td><td>Larry Brown</td></tr>
<tr><td>US</td><td>Mary Jordon</td></tr>
<tr><td></td><td>Peter Jones</td></tr>

[...] I know a subpattern is needed of all the possible country codes:

AU|GB|US

but how do you include this as an optional subpattern?


The ? quantifier means zero or one of whatever came before,
representable by {0,1}. Quantifying a subpattern using the
question mark denotes its nonobligatory nature.

So, to match optional two-letter country codes within a table cell
(doesn't properly cater for attributes, but that's rectifiable):

`<td.*>([a-z]{2})?</td.*>`Usi

If you wish to list the possible values, precluding others:

`<td.*>(au|gb|u s)?</td.*>`Usi

--
Jock
Jul 17 '05 #2
"Han" <no****@nowhere .com> wrote in message news:<TT******* *************@r wcrnsc51.ops.as p.att.net>...
Using preg_match_all, I need to capture a list of first and last names plus
an optional country code proceeding them.

For example:

<tr><td>AU</td><td>Jane Smith</td></tr>
<tr><td></td><td>Bill Johnson</td></tr>
<tr><td>GB</td><td>Larry Brown</td></tr>
<tr><td>US</td><td>Mary Jordon</td></tr>
<tr><td></td><td>Peter Jones</td></tr>

The country code might exist, it might not.

I would like the array contents to look like this:

AU Jane Smith
Bill Johnson
US Larry Brown
GB Mary Jordon
Peter Jones

I know a subpattern is needed of all the possible country codes:

AU|GB|US
this pattern should do the job:

"{<tr><td>\ s*([A-Z]{2})?\s*</td><td>\s*(\w+) ?\s*(\w+)\s*</td></tr>}im"

if this pattern is used in preg_match_all, it should produce the
desired result.
it will extract the country code if availible, first name if
availible, and last name.
They will be put in an 2 dim array. If no country code or first name
is given, the array element will be left empty.

hope this helps,

sascha

but how do you include this as an optional subpattern?

Thanks in advance.

Jul 17 '05 #3
Han
John,

Thank you for another detailed reply.

The cryptic syntax is beginning to slowly sink in, but there's still a few
nagging issues.

In my price list, the amount may or may not be preceded with a $ sign.

For instance, the list might look like this:

$2.99
1.99
$3.00
$4.00

I modified my price pattern to accommodate this:

((\\$|\s*)?\d{1 ,3}\.\d{2})

which works great. The problem is, it also creates another array dimension
that contains only $ or space:

$

$
$

I can simply ignore this dimension, but is there a way to prevent it?

Thanks (again) in advance.

"John Dunlop" <jo*********@jo hndunlop.info> wrote in message
news:MP******** *************** *@news.freeserv e.net...
Han wrote:
Using preg_match_all, I need to capture a list of first and last names plus an optional country code proceeding them.

For example:

<tr><td>AU</td><td>Jane Smith</td></tr>
<tr><td></td><td>Bill Johnson</td></tr>
<tr><td>GB</td><td>Larry Brown</td></tr>
<tr><td>US</td><td>Mary Jordon</td></tr>
<tr><td></td><td>Peter Jones</td></tr>

[...] I know a subpattern is needed of all the possible country codes:

AU|GB|US

but how do you include this as an optional subpattern?


The ? quantifier means zero or one of whatever came before,
representable by {0,1}. Quantifying a subpattern using the
question mark denotes its nonobligatory nature.

So, to match optional two-letter country codes within a table cell
(doesn't properly cater for attributes, but that's rectifiable):

`<td.*>([a-z]{2})?</td.*>`Usi

If you wish to list the possible values, precluding others:

`<td.*>(au|gb|u s)?</td.*>`Usi

--
Jock


Jul 17 '05 #4
Han wrote:
((\\$|\s*)?\d{1 ,3}\.\d{2})

which works great. The problem is, it also creates another array
dimension that contains only $ or space:

[...] I can simply ignore this dimension, but is there a way to
prevent it?


Subpatterns that begin with the two character sequence "?:" aren't
captured. You could then write your pattern as:

`(?:\\$|\s*)?\d {1,3}\.\d{2}`

--
Jock
Jul 17 '05 #5
Han
Jock,

That's it--thanks.

I've been spending some time re-reading the pattern documentation on php.net
and it's beginning to sink in.

Again, much appreciated!

"John Dunlop" <jo*********@jo hndunlop.info> wrote in message
news:MP******** *************** *@news.freeserv e.net...
Han wrote:
((\\$|\s*)?\d{1 ,3}\.\d{2})

which works great. The problem is, it also creates another array
dimension that contains only $ or space:

[...] I can simply ignore this dimension, but is there a way to
prevent it?


Subpatterns that begin with the two character sequence "?:" aren't
captured. You could then write your pattern as:

`(?:\\$|\s*)?\d {1,3}\.\d{2}`

--
Jock

Jul 17 '05 #6

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

Similar topics

4
3795
by: Han | last post by:
Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences of the following pattern: score=9999999999&amp; The number shown can be 5-10 characters in length. I would like to extract only the number, stripping off the "score=" and "&amp;".
2
4687
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
3
7288
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a href="http://www.example.com?product=3456789&amp;company=3528"> Mickey Mouse</a></font><br><img width="200" height="200" border="0" src="http://www.example.com/images/3456789.jpg"><b>$8.00</b>
2
2377
by: Han | last post by:
The following pattern (which is one subpattern in a string of several) looks for the following $xxx,xxx.xx (with the dollar sign) or xxx,xxx.xx (space in replace of missing dollar sign) It works great WITHOUT a ? quantifier
0
1473
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at once from a much larger string. This surprised me because I expected the preg_match_all to perform the task much faster. Why is this, and is...
10
2027
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display purposes. i have been trying to write the correct pattern for 2 weeks now without any success...i can get preg_replace() to work, with several...
1
1939
by: ngmr80 | last post by:
Hi, I'm experiencing a problem when trying to capture substrings with preg_match_all() from strings like "set('Hello','World')" using the following Regular Expression (PERL syntax): "/^set\((?:'(+)'(?(?!\)),))+\)$/"
6
2437
by: PaulB | last post by:
Hello, as a newbie I'm requesting some help in understanding the regular expression below preg_match_all("|<tr(.*)</tr>|U",$table,$rows); Would anybody please just run through ("|<tr(.*)</tr>|U" from left to right and tell me what it means? Thanks Paul Bird
2
8235
loriann
by: loriann | last post by:
hi, I have a problem with preg_match function returning empty arrays for my wonderful regexes. However, I can't see what I am doing wrong - maybe one of you could help? I'm loading the source of a website into variable and then using preg_match_all to extract all occurrences of a string. it looks like this (where source code is loaded...
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7885
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. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
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...
0
7948
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.