在下面的示例代码中,我根据正则表达式过滤字符串列表,因为我知道只有一个条目可以匹配该字符串。然后,我使用相同的匹配字符串从单个剩余值中获取 2 个分组值。
let input = ["aaaa bbbb";"aaabbbb";"cccc$$$$";"dddddda";" "]
let ValuesOfAB (input: string list) =
let matchString = "(?<a>\w+)\s(?<b>\w+)"
let value = input |> List.filter (fun line -> Regex.Matches(line, matchString).Count <> 0)
|> List.head
(Regex.Matches(value, matchString).[0].Groups.["a"].Value, Regex.Matches(value, matchString).[0].Groups.["b"].Value)
let a = ValuesOfAB input
有没有更好的方法让我不必再次在同一个字符串上使用 Regex.Matches 来获取我希望返回的值?