regular expression help..

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

    regular expression help..

    I need some help with regulars...

    how can I check if $var contains only "----" (minus) no metter how many??

    Thanx...

    point


  • Matthew Vickers

    #2
    Re: regular expression help..

    On Thu, 28 Aug 2003 15:50:49 +0200
    "point" <point@caanNOSP AMproduction.co m> wrote:
    [color=blue]
    > I need some help with regulars...
    >
    > how can I check if $var contains only "----" (minus) no metter how
    > many??
    >
    > Thanx...
    >
    > point[/color]

    Don't cross post, it's considered bad form.

    /^-+$/

    Begining of the line (^), followed by minus (-) one or more times (+)
    followed by end of line ($)

    There are some very good online tutorial for regexs; try here



    Matt

    --
    Quispiam Power Computing | "There are two major products that come out
    Pendle Hill, Australia | of Berkeley: LSD and UNIX. We don't believe
    +61 2 9631 7719 | this to be a coincidence. "
    www.quispiam.com | - Jeremy S. Anderson

    Comment

    • Ian.H [dS]

      #3
      Re: regular expression help..

      On Thu, 28 Aug 2003 15:50:49 +0200 in
      <message-id:bil19u01kqn@ enews1.newsguy. com>
      "point" <point@caanNOSP AMproduction.co m> wrote:
      [color=blue]
      > I need some help with regulars...
      >
      > how can I check if $var contains only "----" (minus) no metter how
      > many??
      >
      > Thanx...
      >
      > point
      >
      >[/color]


      if (preg_match('/^([-]+)$/', $var)) {
      echo "All's good\n";
      } else {
      echo "Oops.. illegal chars!\n";
      }


      HTH =)



      Regards,

      Ian

      --
      Ian.H [Design & Development]
      digiServ Network - Web solutions
      www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      Programming, Web design, development & hosting.

      Comment

      • Andy Hassall

        #4
        Re: regular expression help..

        On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]" <ian@WINDOZEdig iserv.net> wrote:
        [color=blue]
        >preg_match('/^([-]+)$/', $var)[/color]

        Just a tip - don't capture unless you actually want to use the subexpressions,
        and there's no point having a character class of a single character.

        /^-+$/ is enough here.

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        • Ian.H [dS]

          #5
          Re: regular expression help..

          On Thu, 28 Aug 2003 16:27:42 +0100 in
          <message-id:2n7skvkk5rop m0gp5l25vsiqblr n8qebs3@4ax.com >
          Andy Hassall <andy@andyh.co. uk> wrote:
          [color=blue]
          > On Thu, 28 Aug 2003 14:48:25 GMT, "Ian.H [dS]"
          > <ian@WINDOZEdig iserv.net> wrote:
          >[color=green]
          > >preg_match('/^([-]+)$/', $var)[/color]
          >
          > Just a tip - don't capture unless you actually want to use the
          > subexpressions,
          > and there's no point having a character class of a single character.
          >
          > /^-+$/ is enough here.[/color]


          Thanks Andy.. after I replied and re-synced the groups.. I noticed the
          reply without the match / group chars.. I guess it's one of those things
          where "I used it before.. it works..." and I haven't really got around
          to try and improve it.

          Good tip and noted.. especially as I use lots of regex for various
          projects =)



          Regards,

          Ian

          --
          Ian.H [Design & Development]
          digiServ Network - Web solutions
          www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
          Programming, Web design, development & hosting.

          Comment

          • Shawn Wilson

            #6
            Re: regular expression help..

            point wrote:[color=blue]
            >
            > I need some help with regulars...
            >
            > how can I check if $var contains only "----" (minus) no metter how many??[/color]

            Just as a point of interest, I compared the following 3 regular expressions:
            /^-+$/, /[^-]/, /^([-]+)$/

            I was going to suggest !preg_match("/[^-]/",$val), then realized it wouldn't
            return false on an empty string, but I threw it in anyway. The code I used
            follows the comparions. In just about ever case /^-+/ won. No surprised there,
            just thought I find out how much different they are. I reordered the regex
            array 3 times to make sure the array position wasn't biasing the results.

            ------

            /^-+$/ AVG: 4.4089375120221 E-05 seconds (363 sampled)
            /[^-]/ AVG: 4.4289953219965 E-05 seconds (322 sampled)
            /^([-]+)$/ AVG: 4.4381050836472 E-05 seconds (315 sampled)


            -----------------------------------------3--

            /^-+$/ AVG: 4.7261700218106 E-05 seconds (324 sampled)
            /[^-]/ AVG: 5.3900497155554 E-05 seconds (353 sampled)
            /^([-]+)$/ AVG: 5.3713196202328 E-05 seconds (323 sampled)


            --233333333333333 33333-------

            /^-+$/ AVG: 4.3738299402697 E-05 seconds (290 sampled)
            /[^-]/ AVG: 4.3901622804821 E-05 seconds (351 sampled)
            /^([-]+)$/ AVG: 4.5026909342051 E-05 seconds (359 sampled)


            --3------------------------------------------

            /^-+$/ AVG: 4.3636065157915 E-05 seconds (349 sampled)
            /[^-]/ AVG: 4.4002504405861 E-05 seconds (334 sampled)
            /^([-]+)$/ AVG: 4.4881733434057 E-05 seconds (317 sampled)


            TESTINGTESTINGT ESTING

            /^-+$/ AVG: 4.2770132922486 E-05 seconds (298 sampled)
            /[^-]/ AVG: 4.4013239057577 E-05 seconds (367 sampled)
            /^([-]+)$/ AVG: 4.2876200889474 E-05 seconds (335 sampled)


            REORDER ARRAY
            ------

            /^([-]+)$/ AVG: 4.5152124530541 E-05 seconds (334 sampled)
            /^-+$/ AVG: 4.4505271685899 E-05 seconds (338 sampled)
            /[^-]/ AVG: 4.5173778766539 E-05 seconds (328 sampled)


            -----------------------------------------3--

            /^([-]+)$/ AVG: 5.4088808734965 E-05 seconds (322 sampled)
            /^-+$/ AVG: 4.7325738364301 E-05 seconds (341 sampled)
            /[^-]/ AVG: 5.387234051079E-05 seconds (337 sampled)


            --233333333333333 33333-------

            /^([-]+)$/ AVG: 4.4645499606867 E-05 seconds (331 sampled)
            /^-+$/ AVG: 4.3906505752659 E-05 seconds (318 sampled)
            /[^-]/ AVG: 4.4383894004713 E-05 seconds (351 sampled)


            --3------------------------------------------

            /^([-]+)$/ AVG: 4.4498060430799 E-05 seconds (336 sampled)
            /^-+$/ AVG: 4.3501577726225 E-05 seconds (328 sampled)
            /[^-]/ AVG: 4.3935364200955 E-05 seconds (336 sampled)


            TESTINGTESTINGT ESTING

            /^([-]+)$/ AVG: 4.281405290943E-05 seconds (326 sampled)
            /^-+$/ AVG: 4.2799590290457 E-05 seconds (345 sampled)
            /[^-]/ AVG: 4.3385659307694 E-05 seconds (329 sampled)



            REORDER ARRAY
            ------

            /[^-]/ AVG: 4.403849682176E-05 seconds (332 sampled)
            /^([-]+)$/ AVG: 4.4901682613106 E-05 seconds (329 sampled)
            /^-+$/ AVG: 4.3864447107006 E-05 seconds (339 sampled)


            -----------------------------------------3--

            /[^-]/ AVG: 5.405314904047E-05 seconds (339 sampled)
            /^([-]+)$/ AVG: 5.3905426187718 E-05 seconds (329 sampled)
            /^-+$/ AVG: 4.7486948679729 E-05 seconds (332 sampled)


            --233333333333333 33333-------

            /[^-]/ AVG: 4.3712762685922 E-05 seconds (325 sampled)
            /^([-]+)$/ AVG: 4.420560948989E-05 seconds (323 sampled)
            /^-+$/ AVG: 4.3457881970839 E-05 seconds (352 sampled)


            --3------------------------------------------

            /[^-]/ AVG: 4.452014041889E-05 seconds (329 sampled)
            /^([-]+)$/ AVG: 4.4954001013912 E-05 seconds (335 sampled)
            /^-+$/ AVG: 4.4005612532298 E-05 seconds (336 sampled)


            TESTINGTESTINGT ESTING

            /[^-]/ AVG: 4.2576365675663 E-05 seconds (326 sampled)
            /^([-]+)$/ AVG: 4.18363008818E-05 seconds (329 sampled)
            /^-+$/ AVG: 4.1880469391311 E-05 seconds (345 sampled)



            ---------------- START CODE

            <plaintext>
            <?php
            $str = array("------", "-----------------------------------------3--",
            "--233333333333333 33333-------",
            "--3------------------------------------------", "TESTINGTESTING TESTING");

            function getmicrotime(){
            list($usec, $sec) = explode(" ",microtime ());
            return ((float)$usec + (float)$sec);
            }

            foreach($str as $strsample) {
            $res = array(array("/[^-]/",0,0), array("/^([-]+)$/",0,0), array("/^-+$/",0,0));
            for($i=0;$i<100 0;++$i) {
            $rnd = rand(0,2);
            $time_start = getmicrotime();
            preg_match($res[$rnd][0], $strsample);
            $time_end = getmicrotime();
            $res[$rnd][1]++;
            $res[$rnd][2] += ($time_end - $time_start);
            }

            echo "\n\n ".$strsample."\ n\n";

            foreach($res as $val) {
            echo " ".$val[0];
            for($i=strlen($ val[0]);$i<10;++$i) {
            echo " ";
            }
            echo("\tAVG: ".($val[2] / $val[1])." seconds (".$val[1]." sampled)\n");
            }
            }

            ?>
            ---------------- END CODE

            Shawn
            --
            Shawn Wilson
            shawn@glassgian t.com

            Comment

            • Shawn Wilson

              #7
              Re: regular expression help..

              Shawn Wilson wrote:
              [color=blue]
              > Just as a point of interest, I compared the following 3 regular expressions:[/color]
              <snip>
              Wow. I reread my post and am embarrassed by the number of spelling and grammar
              mistakes. In case anyone is wondering, English is my first language.

              Shawn
              --
              Shawn Wilson
              shawn@glassgian t.com

              Comment

              • point

                #8
                Re: regular expression help..

                I forgott to say: Thanx guys :)))
                "point" <point@caanNOSP AMproduction.co m> wrote in message
                news:bil19u01kq n@enews1.newsgu y.com...[color=blue]
                > I need some help with regulars...
                >
                > how can I check if $var contains only "----" (minus) no metter how many??
                >
                > Thanx...
                >
                > point
                >
                >[/color]


                Comment

                Working...