Validating data

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

    Validating data

    I have data that is in this format: 8-8-19-29-1-4-41
    I wanted to write a simple function or regular expression that
    verified that data is in that format. The numbers should only be 1 or
    2 digits. There should be 6 dashes (-) with 7 numbers around the
    dashes. Here are examples of bad data:


    8-19-29-1-4-41 (Only 6 dashes)
    5-1-2-3-4-S-67-4 (Includes a letter)
    5-1-2-3-4-222-67-4 (has three digits in one space)

  • shimmyshack

    #2
    Re: Validating data

    On Apr 27, 11:22 pm, Anthony Smith <mrsmi...@hotma il.comwrote:
    I have data that is in this format: 8-8-19-29-1-4-41
    I wanted to write a simple function or regular expression that
    verified that data is in that format. The numbers should only be 1 or
    2 digits. There should be 6 dashes (-) with 7 numbers around the
    dashes. Here are examples of bad data:
    >
    8-19-29-1-4-41 (Only 6 dashes)
    5-1-2-3-4-S-67-4 (Includes a letter)
    5-1-2-3-4-222-67-4 (has three digits in one space)

    heres one using ereg
    $var = '1-2-3-44-55-66-77';
    if ( ereg( "^[0-9]{1,2}(-[0-9]{1,2}){6}$", $var ) )
    {
    //ok cos it starts with 1 or 2 characters from range 0-9,
    //then has exactly 6 groups of - followed by 1 or 2 digits
    }

    Comment

    • Rik

      #3
      Re: Validating data

      Anthony Smith wrote:
      I have data that is in this format: 8-8-19-29-1-4-41
      I wanted to write a simple function or regular expression that
      verified that data is in that format. The numbers should only be 1 or
      2 digits. There should be 6 dashes (-) with 7 numbers around the
      dashes. Here are examples of bad data:
      >
      >
      8-19-29-1-4-41 (Only 6 dashes)
      5-1-2-3-4-S-67-4 (Includes a letter)
      5-1-2-3-4-222-67-4 (has three digits in one space)
      Regex is one way to go, depending on the source, fscanf() can be your
      friend too.

      --
      Rik Wasmus

      Estimated date being able to walk again: 01-05-2007.
      Less then a week, hurray!

      Comment

      • Schraalhans Keukenmeester

        #4
        Re: Validating data

        On Sat, 28 Apr 2007 17:52:33 +0200, Rik wrote:
        Anthony Smith wrote:
        >I have data that is in this format: 8-8-19-29-1-4-41
        >I wanted to write a simple function or regular expression that
        >verified that data is in that format. The numbers should only be 1 or
        >2 digits. There should be 6 dashes (-) with 7 numbers around the
        >dashes. Here are examples of bad data:
        >>
        >>
        >8-19-29-1-4-41 (Only 6 dashes)
        >5-1-2-3-4-S-67-4 (Includes a letter)
        >5-1-2-3-4-222-67-4 (has three digits in one space)
        >
        Regex is one way to go, depending on the source, fscanf() can be your
        friend too.
        The latter I rarely see in PHP code, but indeed Rik, format strings are a
        great partner sometimes. And of course <winks at Steveespecially useful
        for people who don't "trust" regex ;-) In this case I think fscanf() is
        highly recommendable considering the fixed pattern.

        Regex is a great tool in the right hands, in many cases a great trick for
        obfuscating what you're doing to donwstream programmers using your code,
        and often abused where far easier and less resource intensive string
        functions could/should be used. Regex IS relatively costly in cpu cycles,
        no doubt about it, but it can, when used correctly, sometimes replace a
        pageful of ugly string manipulation code with one single call.

        Nonetheless a lot of people avoid learning regex like the plague, because
        it looks so complicated. That's a pity. There are plenty books & online
        sources for learning regex and a plethora of little regex 'trainer' apps,
        some freeware, some at a reasonable fee.

        The only thing I still haven't been able to conjure up is a nifty regex
        expression that takes the typical erratic female thoughtprocess as input
        and churns out the rationale behind it ;-)

        Sh.

        Comment

        • Vince Morgan

          #5
          Re: Validating data


          "Schraalhan s Keukenmeester" <invalid@invali d.spamwrote in message
          news:pan.2007.0 4.29.07.58.45.8 45718@invalid.s pam...
          and churns out the rationale behind it ;-)
          That may be more to do with the assumption of "rationale" rather than an
          actual inadequacey in the expresion engine @@.
          >
          Sh.

          Comment

          • Toby A Inkster

            #6
            Re: Validating data

            Schraalhans Keukenmeester wrote:
            Regex IS relatively costly in cpu cycles, no doubt about it, but it can,
            when used correctly, sometimes replace a pageful of ugly string
            manipulation code with one single call.
            Regular expressions are one of those examples of sacrificing CPU time in
            order to make the code easier to write, easier to read and easier to
            maintain.

            This is nearly always a beneficial trade.

            Other examples:

            - Comments in source code;
            - Use of constants;
            - SQL databases; and
            - Logically dividing library functions into multiple files.

            --
            Toby A Inkster BSc (Hons) ARCS
            Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

            Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

            * = I'm getting there!

            Comment

            Working...