Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Regular expression library: a poll on features
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pixel <pixel@m...>
Subject: Re: [Caml-list] Regular expression library: a poll on features
Xavier Leroy <xavier.leroy@inria.fr> writes:

(posting on the list cuz of the wishlist below)

> 1- back-references in regexps (e.g. "([a-z]+)\1", meaning any sequence
> of lowercase letters followed by another occurrence of the same sequence);

IMO only usefull in few cases. Could be dropped with no pb.

> 
> 2- partial string matching as per Str.string_partial_match, i.e.
> the ability to recognize that a string is a prefix of a string that
> match a regexp.

quite a surprising feature. I can't imagine an interesting example of
its use.


Here is my wishlist to have regexps easily _usable_
(ie. fill the gap with perl/ruby)

- with compiler support (a la printf's format):

  flags = [ `CASELESS | `MULTILINE | `DOTALL | `EXTENDED | `ANCHORED ]
  m : ?option:flags -> regexp -> string -> string tuple option
  match_all : ?option:flags -> regexp -> string -> string tuple list
  cond_match : ?option:flags -> string -> regexp -> (string tuple -> 'a)
                                       -> regexp -> (string tuple -> 'a)
                                          ...
                                       -> 'a
           (with checking the last regexp is always true unless 'a is unit)
  subst     : string -> regexp -> (string tuple -> string) -> string
  gsubst    : string -> regexp -> (string tuple -> string) -> string
  try_subst : string -> regexp -> (string tuple -> string) -> string option

examples

    match Re.m "(\w+)=(\S+)" s with
    | None -> ...
    | Some(v, val) -> ...
    
    Re.cond_match s
      "^\s*#" (fun() -> None)
      "(\w+)=(\S+)" (fun (v, val) -> Some(v, val))
      "" (fun() -> failwith ("bad line " ^ s))
    
    Re.cond_match s "warning: (.*)" (fun s -> eprintf "a problem occured: %s\n" s)

    Re.subst (Re.subst s "^\s+" (fun() -> "")) "\s+$" (fun() -> "")


- Without compiler support

  flags = [ `CASELESS | `MULTILINE | `DOTALL | `EXTENDED | `ANCHORED ]
  re : string -> regexp
  m : ?option:flags -> regexp -> string -> string list option
  match_all : ?option:flags -> regexp -> string -> string list list
  cond_match : ?option:flags -> string -> (regexp * (string list -> 'a)) list -> 'a
  subst     : string -> regexp -> (string list -> string) -> string
  gsubst    : string -> regexp -> (string list -> string) -> string
  try_subst : string -> regexp -> (string list -> string) -> string option

examples

    match m (re"(\w+)=(\S+)") s with
    | Some [ v ; val ] -> ...
    | _ -> ...
    
    cond_match s [ 
        re"^\s*#", fun() -> None ;
        re"(\w+)=(\S+)", fun (v, val) -> Some(v, val) ;
        re"", fun() -> failwith ("bad line " ^ s) ;
    ]
    
    cond_match s "warning: (.*)" (fun s -> eprintf "a problem occured: %s\n" (hd s))

    subst (subst s (re"^\s+") (fun _ -> "")) (re"\s+$") (fun _ -> "")
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners