我当前正在尝试使用ocaml-re
。文档很少。我想知道如何做类似的事情:
Str.regexp "example \\([A-Za-z]+\\)"
使用Re.Perl
?我认为这将帮助我自然地自己获取其余文档。谢谢!
如果您将此代码从Str
转换为Re.Perl
,则奖励积分:
let read_filename = "example.ts"
let filename = "example2.ts"
let () =
CCIO.(
let modify_file ~chunks =
let r = Str.regexp "example \\([A-Za-z]+\\)" in
match chunks () with
None -> chunks (* is the same as (fun () -> None) *)
| Some chunks ->
let test_chunks = Str.replace_first r "\\1" chunks in (* compute once *)
(fun () -> Some test_chunks) in
with_in read_filename
(fun ic ->
let chunks = read_chunks ic in
let new_chunks = modify_file ~chunks in
with_out ~flags:[Open_binary] ~mode:0o644 filename
(fun oc ->
write_gen oc new_chunks
)
)
)
答案 0 :(得分:2)
不要使用Re.Perl
,Re的API要简单得多。您可以使用以下方法构造您的re:
let re =
let open Re in
alt [rg 'A' 'Z'; rg 'a' 'z'] (* [A-Za-z] *)
|> rep1a (* [A-Za-z]+ *)
|> group (* ([A-Za-z]+) *)
|> compile