2010-07-10 19 views
17

Desidero utilizzare espressioni regolari in OCaml e sembra che il modulo Str fornisca queste funzionalità.Espressioni regolari in OCaml

Così ho provato con un semplice programma:

open Str 
let regx = regexp "." 

ma mi dà il seguente errore

File "lol.ml", line 1, characters 0-1: Error: Error while linking lol.cmo: Reference to undefined global `Str'

Come se il modulo non è presente, ma se tolgo open Str si dice che regexp è un valore non associato.

Non capisco che tipo di problema si tratta, Str dovrebbe essere un modulo standard (secondo http://caml.inria.fr/pub/docs/old-311/libref/Str.html) quindi sono incompetenti .. l'unica cosa che ho pensato è che la firma (mli) è presente ma l'attuazione (ml) non lo è.

Sono in esecuzione Objective Caml version 3.11.0 in base allo strumento ocaml.

Qualcuno può aiutarmi a capirlo? Grazie in anticipo

+3

Se si può evitare, è * veramente * don 'Voglio usare il modulo 'Str'. La sua API non è rientranti, portando a tutti i tipi di problemi. Fortunatamente, PCRE ha buoni collegamenti OCaml su: http://www.ocaml.info/home/ocaml_sources.html Si potrebbe anche voler controllare Mikmatch per il supporto della sintassi sul motore PCRE. –

risposta

25

Da the manual:

Programs that use the str library must be linked as follows:

ocamlc other options str.cma other files 
ocamlopt other options str.cmxa other files 
+1

Grazie, non sapevo che problema! – Jack

16

Oppure si può mettere

#load "str.cma";; 

se si sta facendo l'interprete

+0

Grazie, questo sarà utile durante il tentativo da troupvel – Jack

+0

hi @newacct, puoi spiegare perché l'ocamllex mostra un errore quando ho messo quella linea in prima linea nel mio lexer.mll? l'errore è: riferimento a "Str" globale non definito .... –

1

In alternativa al modulo Str c'è anche Re2.

  1. installarlo utilizzando opam install re2
  2. Usa il modulo in your_file.ml come questo:

    open Re2.Std 
    open Re2.Infix 
    let change input_text = Re2.rewrite_exn ~/"change this" "to that" input_text 
    let() = printf "%s" (change "change this") 
    
  3. Compila con ocamlbuild -use-ocamlfind -package re2 -package core -tag thread your_file.byte