Ask a Question related to PHP Development, Design and Development.
-
Han #1
preg_match_all ? quantifier problem
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.
Han Guest
-
syntax Error: invalid quantifier
syntax Error: invalid quantifier -
preg_match VS preg_match_all
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&" ... -
finding pattern with preg_match_all
Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences of the following pattern: ... -
[PHP] preg_match_all
* Thus wrote Floris (florisvankempen@home.nl): Use the word boundry character: $regex1 = ">*\b("; $regex2 = ")\b*<"; Curt -- -
preg, patern quantifier limit
Hi, i did a standard preg {1,65536} What i am simply trying to do is to validate the a form that will insert into my TEXT database field. So i... -
Thomas Mlynarczyk #2
Re: preg_match_all ? quantifier problem
Also sprach Han:
It does not fail, but it gives unexpected results. This regex translates> 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>))?
into "match the above pattern OR match an empty string".
preg_match_all will give you all the matches it finds. Just have a look at> I need the rest of the pattern results, not matter if this subpattern
> exists or not.
the array of found matches (print_r()).
$test = '$123,123.12</b> $1,1.1</b> $23,23.23</b>';> If it doesn't exist, I need the respective array value
> to be empty.
$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
Thomas Mlynarczyk Guest
-
Han #3
Re: preg_match_all ? quantifier problem
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" <blue_elephant55@hotmail.com> wrote in message
news:bm0qd3$rs6$05$1@news.t-online.com...your> 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 extendthem> regex to something like "Match the valid pattern as before and capturesubpattern,> 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> if not, then you have an invalid pattern at second-level index 0.
>
> Greetings, Thomas
>
>
Han Guest



Reply With Quote

