package re
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
RE is a regular expression library for OCaml
Install
dune-project
Dependency
github.com
Readme
Changelog
LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception License
Edit opam file
Versions (20)
Authors
Maintainers
Sources
1.14.0.tar.gz
md5=03f4a83100cb9229a796b85c698076e1
sha512=cd2cc39f951ca6b7be631bbb5531ed13bc040e629842671bf6fef3911b20ef1653fa9a1f0aa23b094d252cffc9a9efe7ffca69e50d362ab935bc0cc447548124
doc/src/re/replace.ml.html
Source file replace.ml
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53let replace ?(pos = 0) ?len ?(all = true) re ~f s = if pos < 0 then invalid_arg "Re.replace"; let limit = match len with | None -> String.length s | Some l -> if l < 0 || pos + l > String.length s then invalid_arg "Re.replace"; pos + l in (* buffer into which we write the result *) let buf = Buffer.create (String.length s) in (* iterate on matched substrings. *) let rec iter pos on_match = if pos <= limit then ( match Compile.match_str ~groups:true ~partial:false re s ~pos ~len:(limit - pos) with | Match substr -> let p1 = Group.start_offset substr 0 |> Group.Offset.get_no_check in let p2 = Group.stop_offset substr 0 |> Group.Offset.get_no_check in if pos = p1 && p1 = p2 && on_match then ( (* if we matched an empty string right after a match, we must manually advance by 1 *) if p2 < limit then Buffer.add_char buf s.[p2]; iter (p2 + 1) false) else ( (* add string between previous match and current match *) Buffer.add_substring buf s pos (p1 - pos); (* what should we replace the matched group with? *) let replacing = f substr in Buffer.add_string buf replacing; if all then (* if we matched an empty string, we must manually advance by 1 *) iter (if p1 = p2 then ( (* a non char could be past the end of string. e.g. $ *) if p2 < limit then Buffer.add_char buf s.[p2]; p2 + 1) else p2) (p1 <> p2) else Buffer.add_substring buf s p2 (limit - p2)) | Running _ -> () | Failed -> Buffer.add_substring buf s pos (limit - pos)) in iter pos false; Buffer.contents buf ;; let replace_string ?pos ?len ?all re ~by s = replace ?pos ?len ?all re s ~f:(fun _ -> by)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>