http://www-106.ibm.com/developerworks/java/library/j-mer0827/
Magic with Merlin: Parse sequences of characters with the new regex library
Complete example
These two classes -- Pattern and Matcher -- are the whole pattern-matching library. Coming up with the right regular expression and then working with the results of the Matcher class is really all there is to the library. Until a dedicated book on regular expressions comes out for the Java language, find a good book on Perl to learn more about the specific patterns. Listing 1 provides a complete example by looking for the longest word in a particular file passed in from the command line as input.Listing 1. Longest word example
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*;
import java.util.regex.*;public class Longest {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("Provide a filename");
return;
} try {
// Map File from filename to byte buffer
FileInputStream input =
new FileInputStream(args[0]);
FileChannel channel = input.getChannel();
int fileLength = (int)channel.size();
MappedByteBuffer buffer = channel.map(
FileChannel.MapMode.READ_ONLY, 0, fileLength); // Convert to character buffer
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer); // Create line pattern
Pattern linePattern =
Pattern.compile(".*$", Pattern.MULTILINE); // Create word pattern
Pattern wordBreakPattern =
Pattern.compile("[\\p{Punct}\\s}]"); // Match line pattern to buffer
Matcher lineMatcher =
linePattern.matcher(charBuffer); // Holder for longest word
String longest = ""; // For each line
while (lineMatcher.find()) { // Get line
String line = lineMatcher.group(); // Get array of words on line
String words[] = wordBreakPattern.split(line); // Look for longest word
for (int i=0, n=words.length; i<n; i++) {
if (words[i].length() > longest.length()) {
longest = words[i];
}
}
}
// Report
System.out.println("Longest word: " + longest);
// Close
input.close();
} catch (IOException e) {
System.err.println("Error processing");
}
}
}
Magic with Merlin: Parse sequences of characters with the new regex library
Complete example
These two classes -- Pattern and Matcher -- are the whole pattern-matching library. Coming up with the right regular expression and then working with the results of the Matcher class is really all there is to the library. Until a dedicated book on regular expressions comes out for the Java language, find a good book on Perl to learn more about the specific patterns. Listing 1 provides a complete example by looking for the longest word in a particular file passed in from the command line as input.Listing 1. Longest word example
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*;
import java.util.regex.*;public class Longest {
public static void main(String args[]) {
if (args.length != 1) {
System.err.println("Provide a filename");
return;
} try {
// Map File from filename to byte buffer
FileInputStream input =
new FileInputStream(args[0]);
FileChannel channel = input.getChannel();
int fileLength = (int)channel.size();
MappedByteBuffer buffer = channel.map(
FileChannel.MapMode.READ_ONLY, 0, fileLength); // Convert to character buffer
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buffer); // Create line pattern
Pattern linePattern =
Pattern.compile(".*$", Pattern.MULTILINE); // Create word pattern
Pattern wordBreakPattern =
Pattern.compile("[\\p{Punct}\\s}]"); // Match line pattern to buffer
Matcher lineMatcher =
linePattern.matcher(charBuffer); // Holder for longest word
String longest = ""; // For each line
while (lineMatcher.find()) { // Get line
String line = lineMatcher.group(); // Get array of words on line
String words[] = wordBreakPattern.split(line); // Look for longest word
for (int i=0, n=words.length; i<n; i++) {
if (words[i].length() > longest.length()) {
longest = words[i];
}
}
}
// Report
System.out.println("Longest word: " + longest);
// Close
input.close();
} catch (IOException e) {
System.err.println("Error processing");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货