OMG*_*chy 5 f# pattern-matching
我有一个模式匹配其参数的函数,它是string:
let processLexime lexime
match lexime with
| "abc" -> ...
| "bar" -> ...
| "cat" -> ...
| _ -> ...
Run Code Online (Sandbox Code Playgroud)
这按预期工作.但是,我现在试图通过表达"匹配string仅包含以下字符" 来扩展它.在我的具体示例中,我希望只匹配包含数字的任何内容.
我的问题是,我怎样才能在F#中表达这一点?我更喜欢在没有任何库的情况下这样做FParsec,因为我主要是为了学习目的而这样做.
您可以使用活动模式:https://msdn.microsoft.com/en-us/library/dd233248.aspx
let (|Integer|_|) (str: string) =
let mutable intvalue = 0
if System.Int32.TryParse(str, &intvalue) then Some(intvalue)
else None
let parseNumeric str =
match str with
| Integer i -> printfn "%d : Integer" i
| _ -> printfn "%s : Not matched." str
Run Code Online (Sandbox Code Playgroud)