|
Posted by Schraalhans Keukenmeester on 04/29/07 07:58
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 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 ;-)
Sh.
Navigation:
[Reply to this message]
|