Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?B?Sm9zaHVhIENyYW5tZXIg8J+Qpw==?= Newsgroups: comp.lang.java.programmer Subject: Re: regex tidy Date: Fri, 23 May 2014 15:05:41 -0500 Organization: A noiseless patient Spider Lines: 24 Message-ID: References: <375vn9th0bc1faa04eg136be53hbvfjq4v@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 23 May 2014 20:05:46 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="8b6ddcc38ecbed2f71b30fabadb8aa0b"; logging-data="14951"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZpNtqczKyORuZMOxfVOH8ZkMpKb7YJa0=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Thunderbird/30.0 In-Reply-To: Cancel-Lock: sha1:1nwl4vqybqV/JmMQSId2sjCZ/jI= Xref: csiph.com comp.lang.java.programmer:28552 On 5/23/2014 2:33 PM, Leif Roar Moldskred wrote: > Roedy Green wrote: >> >> What would a Java regex tidier do? >> > > Evil. > > Regular expressions should be written to as closely as > possible mirror a mental or conceptual model of the > inputs it will match, and not to be as short or compact > as possible. > > For instance, to match dates on the pattern "YY-MM-dd" > your regular expression should be "\\d{2}-\\d{2}-\\d{2}" > and not "((^|-)\\d{2}){3}" Those two regexes aren't actually equivalent. The latter matches -00-00-00, for example. I assume you meant "\\d{2}(?:-\\d{2}){2}"? -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth