|
Posted by Steve on 10/18/07 03:40
<davranfor@gmail.com> wrote in message
news:1192656086.671916.314180@q5g2000prf.googlegroups.com...
> On 17 oct, 21:10, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> On Wed, 17 Oct 2007 22:50:59 +0200, <davran...@gmail.com> wrote:
>> > On 17 oct, 20:20, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
>> >> On Wed, 17 Oct 2007 21:59:49 +0200, <davran...@gmail.com> wrote:
>> >> > Hello
>>
>> >> > I need a regular expression that validate a list of numbers
>> >> > separated
>> >> > by "-" , numbers can not be greater than 999
>>
>> >> > Valid examples
>> >> > 0
>> >> > 12-455-01
>> >> > 1-9
>> >> > 125-32-155-45-45
>>
>> >> > Invalid examples
>> >> > -1
>> >> > 45-
>> >> > 1-45665456-4
>> >> > 12-45-
>> >> > -
>>
>> >> Hmmz, showing a 'best try' would be appreciated. However, I normally
>> >> can't
>> >> resist a good regex, so here it goes:
>>
>> >> '/^([1-9][0-9]?)?[0-9](-([1-9][0-9]?)?[0-9])*$/'
>>
>> >> Untested BTW. I've made 78-034-89 invalid, as a number should not
>> >> start
>> >> with zero, unless it's only 1 zero. 't Would be simpler if it was
>> >> valid,
>> >> your choice.
>>
>> > works like a charm, can you explain the simple way without check zero
>> > at start?
>>
>> '/^ # match start of string
>> [0-9]{1,3} # match 1 to 3 digits
>> ( # start subpattern
>> - # literal -
>> [0-9]{1,3} # match 1 to 3 digits
>> )* # match subpattern zero or more times
>> $ # match has to run all the way up untill the end
>> of the string
>> /x'
>>
>> When starting with regexes, and certainly for the more complex ones,
>> don't
>> underestimate the power of /x: comments in the regex itself, indentation
>> to keep things clear, it does wonders. It also keeps you from
>> rediscovering how the hell your regex worked 6 months after you wrote it
>> when you have to track a bug.....
>> --
>> Rik Wasmus
>
> Thanks !!! simple and beatifull
first, very good rik.
second, dave...if you're going to cross post the same question, at least
have the courtesy to go back and say, 'i found an answer, thanks for your
time.' shortly after your posting to alt.php, you had two responses...one of
which (not mine, btw), is much more simply expressed than rik's. either way,
be courteous to both of the groups you posted to...that way, we'll keep
responding to your questions rather than thinking that someone somewhere
else is responding.
Navigation:
[Reply to this message]
|