-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.fsx
More file actions
executable file
·53 lines (40 loc) · 1.36 KB
/
regex.fsx
File metadata and controls
executable file
·53 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env -S dotnet fsi
#load "Lib/Extensions.fs"
open Extensions
let tryParseInt = System.Int32.tryParse
let (|Integer|_|) (str:string) =
System.Int32.tryParse str
let (|Match|_|) (regex:string) str =
let m = System.Text.RegularExpressions.Regex(regex).Match(str)
match m.Success with
| false -> None
| true -> Some [ for g in (Seq.skip 1 m.Groups) -> g.Value ]
let (|Matches|_|) (regex:string) str =
let ms = System.Text.RegularExpressions.Regex(regex).Matches(str)
if ms.Count = 0
then None
else Some [ for m in ms -> [ for g in (Seq.skip 1 m.Groups) -> g.Value ]]
let (|IntString|) lol =
List.map (fun ([Integer x;str]) -> x,str) lol
let (|Default|) noValue opt =
match opt with
| None -> noValue
| Some x -> x
match "1a2b3c4d5e6f" with
| Matches @"(\d+)(\D+)" (IntString x) -> printfn "%A" x
| Match @"(\d+)" [Integer num] -> printfn "%d" num
| _ -> printfn "Nothing"
let userInput input =
let input = defaultArg (System.Int32.tryParse input) 0
printfn "User Input + 10: %d" (input + 10)
userInput "10"
userInput "1"
userInput "12foo"
let printOpt opt =
printfn "Opt: %A" opt
let incrOpt (Default 0 x) =
Some (x+1)
printOpt (incrOpt None)
printOpt (incrOpt (Some 1))
printOpt (incrOpt (Some 2))
printOpt (incrOpt (incrOpt (incrOpt (incrOpt (tryParseInt "")))))