preg_match_all ? quantifier problem

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. syntax Error: invalid quantifier
      syntax Error: invalid quantifier
    2. 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&amp;" ...
    3. 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: ...
    4. [PHP] preg_match_all
      * Thus wrote Floris (florisvankempen@home.nl): Use the word boundry character: $regex1 = ">*\b("; $regex2 = ")\b*<"; Curt --
    5. 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...
  3. #2

    Default Re: preg_match_all ? quantifier problem

    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


    Thomas Mlynarczyk Guest

  4. #3

    Default 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...
    > 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
    >
    >

    Han Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139