ให้ทำการสร้าง package ชื่อ match และสร้างไฟล์ HrefMatch.java เขียนโค้ดเข้าไปดังนี้
package match; import java.io.*; import java.net.*; import java.util.regex.*; public class HrefMatch { public static void main(String[] args) { try { // get URL string from command line or use default String urlString; if (args.length > 0) { urlString = args[0]; } else { urlString = "http://www.javathailand2.com"; } // open reader for URL InputStreamReader in = new InputStreamReader(new URL(urlString).openStream()); // read contents into string builder StringBuilder input = new StringBuilder(); int ch; while ((ch = in.read()) != -1) { input.append((char) ch); } // search for all occurrences of pattern String patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>"; Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); String match = input.substring(start, end); System.out.println(match); } } catch (IOException e) { e.printStackTrace(); } catch (PatternSyntaxException e) { e.printStackTrace(); } } }
ผลการทำงาน
<a href='http://www.javathailand2.com/'>
<a href='http://www.javathailand2.com/p/blog-page_1.html'>
<a href='http://www.javathailand2.com/p/blog-page.html'>
<a href='http://www.javathailand2.com/p/inhouse-trainning.html'>
<a href='http://www.javathailand2.com/p/blog-page_10.html'>
<a href='http://www.javathailand2.com/2019/01/2018.html'>
<a href='http://www.javathailand2.com/2018/12/regular-expression.html'>
<a href='http://www.javathailand2.com/2018/12/memory-map-file.html'>
<a href='http://www.javathailand2.com/2018/12/serialization-and-cloning.html'>
<a href='http://www.javathailand2.com/2018/12/objectstream-zip-programming.html'>
<a href='http://www.javathailand2.com/2018/12/reading-and-writing-binary-data.html'>
<a href='http://www.javathailand2.com/2018/12/text-file.html'>
<a href='http://www.javathailand2.com/2018/12/interrupt.html'>
<a href='http://www.javathailand2.com/2018/12/sleep.html'>
<a href='http://www.javathailand2.com/2018/12/thread-rescheduling.html'>
<a href='https://plus.google.com/103560976406473444540'>
<a href='http://www.javathailand2.com/2019/01/2018.html'>
<a href='http://www.javathailand2.com/2018/12/text-file.html'>
<a href='http://www.javathailand2.com/2019/01/2018.html'>
<a href='http://www.javathailand2.com/2018/12/sro-self-running-object.html'>
<a href='http://www.javathailand2.com/2018/12/thread-rescheduling.html'>
<a href='http://www.javathailand2.com/2018/12/thread-priorities.html'>
<a href='http://www.javathailand2.com/2018/12/serialization-and-cloning.html'>
<a href='http://www.javathailand2.com/2018/12/thread_17.html'>
<a href='http://www.javathailand2.com/2018/12/sleep.html'>
<a href='http://www.javathailand2.com/2018/12/super-references.html'>
<a href='http://www.javathailand2.com/2018/12/objectstream-zip-programming.html'>
โปรแกรมจะมองเห็นทุกอย่างที่เข้าข่ายกับตัวตรวจสอบ regex ของ match ซึ่งก็คือ a ทุกตัวที่มี href ตามเงื่อนไขครับ
ไม่มีความคิดเห็น:
แสดงความคิดเห็น