[英]Iterating over split string in OCaml
Let's say I have a string:假设我有一个字符串:
"ab bc cdv gf
ed aqb ahf sd
abcdef
I want to a) Split it by ' '
and/or '\r\n'
, '\t'
b) Iterate over newly created list of these substrings, split by separators and match each of them to some criteria (for example, only choose words starting with 'a'
, aka ["ab", "ahf", "abcdef"]
我想 a) 按' '
和/或'\r\n'
、 '\t'
拆分它 b) 遍历新创建的这些子字符串列表,按分隔符拆分并将它们中的每一个与某些条件匹配(例如,只选择以'a'
开头的单词,又名["ab", "ahf", "abcdef"]
Note: also we can't use Str
or any other additional libraries.注意:我们也不能使用Str
或任何其他附加库。
I came up with some sort of this code:我想出了一些这样的代码:
let f g =
String.split_on_char ' ' g
|> List.iter (fun x -> x);;
Obviously though, it shows an error.显然,它显示了一个错误。 And even if it worked, it wouldn't have split out the '\r\n'
.即使它有效,它也不会拆分出'\r\n'
。 Instead of List.iter
I could have used List.map (fun x -> x)
, but I will just get the split (by ' '
character only) list of substrings.我可以使用List.map (fun x -> x)
而不是List.iter
,但我只会得到子字符串的拆分列表(仅按' '
字符)。 So now another question: how can I use所以现在另一个问题:我该如何使用
"match (something?) with
| ..."
in this case?在这种情况下? I see no way in adding match into the code above.我看不出在上面的代码中添加 match 的方法。 Do we use the reverse |>
and List.iter
in this case or is there another way I'm not aware of?在这种情况下我们是使用反向|>
和List.iter
还是有另一种我不知道的方式?
Simple approach: let's just keep splitting on whietspace characters we want to split on, use List.concat_map
to maintain a "flat" list, and then reject empty lists.简单的方法:让我们继续拆分我们想要拆分的空白字符,使用List.concat_map
维护一个“平面”列表,然后拒绝空列表。
let s = "ab bc cdv gf ed aqb ahf sd abc\r\ndef"
let split = String.split_on_char in
List.(
split ' ' s
|> concat_map (split '\n')
|> concat_map (split '\r')
|> filter ((<>) "")
)
(* Result:
* ["ab"; "bc"; "cdv"; "gf"; "ed"; "aqb"; "ahf"; "sd"; "abc"; "def"]
*)
You might also use your regular expression library of choice and split on \s+
, but apparently that isn't allowed.您也可以使用您选择的正则表达式库并在\s+
上拆分,但显然这是不允许的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.