Validating data

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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)

Re: Validating data

Quoted text here. Click to load it

heres one using ereg
$var = '1-2-3-44-55-66-77';
if ( ereg( "^[0-9](-[0-9])$", $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

Re: Validating data

Anthony Smith wrote:
Quoted text here. Click to load it

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!

Re: Validating data

On Sat, 28 Apr 2007 17:52:33 +0200, Rik wrote:

Quoted text here. Click to load it

The latter I rarely see in PHP code, but indeed Rik, format strings are a
great partner sometimes. And of course <winks at Steve> especially 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 ;-)


Re: Validating data

Quoted text here. Click to load it
That may be more to do with the assumption of "rationale" rather than an
actual inadequacey in the expresion engine @@.
Quoted text here. Click to load it

Re: Validating data

Schraalhans Keukenmeester wrote:

Quoted text here. Click to load it

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

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
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Site Timeline