File: test_replace.ml

package info (click to toggle)
ocaml-re 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 848 kB
  • sloc: ml: 8,054; makefile: 18; sh: 11
file content (33 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
open Import

let%expect_test "test_replace" =
  let re = Re.Posix.compile_pat "[a-zA-Z]+" in
  let f sub = String.capitalize_ascii (Re.Group.get sub 0) in
  print_endline (Re.replace re ~f " hello world; I love chips!");
  [%expect {| Hello World; I Love Chips! |}];
  print_endline (Re.replace ~all:false re ~f " allo maman, bobo");
  [%expect {| Allo maman, bobo |}];
  print_endline (Re.replace re_empty ~f:(fun _ -> "a") "");
  [%expect {| a |}];
  print_endline (Re.replace (Re.compile (Re.rep (Re.char 'a'))) ~f:(fun _ -> "*") "cat");
  [%expect {| *c*t* |}]
;;

let%expect_test "test_replace_string" =
  let re = Re.Posix.compile_pat "_[a-zA-Z]+_" in
  print_endline (Re.replace_string re ~by:"goodbye" "_hello_ world");
  [%expect {| goodbye world |}];
  print_endline (Re.replace_string ~all:false re ~by:"brown" "The quick _XXX_ fox");
  [%expect {| The quick brown fox |}]
;;

let%expect_test "test_bug_55" =
  let re = Re.(compile bol) in
  let res = Re.replace_string re ~by:"z" "abc" in
  print_endline res;
  [%expect {| zabc |}];
  let re = Re.(compile eow) in
  let res = Re.replace_string re ~by:"X" "one two three" in
  print_endline res;
  [%expect {| oneX twoX threeX |}]
;;