Created
October 9, 2014 01:53
-
-
Save sayurin/bf4b8ffd6cffcd65d685 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Text.RegularExpressions | |
let parse1 line = | |
// TODO: 先頭から解析できる範囲だけであり終端チェックはされていない | |
let matches = Regex.Matches(line, @"(?:^|\G,)(?<key>[^=,]+)=(?<val>[^=,]+)") | |
Array.init matches.Count (fun i -> matches.[i].Groups.["key"].Value, matches.[i].Groups.["val"].Value) | |
let parse2 line = | |
// TODO: 要素数0が認められない | |
let m = Regex.Match(line, "^(?<key1>[^=,]+)=(?<val1>[^=,]+)(?:,(?<key>[^=,]+)=(?<val>[^=,]+))*$") | |
if not m.Success then None else | |
let keys, vals = m.Groups.["key"].Captures, m.Groups.["val"].Captures | |
let pairs = List.init keys.Count (fun i -> keys.[i].Value, vals.[i].Value) | |
(m.Groups.["key1"].Value, m.Groups.["val1"].Value) :: pairs |> Some | |
let parse3 line = | |
let m = Regex.Match(line, "^(?:(?:^|,)(?<key>[^=,]+)=(?<val>[^=,]+))*$") | |
if not m.Success then None else | |
let keys, vals = m.Groups.["key"].Captures, m.Groups.["val"].Captures | |
Array.init keys.Count (fun i -> keys.[i].Value, vals.[i].Value) |> Some | |
// parse1 "a=b,c=d,e" | |
// parse2 "" | |
parse3 "a=b,c=d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment