Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32755

Re: regexp 0 - 3 digits

Newsgroups comp.lang.javascript
Subject Re: regexp 0 - 3 digits
From "Evertjan." <exxjxw.hannivoort@inter.nl.net>
References <v_edndmyGa_2KlXFnZ2dnUU7-I_NnZ2d@westnet.com.au> <173p630vvwvm8.1i0p4mivdhm7b.dlg@40tude.net> <oaak7j$1ag0$1@csiph.com>
Date 2017-03-15 09:19 +0100
Message-ID <XnsA7395EE1BE64eejj99@194.109.6.166> (permalink)

Show all headers | View raw


Cezary Tomczyk <cezary.tomczyk@gmail.com> wrote on 15 Mar 2017 in 
comp.lang.javascript:

> On 15/03/2017 04:55, JJ wrote:
>> On Wed, 15 Mar 2017 14:39:22 +1100, Andrew Poulos wrote:
>>> I have this string
>>>    dd:dd:dd,ddd
>>> where the d's are digits and I'm using this to match it
>>>    match(/^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]/);
>>>
>>> Now I find that after the comma there may be from 0 to 3 digits.
>>>
>>> How can I modify the expression to handle that?
>>>
>>> Andrew Poulos
>>
>>   /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9]{0,3}/
>>
>> You can short it to:
>>
>>   /^[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{0,3}/
> 
> or to:
> 
> /^[\d]{2}:[\d]{2}:[\d]{2},[\d]{0,3}/

\d\d is shorter than \d{2}

No reason for the '[]'s
and
'\d\d' is shorter than '\d{2}'

/^\d\d:\d\d:\d\d,\d{0,3}/

Meseems you want no trailing characters,
so add a final '$':

/^\d\d:\d\d:\d\d,\d{0,3}$/

But probably you want to have no comma, 
when there are zero decimals:

/^\d\d:\d\d:\d\d(,\d{1,3})?$/

or if you want to make the comma optional,
when no decimals:

/^\d\d:\d\d:\d\d(,|,\d{1,3})?$/

==================

testing:

<script type='text/javascript'> 'use strict'
	function w(x,a) { document.write(x.test(a)+'<br>'); };
	w(/^\d\d:\d\d:\d\d(,\d{1,3})?$/, '01:01:01,12'); // true
	w(/^\d\d:\d\d:\d\d(,\d{1,3})?$/, '01:01:01,12 '); // false
	w(/^\d\d:\d\d:\d\d(,\d{1,3})?$/, '01:01:01'); // true
	w(/^\d\d:\d\d:\d\d(,\d{1,3})?$/, '01:01:01,'); // false
	w(/^\d\d:\d\d:\d\d(,|,\d{1,3})?$/, '01:01:01'); // true
	w(/^\d\d:\d\d:\d\d(,|,\d{1,3})?$/, '01:01:01,'); // true
</script>

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

regexp 0 - 3 digits Andrew Poulos <ap_prog@hotmail.com> - 2017-03-15 14:39 +1100
  Re: regexp 0 - 3 digits JJ <jj4public@vfemail.net> - 2017-03-15 10:55 +0700
    Re: regexp 0 - 3 digits Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-03-15 06:42 +0100
      Re: regexp 0 - 3 digits "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-03-15 09:19 +0100
        Re: regexp 0 - 3 digits Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2017-03-15 09:29 +0100
          Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 03:01 -0700
          Re: regexp 0 - 3 digits "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-15 05:39 -0700
        Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 05:20 -0700
          Re: regexp 0 - 3 digits "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-15 05:43 -0700
            Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 05:48 -0700
              Re: regexp 0 - 3 digits "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-15 06:28 -0700
                Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 06:43 -0700
                Re: regexp 0 - 3 digits Jon Ribbens <jon+usenet@unequivocal.eu> - 2017-03-15 14:06 +0000
                Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 07:56 -0700
                Re: regexp 0 - 3 digits Jon Ribbens <jon+usenet@unequivocal.eu> - 2017-03-15 16:05 +0000
                Re: regexp 0 - 3 digits Julio Di Egidio <julio@diegidio.name> - 2017-03-15 21:12 -0700
                Re: regexp 0 - 3 digits Jon Ribbens <jon+usenet@unequivocal.eu> - 2017-03-16 11:04 +0000
                Re: regexp 0 - 3 digits "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-15 08:12 -0700
  Re: regexp 0 - 3 digits "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-03-15 05:45 -0700
  Re: regexp 0 - 3 digits Dr J R Stockton <J.R.Stockton@physics.org> - 2017-03-15 11:54 -0700
    Re: regexp 0 - 3 digits Dr J R Stockton <reply1700@merlyn.demon.co.uk.invalid> - 2017-04-03 19:59 +0100
      Re: regexp 0 - 3 digits "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-04-04 09:38 +0200
        Re: regexp 0 - 3 digits Dr J R Stockton <J.R.Stockton@physics.org> - 2017-04-04 06:20 -0700

csiph-web