regexp problem

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

    regexp problem

    Hi

    I have an array with following values:
    419;
    20;
    19;20;21;
    18;30;
    17;20;25;
    16;26;
    16;17;20;21;22; 34

    I would like to find out in how many keys there is "19;". If I simply search for
    "19;" it will also match "419;" witch is of course wrong. I tried to write a
    regexp but can't seem to pull it off. :-( Could somebody please assist me with
    that regexp

    thanks
    gordan


  • d

    #2
    Re: regexp problem

    "Gordan" <gordanMAKNI@to rkul.hr> wrote in message
    news:dq57af$j8t $1@news1.xnet.h r...[color=blue]
    > Hi
    >
    > I have an array with following values:
    > 419;
    > 20;
    > 19;20;21;
    > 18;30;
    > 17;20;25;
    > 16;26;
    > 16;17;20;21;22; 34
    >
    > I would like to find out in how many keys there is "19;". If I simply
    > search for "19;" it will also match "419;" witch is of course wrong. I
    > tried to write a regexp but can't seem to pull it off. :-( Could somebody
    > please assist me with that regexp
    >
    > thanks
    > gordan[/color]

    Why are you using a regular expression? Also, how are you iterating through
    your array?

    dave


    Comment

    • Andrew @ Rockface

      #3
      Re: regexp problem

      Gordan wrote:[color=blue]
      > Hi
      >
      > I have an array with following values:
      > 419;
      > 20;
      > 19;20;21;
      > 18;30;
      > 17;20;25;
      > 16;26;
      > 16;17;20;21;22; 34
      >
      > I would like to find out in how many keys there is "19;". If I simply search for
      > "19;" it will also match "419;" witch is of course wrong. I tried to write a
      > regexp but can't seem to pull it off. :-( Could somebody please assist me with
      > that regexp[/color]

      Someone will probably come up with a better regex but try:

      <?php

      $foo = array(
      "20;",
      "419;",
      "19;20;21;" ,
      "20;19;21;" ,
      "21;20;19;" ,
      "19;",
      "194;",
      "18;30;",
      "17;20;25;" ,
      "16;26;",
      "16;17;20;21;22 ;34"
      );

      foreach ($foo as $bar) {
      if (preg_match("/^19;|;19;/", $bar)) {
      echo $bar."<br>\n";
      }
      }

      ?>

      This matches:
      19;20;21;
      20;19;21;
      21;20;19;
      19;

      --
      Andrew @ Rockface
      np: Naked Raygun - Metastasis [stopped]

      Comment

      • d

        #4
        Re: regexp problem

        "Andrew @ Rockface" <andrew@rockfac e-records.co.uk> wrote in message
        news:dq5bb5$ibn $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
        > Gordan wrote:[color=green]
        >> Hi
        >>
        >> I have an array with following values:
        >> 419;
        >> 20;
        >> 19;20;21;
        >> 18;30;
        >> 17;20;25;
        >> 16;26;
        >> 16;17;20;21;22; 34
        >>
        >> I would like to find out in how many keys there is "19;". If I simply
        >> search for "19;" it will also match "419;" witch is of course wrong. I
        >> tried to write a regexp but can't seem to pull it off. :-( Could somebody
        >> please assist me with that regexp[/color]
        >
        > Someone will probably come up with a better regex but try:
        >
        > <?php
        >
        > $foo = array(
        > "20;",
        > "419;",
        > "19;20;21;" ,
        > "20;19;21;" ,
        > "21;20;19;" ,
        > "19;",
        > "194;",
        > "18;30;",
        > "17;20;25;" ,
        > "16;26;",
        > "16;17;20;21;22 ;34"
        > );
        >
        > foreach ($foo as $bar) {
        > if (preg_match("/^19;|;19;/", $bar)) {
        > echo $bar."<br>\n";
        > }
        > }[/color]

        /(?:^|;)19;/ will do it, too.
        [color=blue]
        > ?>
        >
        > This matches:
        > 19;20;21;
        > 20;19;21;
        > 21;20;19;
        > 19;
        >
        > --
        > Andrew @ Rockface
        > np: Naked Raygun - Metastasis [stopped]
        > www.rockface-records.co.uk[/color]


        Comment

        • Gordan

          #5
          Re: regexp problem

          THANK YOU both it works perfectly :-)

          gordan


          Comment

          • Rich

            #6
            Re: regexp problem


            "Gordan" <gordanMAKNI@to rkul.hr> wrote in message
            news:dq57af$j8t $1@news1.xnet.h r...[color=blue]
            > Hi
            >
            > I have an array with following values:
            > 419;
            > 20;
            > 19;20;21;
            > 18;30;
            > 17;20;25;
            > 16;26;
            > 16;17;20;21;22; 34
            >
            > I would like to find out in how many keys there is "19;". If I simply
            > search for "19;" it will also match "419;" witch is of course wrong. I
            > tried to write a regexp but can't seem to pull it off. :-( Could somebody
            > please assist me with that regexp
            >
            > thanks
            > gordan
            >[/color]

            To count the number of times 19 occurs, something like this would work.

            <?
            $myarray = array(
            "419;",
            "20;",
            "19;20;21;" ,
            "18;30;",
            "17;20;25;" ,
            "16;26;",
            "16;17;20;21;22 ;34"
            );

            $num = 19;

            echo "There are/is ".count(preg_gr ep("/(?:^|;)$num;/", $myarray))."
            occurrence(s) of $num.";
            ?>


            Rich


            Comment

            Working...