#始めに
F#でのファイル入出力処理を書いてみました
ついでにyahooファイナンスからURLを抽出する処理も書いてみました
#コード
module File3
open System.IO;
open System.Text;
//一文字単位の解析を行う
//ダブルクオテーションで囲まれている文字列を抜き出す
let rec parseStr1 strs hasDoubleQuaote =
match strs with
|x::xs -> match x with
//ダブルクオーテーションが見つかったときの処理
|'"' -> match hasDoubleQuaote with
//すでにダブルクオテーションが見つかっている場合は、ここまでが切り出し対象
|true -> '|' :: parseStr1 xs false
//まだにダブルクオテーションが見つかっていない場合は、ここからが切り出し対象
|false -> parseStr1 xs true
//ダブルクオーテーション以外の文字がマッチしたら、ダブルクオテーションがすでに見つかっている場合のみ、
//文字の切り出しを行う
|_ -> match hasDoubleQuaote with
|true -> x::(parseStr1 xs hasDoubleQuaote)
|false -> parseStr1 xs hasDoubleQuaote
|[]->[]
let parseStr strs = parseStr1 strs false
//一行単位の解析を行う
//テキストファイルから1行読み込んだ文字列を解析します
let analyzeText (str:string) f =
let ff1 = str.ToCharArray(0,str.Length) |> //文字列を文字型の配列にする
Array.toList |> //配列をリストに変換する
f |> //引数で渡された関数を実行する
List.toSeq |> //実行結果をシーケンスに変換する
Seq.map string |> //シーケンスのオブジェクトを文字列型に変換する
String.concat "" ; //シーケンスに含まれる文字列を連結する
let n = ff1.IndexOf("http:");
let f3 = match n > 0 with
|true -> ff1.Substring(n) + "\n"
|false -> null
f3
//テキストファイル全体の内容を解析する
let rec printParsedStr series =
match series with
|(x:string)::xs -> match x.IndexOf("http")>0 with
|true -> analyzeText x parseStr + printParsedStr xs
|false -> printParsedStr xs
|[] -> ""
//yahooファイナンスのhtmlファイルを読み込み、そこに含まれるurlを抽出する
let inputPath = @"C:\test\yahoo.txt" //yahooファイナンスのhtmlソースが含まれている
let outputPath= @"C:\test\result.txt" //抽出結果が格納されているテキストファイル
File.WriteAllText(
outputPath,
Seq.toList (File.ReadLines(inputPath)) |> printParsedStr //A
)