Re: regex capability
On 4/5/2011 6:33 AM, Robert Klemme wrote:
On Apr 5, 2:35 am, markspace<-@.> wrote:
Why re-invent the wheel?
In this case I just wanted to demonstrate the strategy to first check
overall validity of the input and extract the interesting part and
then ripping that interesting part apart. Whether a Scanner or
another Matcher is used for the second step wasn't that important to
me. Also, the thread is called "regex capability". :-)
Fair enough. :)
But, of course, your approach using the Scanner is perfectly
compatible with the two step strategy as Patricia also pointed
out. :-)
Don't forget too that Scanner can do other things besides use
delimiters. It has methods like skip() and findInLine() that ignore
delimiters and could be used to build a simple parser. You can also
change the delimiters on the fly to extract different sections of text.
A simple change to my example above:
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.findInLine( "Support DDR2" );
scanner.useDelimiter( "[ /]+" );
while( scanner.hasNextInt() ) {
System.out.println( scanner.nextInt() );
}
}
}