F#活动模式匹配多个模式 [英] F# active pattern match multiple patterns

查看:80
本文介绍了F#活动模式匹配多个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一些具有以下格式的文本文件:

I have some text file with the following format:

07:12:07:750-> 10 8 -1 1 98887602 ABC

07:12:07:750 -> 10 8 -1 1 98887602 ABC

在上面的示例中:前几个字节是时间:07:12:07:750;时间过后,尾随->"没有用然后是"10 8 -1",然后是一个有用的序列号,在此示例中,它是"1",然后是一个无用的长数字,在这里是"98887602",然后 一个有用的符号,这里是"ABC"

In the above example: the first few bytes are the time: 07:12:07:750; after the time, the trailing "->" is useless; then followed by "10 8 -1" then a useful sequence #, in this example, it is "1", then a useless long digit, here it is "98887602", then a useful symbol, here, it is "ABC"

我想使用以下功能提取3个有用的信息:时间,序列号和符号.在上面的示例中,有用的信息是:"07:12:07"(我不需要:750),"1"和"ABC"

I want to use the following functions to extract 3 useful information: the time,  the sequence #, and the symbol.  In the above example, the useful information is: "07:12:07" (I don’t need :750), "1" and "ABC"

let (| Match1 | _ |)(模式:字符串)(输入:字符串)=

    let m = Regex.Match(输入,模式)

    let m = Regex.Match(input, pattern) in

    如果 m.Success

    然后 一些(List.tail [ for in m.Groups ->

    then Some (List.tail [ for g in m.Groups -> g.Value ])

    其他

let (| Match3 | _ |)(模式:字符串)(输入:字符串)=

    匹配 (| Match1 | _ ||)模式输入 with

    match (|Match1|_|) pattern input with

    |一些(一个::两个::三个:: []) ->

    | Some (one :: two :: three :: []) -> Some (one, two, three)

    |一些[] -> failwith "Match3成功,但未找到任何组."

    | Some [] -> failwith "Match3 succeeded, but no groups found."

    |一些_ -> failwith 匹配3成功,但没有3个匹配项."

    | Some _ -> failwith "Match3 succeeded, but did not 3 matches."

    |无 -> 无;

    | None -> None 

我想我可以做类似的事情:

I think I can do something like:

let input = @&; 07:12:07:750-> 10 8 -1 1 98887602 ABC"

let input = @"07:12:07:750 -> 10 8 -1 1 98887602 ABC"

let pattern = @" ^(\ d {2}:\ d {2}:\ d {2} )(.{8} 10 8 -1 \ d +)"

let pattern = @"^(\d{2}:\d{2}:\d{2})(.{8}10 8 -1 \d+)"

let 时间,seqNo ,符号=

            匹配 输入 with

               match input with

            | Match3模式(a,b,c) ->

               | Match3 pattern (a, b, c) -> (a, b, c)

            | _ -> 找不到匹配项."

               | _ -> failwith "Match not found."

但这没有用.

欢迎提出任何想法!

谢谢,

推荐答案

当正则表达式只有两个捕获组时,列表中将不会包含三个元素.

You won't get three elements in a list when the regex only has two capture groups.

通过添加跟踪语句

    | Some x -> 
      printfn "%A" x
      failwith "Match3 succeeded, but did not 3 matches."

进入Match3,而不是Some _子句,将匹配显示为["07:12:07"; ":750-> 10 8 -1 1"] –分别匹配组(\ d {2}:\ d {2}:\ d {2})和(.{8} 10 8 -1 \ d +).

into Match3, instead of the Some _ clause, the matches are shown to be ["07:12:07"; ":750 -> 10 8 -1 1"] -- matching the groups (\d{2}:\d{2}:\d{2}) and (.{8}10 8 -1 \d+) respectively.

修复正则表达式

let pattern = @"^(\d{2}:\d{2}:\d{2}).{8}10 8 -1 (\d+) \d* (.*)"

然后产生

val ( |Match1|_| ) : string -> string -> string list option
val ( |Match3|_| ) : string -> string -> (string * string * string) option
val input : string = "07:12:07:750 -> 10 8 -1 1 98887602 ABC"
val pattern : string = "^(\d{2}:\d{2}:\d{2}).{8}10 8 -1 (\d+) \d* (.*)"
val time : string = "07:12:07"
val symbol : string = "ABC"
val seqNo : string = "1"


这篇关于F#活动模式匹配多个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆