Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 05 Apr 2011 07:29:09 -0500 Date: Tue, 05 Apr 2011 05:28:59 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: regex capability References: <8vun3sFs1lU1@mid.individual.net> <3fc6e9f5-cf25-44f5-885e-48129ae2e8b3@e9g2000vbk.googlegroups.com> In-Reply-To: <3fc6e9f5-cf25-44f5-885e-48129ae2e8b3@e9g2000vbk.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 45 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-yNVVcK6p4cBvBrOygdNjZnx5YiuWO1s5cYcy5QFcyrERgV7PjEUPXeufYsZ5ety8BxiyC9DuJKjAyxD!ami5wtsY5tS8sj4LZahMgauwLmPxEL2A3Npoq+yjJV1I7MNUKAQX56xUcqm188b4lc2H6yvfOfTS!SDVc2kCuREkUN57KvKu7uiV0qD6jYVs/7PebzCyuwyE= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2621 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:2884 On 4/5/2011 2:10 AM, Paul Cager wrote: > On Apr 5, 2:35 am, markspace<-@.> wrote: >> On 4/4/2011 1:13 PM, Robert Klemme wrote: >> >>> if ( m.matches() ) { >>> for (m = number.matcher(m.group(1)); m.find();) { >>> int x = Integer.parse(m.group()); >>> } >> >> Why re-invent the wheel? >> >> public class ScannerTest { >> public static void main(String[] args) { >> StringReader in = new StringReader( >> "Support DDR2 100/200/300/400 DDR2 SDRAM"); >> >> Scanner scanner = new Scanner(in); >> scanner.useDelimiter( "[^0-9]+" ); >> while( scanner.hasNextInt() ) { >> System.out.println( scanner.nextInt() ); >> } >> } >> >> } >> >> (Lightly tested.) > > $ java ScannerTest > 2 > 100 > 200 > 300 > 400 > 2 This is a nice illustration of the case for a strategy I often use in this sort of situation, combining tools using each to do the jobs it does best. For example, a regular expression match could pull out the "100/200/300/400" substring, and a Scanner could extract the integers from that. More generally, it could be split and then each of the split results processed some other way. Patricia