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/pcre.ml.html
Source file pcre.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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179module Re = Core exception Parse_error = Perl.Parse_error exception Not_supported = Perl.Not_supported type regexp = Re.re type flag = [ `CASELESS | `MULTILINE | `ANCHORED | `DOTALL ] type split_result = | Text of string | Delim of string | Group of int * string | NoGroup type groups = Core.Group.t let re ?(flags = []) pat = let opts = List.map (function | `CASELESS -> `Caseless | `MULTILINE -> `Multiline | `ANCHORED -> `Anchored | `DOTALL -> `Dotall) flags in Perl.re ~opts pat ;; let re_result ?flags s = match re ?flags s with | s -> Ok s | exception Not_supported -> Error `Not_supported | exception Parse_error -> Error `Parse_error ;; let regexp ?flags pat = Re.compile (re ?flags pat) let extract ~rex s = Re.Group.all (Re.exec rex s) let exec ~rex ?pos s = Re.exec rex ?pos s let names rex = Re.group_names rex |> List.map fst |> Array.of_list let get_named_substring_opt rex name s = let rec loop = function | [] -> None | (n, i) :: rem when n = name -> (match Re.Group.get_opt s i with | None -> loop rem | Some _ as s -> s) | _ :: rem -> loop rem in loop (Re.group_names rex) ;; let get_substring_ofs s i = Re.Group.offset s i let pmatch ~rex s = Re.execp rex s let substitute ~rex ~subst str = let b = Buffer.create 1024 in let rec loop pos on_match = if Re.execp ~pos rex str then ( let ss = Re.exec ~pos rex str in let start, fin = Re.Group.offset ss 0 in if on_match && start = pos && start = fin then ( if (* Empty match following a match *) pos < String.length str then ( Buffer.add_char b str.[pos]; loop (pos + 1) false)) else ( let pat = Re.Group.get ss 0 in Buffer.add_substring b str pos (start - pos); Buffer.add_string b (subst pat); if start = fin then ( if (* Manually advance by one after an empty match *) fin < String.length str then ( Buffer.add_char b str.[fin]; loop (fin + 1) false)) else loop fin true)) else Buffer.add_substring b str pos (String.length str - pos) in loop 0 false; Buffer.contents b ;; let split ~rex s = let rec split accu start = if start = String.length s then accu else ( match let g = Re.exec rex s ~pos:start in if Group.stop g 0 = start then Re.exec rex s ~pos:(start + 1) else g with | exception Not_found -> String.sub s start (String.length s - start) :: accu | g -> let next = Group.stop g 0 in split (String.sub s start (Group.start g 0 - start) :: accu) next) in match Re.exec rex s ~pos:0 with | g -> List.rev (if Group.start g 0 = 0 then split [] (Group.stop g 0) else split [ String.sub s 0 (Group.start g 0) ] (Group.stop g 0)) | exception Not_found -> if s = "" then [] else [ s ] ;; (* From PCRE *) let string_unsafe_sub s ofs len = let r = Bytes.create len in Bytes.unsafe_blit s ofs r 0 len; Bytes.unsafe_to_string r ;; let quote s = let len = String.length s in let buf = Bytes.create (len lsl 1) in let pos = ref 0 in for i = 0 to len - 1 do match String.unsafe_get s i with | ('\\' | '^' | '$' | '.' | '[' | '|' | '(' | ')' | '?' | '*' | '+' | '{') as c -> Bytes.unsafe_set buf !pos '\\'; incr pos; Bytes.unsafe_set buf !pos c; incr pos | c -> Bytes.unsafe_set buf !pos c; incr pos done; string_unsafe_sub buf 0 !pos ;; let full_split ?(max = 0) ~rex s = if String.length s = 0 then [] else if max = 1 then [ Text s ] else ( let results = Re.split_full rex s in let matches = List.map (function | `Text s -> [ Text s ] | `Delim d -> let matches = Re.Group.all_offset d in let delim = Re.Group.get d 0 in Delim delim :: (let l = ref [] in for i = 1 to Array.length matches - 1 do l := (if matches.(i) = (-1, -1) then NoGroup else Group (i, Re.Group.get d i)) :: !l done; List.rev !l)) results in List.concat matches) ;; type substrings = Group.t let get_substring s i = Re.Group.get s i let get_named_substring rex name s = match get_named_substring_opt rex name s with | None -> raise Not_found | Some s -> s ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>