Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: regex capability Date: Mon, 04 Apr 2011 22:13:39 +0200 Lines: 48 Message-ID: <8vun3sFs1lU1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net gy2gFRAtfn8t5Ys3LvTG0A0dVjRATAaQIEkkAibGcI/TQl6hI= Cancel-Lock: sha1:dJ88gnNZdmFpLAEMoEfBqDyCD6E= User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.15) Gecko/20110303 Lightning/1.0b2 Thunderbird/3.1.9 In-Reply-To: X-Antivirus: avast! (VPS 110404-1, 04.04.2011), Outbound message X-Antivirus-Status: Clean Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2852 On 04.04.2011 10:26, bugbear wrote: > Roedy Green wrote: >> Consider a string like this: >> >> Support DDR2 1066/800/667/533/400 DDR2 SDRAM >> >> Is it possible to compose a regex that will peel out those numbers for >> you each in its own field, or do you have to extract the string >> "1066/800/667/533/400" and use split? >> >> The various things I have tried just grab the last number. > > I think normal practice (in Perl, and Java) would be repeated > use of a fairly simple regexp. > > In Java, I use > > while(matcher.find()) { > ... > } > > The key is that Matcher is stateful. And for added security a two level approach could be taken: // untested Pattern whole = Pattern.compile("Support DDR2 (\\d+(?:/\\d+)*) DDR2 SDRAM"); Pattern number = Patter.compile("\\d+"); Matcher m = whole.matcher(input); if ( m.matches() ) { for (m = number.matcher(m.group(1)); m.find();) { int x = Integer.parse(m.group()); } } else { // error? } Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/