Path: csiph.com!.POSTED.204-73-239-109.cust.centrio.cz!not-for-mail From: Cezary Tomczyk Newsgroups: comp.lang.javascript Subject: Re: regexp 0 - 3 digits Date: Wed, 15 Mar 2017 09:29:58 +0100 Organization: csiph.com Internet News Service Message-ID: References: <173p630vvwvm8.1i0p4mivdhm7b.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 15 Mar 2017 08:29:59 +0000 (UTC) Injection-Info: csiph.com; posting-host="204-73-239-109.cust.centrio.cz:109.239.73.204"; logging-data="50696"; mail-complaints-to="admin@kev009.com" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: Xref: csiph.com comp.lang.javascript:32757 On 15/03/2017 09:19, Evertjan. wrote: > Cezary Tomczyk 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} Yes and it does the same. > 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: > > Nice. Btw this is a very good case as a candidate to write first unit tests and then required function. -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility linting tool