Regural expression strange behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josip

    Regural expression strange behaviour

    I have the following code (I need to extract everything between
    slash):

    $buff="0,x,R / 1,y,R / - / 1,y,R";

    preg_match_all( "/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz );

    var_dump($izlaz );


    This outputs:

    array(3) {
    [0]=>
    array(4) {
    [0]=>
    string(7) "0,x,R /"
    [1]=>
    string(8) " 1,y,R /"
    [2]=>
    string(4) " - /"
    [3]=>
    string(8) " 1,y,R"
    }

    [1]=>
    array(4) {
    [0]=>
    string(5) "0,x,R"
    [1]=>
    string(5) "1,y,R"
    [2]=>
    string(0) ""
    [3]=>
    string(5) "1,y,R"
    }

    [2]=>
    array(4) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(1) "-"
    [3]=>
    string(0) ""
    }
    }



    Problem is in the 3rd row: why is it there? I would expect '-' sign in
    2nd row...


  • Josip

    #2
    Re: Regural expression strange behaviour

    On Tue, 18 Jan 2005 22:20:09 +0100, Josip <njuzovi@gmail. com> wrote:
    [color=blue]
    >I have the following code (I need to extract everything between
    >slash):
    >
    >$buff="0,x,R / 1,y,R / - / 1,y,R";
    >
    >preg_match_all ("/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz );
    >[/color]

    Hm, this is partial solution...

    preg_match_all( "/(\s*[0-9]\s*,\s*[a-z]\s*,\s*[LR]\s*\s*|\s*[\-]\s*)[\/]\s*/U",$buff,$izlaz );

    Comment

    • John Dunlop

      #3
      Re: Regural expression strange behaviour

      Josip wrote:
      [color=blue]
      > I have the following code (I need to extract everything between
      > slash):
      >
      > $buff="0,x,R / 1,y,R / - / 1,y,R";
      >
      > preg_match_all( "/\s*([0-9]\s*,\s*[a-z]\s*,\s*.\s*)\s*[\/]\s*|\s*(-)\s*[\/]/U",$buff,$izlaz );[/color]

      Is that really your pattern? I only ask because the output
      below doesn't come from that.
      [color=blue]
      > var_dump($izlaz );
      >
      >
      > This outputs:
      >
      > array(3) {
      > [0]=>
      > array(4) {[/color]

      No it doesn't. There are only three matches to the full
      pattern in your subject string.
      [color=blue]
      > [0]=>
      > string(7) "0,x,R /"
      > [1]=>
      > string(8) " 1,y,R /"
      > [2]=>
      > string(4) " - /"
      > [3]=>
      > string(8) " 1,y,R"[/color]

      The last string here doesn't match because there must be a
      slash at the end. You should allow the last slash to be
      optional in your pattern. Consider:

      `(\d\s*,\s*[a-z]\s*,\s*.|-)\s*/?`

      [ ... ]
      [color=blue]
      > Problem is in the 3rd row: why is it there? I would expect '-' sign in
      > 2nd row...[/color]

      Your subject string is '0,x,R / 1,y,R / - / 1,y,R'. The
      hyphen is in the third segment, not the second. Move the
      hyphen to the second segment and watch what happens.

      --
      Jock

      Comment

      Working...