当前位置: 技术问答>java相关
在JAVA中如何用正侧表达式
来源: 互联网 发布时间:2015-11-02
本文导语: 比如我要把字符中的数字替换成*号,把一串字符中的空格删了,请问如何做呢 | http://www-106.ibm.com/developerworks/java/library/j-mer0827/ Magic with Merlin: Parse sequences of characters with the new regex libra...
比如我要把字符中的数字替换成*号,把一串字符中的空格删了,请问如何做呢
|
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 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 longest.length()) {
longest = words[i];
}
}
}
// Report
System.out.println("Longest word: " + longest);
// Close
input.close();
} catch (IOException e) {
System.err.println("Error processing");
}
}
}