正規表現で、とにかく http で始まる URL らしきものの取得

  VS2010(C#)



01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Net;
06.using System.Text.RegularExpressions;
07.using System.IO;
08. 
09.namespace RegexTest {
10.    class Program {
11.        static void Main(string[] args) {
12. 
13.            // インターネットアクセス
14.            WebClient client = new WebClient();
15.            client.Encoding = Encoding.UTF8;
16.            //client.Encoding = Encoding.GetEncoding("shift_jis");
17.            //client.Encoding = Encoding.GetEncoding("euc-jp");
18.            string result = client.DownloadString("http://gigazine.net/");
19. 
20.            // 書き込み用テキストファイルの準備
21.            FileStream fs = new FileStream("result.txt",FileMode.Create,FileAccess.Write);
22.            StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("shift_jis"));
23. 
24.            sw.WriteLine("SHIFT_JIS で書き込んでいます");
25. 
26.            MatchCollection mc = Regex.Matches(result, "(https?://.+?)[\"']?[;)>\\s]");
27.            foreach (Match match in mc) {
28.                sw.WriteLine( match.Groups[1] );
29.            }
30. 
31.            sw.Close();
32.            sw.Dispose();
33.            fs.Close();
34.            fs.Dispose();
35. 
36.        }
37.    }
38.}



  PHP



01.<?php
02.header( "Content-Type: text/html; Charset=utf-8" );
03.header( "pragma: no-cache" );
04.header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
05.header( "Cache-control: no-cache" );
06.//Cache-Control: private, no-store, no-cache, must-revalidate
07. 
08.$result = file_get_contents("http://gigazine.net/");
09. 
10.preg_match_all("|(https?://.+?)[\"']?[;)>\\s]|u", $result, $matches, PREG_PATTERN_ORDER );
11. 
12.print "<pre>";
13.foreach( $matches[1] as $value ) {
14.    print($value . "\n");
15.}
16.print "</pre>";
17. 
18. 
19.?>



  Java

01.import java.io.BufferedReader;
02.import java.io.InputStream;
03.import java.io.InputStreamReader;
04.import java.io.PrintWriter;
05.import java.net.HttpURLConnection;
06.import java.net.URL;
07.import java.util.regex.Matcher;
08.import java.util.regex.Pattern;
09. 
10. 
11.public class HttpGetAndRegex {
12. 
13.    /**
14.     * @param args
15.     */
16.    public static void main(String[] args) {
17.         
18.        try {
19.            URL url = new URL("http://gigazine.net/");
20.            // 接続オブジェクト
21.            HttpURLConnection http = (HttpURLConnection)url.openConnection();
22.            http.setRequestMethod("GET");
23.            // 接続
24.            http.connect();
25.             
26.            // http から InputStream を取得する
27.            InputStream i_stream = http.getInputStream();
28.             
29.            // InputStream から リーダを作成する( キャラクタセットを指定 )
30.            // UTF-8 でリーダーを作成( インターネット上のデータが UTF-8 なので )
31.            InputStreamReader i_stream_reader = new InputStreamReader(i_stream, "UTF-8");
32.             
33.            // リーダを行単位で読み込める BufferedReader を使って全ての文字列を取得する )
34.            BufferedReader buffer_reader = new BufferedReader(i_stream_reader);
35.             
36.            String result_string = "";
37.            String line_buffer = null;  
38.            // BufferedReader は、readLine が null を返すと読み込み終了  
39.            while ( null != (line_buffer = buffer_reader.readLine() ) ) {  
40.                result_string += line_buffer;
41.            }
42.             
43.            // 全て閉じる
44.            buffer_reader.close();
45.            i_stream_reader.close();
46.            i_stream.close();
47.            http.disconnect();
48.             
49.            // **************************************************
50.            // 書き出し用テキストファイルの用意
51.            // http://docs.oracle.com/javase/jp/7/technotes/guides/intl/encoding.doc.html
52.            // **************************************************
53.            PrintWriter pw = new PrintWriter(".\\result.txt", "SHIFT_JIS");
54.//          PrintWriter pw = new PrintWriter(".\\result.txt", "EUC-JP");
55.            pw.println( "SHIFT_JIS で書き込んでいます" );
56.             
57.            // **************************************************
58.            // 正規表現による検索開始
59.            // **************************************************
60.            String regex = "(https?://.+?)[\"']?[;)>\\s]";
61.            Pattern pattern = Pattern.compile(regex);
62. 
63.            Matcher matcher = pattern.matcher(result_string);
64.            while(matcher.find()){
65.                System.out.println(matcher.group(1));
66.                pw.println( matcher.group(1) );
67.            }
68.             
69.            pw.flush();
70.            pw.close();
71.             
72.        }
73.        catch( Exception e ) {
74.            e.printStackTrace();
75.        }
76.         
77. 
78.    }
79. 
80.}










  infoboard   管理者用   





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ