3 ms·
Here is (I think) the example regex ported to OCaml's Re library [1] let my_regex = let open Re in seq [ bos; opt (str "0x"
by pmahoney 6y ago
Here is (I think) the example regex ported to OCaml's Re library [1]
let my_regex =
let open Re in
seq [
bos;
opt (str "0x");
repn (
alt [
rg 'A' 'F';
rg 'a' 'f';
rg '0' '9';
]
) 4 None |> group;
eos;
]
|> compile
I'm familiar with standard (compact) regex syntax, but I've been using the above syntax recently in a couple small places. I'm a bit on the fence as to which is "better". The compact syntax is, of course, more compact. I think it's a very similar comparison between APL (which I've not used) and most other common programming languages.
One advantage of the expanded syntax is that it's a bit nicer to incorporate a string variable, e.g. "str some_string" vs. "/#{Regexp.escape(some_string)}/" (to borrow Ruby's syntax).
[1] https://github.com/ocaml/ocaml-re https://github.com/ocaml/ocaml-re