Scanner定界符与使用正则表达式扫描
import java.util.Scanner; import java.util.regex.MatchResult; /** * @author clydelou * */ public class ScannerDelimiter { /** * @param args */ static String threatData = "58.27.82.161@02/10/2005\n" + "204.45.234.40@02/11/2005\n" + "58.27.82.161@02/11/2005\n" + "58.27.82.161@02/12/2005\n" + "58.27.82.161@02/13/2005\n" + "[next log section with different data format]"; public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner("12, 42,78,99,42");// 默认情况下,Scanner根据空白字符对输入进行分词,但是你可以用正则表达式指定自己所需的定界符 scanner.useDelimiter("\\s*,\\s*");// 使用逗号(包括逗号前后任意的空白字符)作为定界符 System.out.println(scanner.delimiter());// 使用delimiter()方法用来返回当前正在作为定界符的Pattern对象 while (scanner.hasNextInt()) System.out.print(scanner.nextInt() + "\t"); System.out.println(); // 使用正则表达式扫描 // 当next()方法配合指定的正则表达式使用时,将找到下一个匹配该模式的输入部分,调用match()方法就可以获得匹配结果. // 注意:在配合正则表达式使用扫描时,它仅仅针对下一个输入分词进行匹配,如果正则表达式中含有界定符,那永远都不可能匹配成功. scanner = new Scanner(threatData); String pattern = "(\\d+[.]\\d+[.]+\\d+[.]\\d+)@" + "(\\d{2}/\\d{2}/\\d{4})"; // Capturing groups are indexed from left to right, starting at one. // Group zero denotes the entire pattern, so the expression m.group(0) // is equivalent to m.group(). // Groups and capturing // // Capturing groups are numbered by counting their opening parentheses // from left to right. In the expression ((A)(B(C))), for example, there // are four such groups: // // 1 ((A)(B(C))) // 2 (A) // 3 (B(C)) // 4 (C) // // Group zero always stands for the entire expression. // // Capturing groups are so named because, during a match, each // subsequence of the input sequence that matches such a group is saved. // The captured subsequence may be used later in the expression, via a // back reference, and may also be retrieved from the matcher once the // match operation is complete. // // The captured input associated with a group is always the subsequence // that the group most recently matched. If a group is evaluated a // second time because of quantification then its previously-captured // value, if any, will be retained if the second evaluation fails. // Matching the string "aba" against the expression (a(b)?)+, for // example, leaves group two set to "b". All captured input is discarded // at the beginning of each match. // // Groups beginning with (? are pure, non-capturing groups that do not // capture text and do not count towards the group total. // 组(Groups)是用括号划分的正则表达式,可以根据组的标号来引用某个组.组号为0表示整个表达式,组号1表示被第一对括号括起来的组,依此类推. while (scanner.hasNext(pattern)) { scanner.next(pattern); MatchResult match = scanner.match(); String ip = match.group(1); String date = match.group(2); System.out.format("Threat on %s from %s\n", date, ip); } } }