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

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 2368
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
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...
3
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...
5
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>...
0
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...
4
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...
10
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...
1
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): ...
6
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...
2
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.