Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nigel Wade Newsgroups: comp.lang.java.programmer Subject: Re: Confusion about String.matches method Date: Wed, 01 Jun 2011 16:02:16 +0100 Lines: 29 Message-ID: <94n2jpFa1oU1@mid.individual.net> References: <50016a00-9422-46d4-8278-619fedafff55@c41g2000yqm.googlegroups.com> <2fd869d6-c2f6-4360-85b4-6df2b6e68fb0@p13g2000yqh.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: individual.net E2gT9rZedPNfiSmNdW6XFgFZsmrE95z4n6F5kdI9x1Bt5sMg+T Cancel-Lock: sha1:FCO+X6iyDBlFeOBQg1WrgO+HS+Y= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 In-Reply-To: <2fd869d6-c2f6-4360-85b4-6df2b6e68fb0@p13g2000yqh.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4857 On 01/06/11 15:10, laredotornado wrote: > > K, thought I had this all rigured out thanks to everyone's > suggestions, but I still have this one RE that's failing and I can't > figure out why. I have > > "G37 Convertible\n$45,750*".matches("^.*\\Q$45,750\\E.*$") > > which returns false. If I remove the new line ("\n"), it matches, but > I can't guarantee my input won't contain new lines. How can I modify > my regular expression to match? Thanks, - Dave Welcome to the wonderful world of RE, and strings. To match '\n' inside an RE you need to escape the '\', because it's a special RE character. To escape it you precede it with '\', the RE escape character. So what you actually need in the RE is '\\n'. But '\' is also a special character in a string, so you need to escape each '\' in the string - with a '\'. So, to get your '\\n' in the RE you need to have '\\\\n' in the string. Simple? [Anyone who claims they understand RE is just someone who hasn't yet realized they don't]. -- Nigel Wade