473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match_all ? quantifier problem

Han
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

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))

but fails WITH a ? quantifier

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))?

I need the rest of the pattern results, not matter if this subpattern exists
or not. If it doesn't exist, I need the respective array value to be empty.

What's wrong with my syntax?

Thanks.
Jul 17 '05 #1
2 2389
Also sprach Han:
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

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))

but fails WITH a ? quantifier

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))?
It does not fail, but it gives unexpected results. This regex translates
into "match the above pattern OR match an empty string".
I need the rest of the pattern results, not matter if this subpattern
exists or not.
preg_match_all will give you all the matches it finds. Just have a look at
the array of found matches (print_r()).
If it doesn't exist, I need the respective array value
to be empty.


$test = '$123,123.12</b> $1,1.1</b> $23,23.23</b>';
$pattern = '/(?:\\$|\s*)(?:\ d{1,3}\,)?\d{1, 3}\.\d{2}(?:</b>)/';
preg_match_all( $pattern, $test, $result);
echo '<pre>'; print_r($result ); echo '</pre>';

Note that I stripped the additional parentheses around the whole regex.
Now, $result should be:

$result[0] = '$123,123.12</b>'
$result[1] = '$23,23.23</b>'
(as '$1,1.1</b>' does not match)

Do I understand you right, that, in the above case, you would like $result
to be

$result[0] = '$123,123.12</b>'
$result[1] = ''
$result[2] = '$23,23.23</b>'

Well, that would be a bit more difficult (Remember, that anything could be
there instead of the "almost correct" $1,1.1</b>). You'd have to extend your
regex to something like "Match the valid pattern as before and capture them
with parentheses nr. 1 (that's your original regex) OR match any invalid
pattern, but don't capture them." Then use preg_match_all with the flag
PREG_SET_ORDER and in the result array, check for every index if
second-level index 1 exists. If so, it should contain your valid subpattern,
if not, then you have an invalid pattern at second-level index 0.

Greetings, Thomas
Jul 17 '05 #2
Han
Thom,

Thanks for your reply.

It seems like trying to include this and other conditional requirements in
one pass might be asking for too much. It will ultimately be much easier to
simply capture larger blocks of markup and parse them accordingly upon
outputting to the page.

Your explanation was very instructive though and I sincerely appreciate your
time.

"Thomas Mlynarczyk" <bl************ *@hotmail.com> wrote in message
news:bm******** *****@news.t-online.com...
Also sprach Han:
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

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))

but fails WITH a ? quantifier

((?:\\$|\s*)(?: \d{1,3}\,)?\d{1 ,3}\.\d{2}(?:</b>))?
It does not fail, but it gives unexpected results. This regex translates
into "match the above pattern OR match an empty string".
I need the rest of the pattern results, not matter if this subpattern
exists or not.


preg_match_all will give you all the matches it finds. Just have a look at
the array of found matches (print_r()).
If it doesn't exist, I need the respective array value
to be empty.


$test = '$123,123.12</b> $1,1.1</b> $23,23.23</b>';
$pattern = '/(?:\\$|\s*)(?:\ d{1,3}\,)?\d{1, 3}\.\d{2}(?:</b>)/';
preg_match_all( $pattern, $test, $result);
echo '<pre>'; print_r($result ); echo '</pre>';

Note that I stripped the additional parentheses around the whole regex.
Now, $result should be:

$result[0] = '$123,123.12</b>'
$result[1] = '$23,23.23</b>'
(as '$1,1.1</b>' does not match)

Do I understand you right, that, in the above case, you would like $result
to be

$result[0] = '$123,123.12</b>'
$result[1] = ''
$result[2] = '$23,23.23</b>'

Well, that would be a bit more difficult (Remember, that anything could be
there instead of the "almost correct" $1,1.1</b>). You'd have to extend

your regex to something like "Match the valid pattern as before and capture them with parentheses nr. 1 (that's your original regex) OR match any invalid
pattern, but don't capture them." Then use preg_match_all with the flag
PREG_SET_ORDER and in the result array, check for every index if
second-level index 1 exists. If so, it should contain your valid subpattern, if not, then you have an invalid pattern at second-level index 0.

Greetings, Thomas

Jul 17 '05 #3

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

Similar topics

2
4698
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
7297
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>
5
6098
by: Han | last post by:
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>
0
1482
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 there an easier way to resolve this matter? for ($i = 0; $i < $m; $i++, $start+=20){
4
1987
by: Fabian | last post by:
Hi all there, I have already tried asking for help a couple of days ago. I try to rephrase better my problem: I need to grab a webpage that looks like this: <td width=80 align=center valign=top><a href="<link that should not be grabbed by the pattern>" id=r><img src=image.jpg width=66 height=79 alt="" border=1><br><font size=-2>Bla Bla text</font></a></td><td
10
2045
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 simple patterns but when i use preg_match_all, i either get unintended results or incomplete matches.
1
1950
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
2443
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
8269
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 into $page variable): preg_match ('/<div\s+?id="srNum_\d+?"\s+?class="number">(.*?)<\/div>/', $page,...
0
9706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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,...
1
10321
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,...
0
6853
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
5522
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
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
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.