[Java 教學範例拷貝]- Pattern、Matcher
[Java 教學範例拷貝]- Pattern、Matcher
剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/
http://openhome.cc/Gossip/JavaGossip-V1/PatternMatcher.htm
import java.util.regex.*;
public class UsePatternMatcher {
public static void main(String[] args) {
String phones1 =
"Justin's phone number: 0939-100391\n" +
"momor's phone number: 0939-666888\n";
Pattern pattern = Pattern.compile(".*0939-\\d{6}");
Matcher matcher = pattern.matcher(phones1);
while(matcher.find()) {
System.out.println(matcher.group());
}
String phones2 =
"caterpillar's phone number: 0952-600391\n" +
"bush's phone number: 0939-550391";
matcher = pattern.matcher(phones2);
while(matcher.find()) {
System.out.println(matcher.group());
}
}
}
|