Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Gene Wirchenko Newsgroups: comp.lang.java.programmer Subject: Re: Confusion about String.matches method Date: Wed, 08 Jun 2011 07:50:13 -0700 Organization: A noiseless patient Spider Lines: 49 Message-ID: References: <50016a00-9422-46d4-8278-619fedafff55@c41g2000yqm.googlegroups.com> <2fd869d6-c2f6-4360-85b4-6df2b6e68fb0@p13g2000yqh.googlegroups.com> <94n2jpFa1oU1@mid.individual.net> <4deca972$0$12380$c30e37c6@exi-reader.telstra.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="7Qrvczazr82YckO5XW8Vtw"; logging-data="10164"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hW1A0b7PCvI38mqw5pZge0iFMXkK2jBk=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:vbUc6e7FiYQuTSvXoCscSQMSEn4= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5096 On Tue, 07 Jun 2011 21:18:54 -0700, Roedy Green wrote: [snip] >The problem with regexes is all it takes is one char off an the whole >thing does not work. You have no clue where the problem is. You >rarely find errors with syntax checking. There is no trace. >The other problem is a regex will work 90% of the time. It may be >quietly rejecting a small percentage of the strings, and you might not >notice. There are more problems than that. I assume that you are familiar with this quote: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. I find regexes to be less than totally useful. I sometimes have to define a format string with substitution parameters. Here is an example: Per client's instruction, the total of all invoices for the current month will be charged against the supplied credit card number on %D unless we hear otherwise prior to that date. The date gets substituted for the %D. There are a few rules. There must be one and only "%D" string. "%" is an escape character and is doubled for the literal "%". I could write a regex for this, BUT I also have to have a routine for executing the string substitution, and regexes do not help with this. I do not want two rather different versions of the code. (As it is, I have two versions of code that are somewhat similar.) More importantly, if one routine gets changed, so should the other, and it should be obvious how to do it. If I wanted to add a second variable to the example above, say a contact name, and wanted the constraint of appearing once and only once, using a regex would get even uglier. I could use regexes for such things as validating with no interpretation, but such data that I have to validate usually has trivial formatting. For example, a Canadian Postal Code is "A9A 9A9" with some limitations on the alphabetic characters. A regex would be overkill. Sincerely, Gene Wirchenko