How can I match strings using "match with" and regex in OCaml?

My OCaml.ml code is as follows:

open Str

let idregex = Str.regexp ['a'-'z' 'A'-'Z']+ ['a'-'z' 'A'-'Z' '0'-'9' '_']*;

let evalT (x,y) = (match x with 
    Str.regexp "Id(" (idregex as var) ")" -> (x,y)

Why does the above code not work? How can I make it work?

EDIT:

I do not need to understand much. So I want it to stay in the OCaml.ml file, and not in the OCamllex file

+4
source share
1 answer

The keyword matchworks with OCaml templates. The regular expression is not an OCaml pattern, it is a different type of pattern, so you are not using them match.

In the same Strmodule with the function regexpare the corresponding functions.

, ocamllex, , ( , ) idregex OCaml .

, , Str.

$ ocaml
        OCaml version 4.01.0

# #load "str.cma";;
# let idregex = Str.regexp "[a-zA-Z]+[a-zA-Z0-9_]*";;
val idregex : Str.regexp = <abstr>
# Str.string_match idregex "a_32" 0;;
- : bool = true
# Str.string_match idregex "32" 0;;
- : bool = false

, OCaml. OCaml ocamllex. micmatch. , OCaml ( ), - micmatch.

+4

All Articles