Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] (Sorry for my last post) lazyness, exceptions?, ocaml syntax rule-of-thumbs
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] (Sorry for my last post) lazyness, exceptions?, ocaml syntax rule-of-thumbs
> > Another annoying problem (maybe it is in the docs...), but I cannot find
> > a function, which checks, whether a string can match a regexp, and
> > return true or false. Search_forward, again, I think should be return an
> > option, and not trowing an exception.
> 
> See Str.string_match : regexp -> string -> int -> bool

In an attempt to prevent one more round of e-mails on this topic, let
me just add that string_match performs "anchored matching" (matching
the RE at the given location in the string) while search_forward
performs "unanchored matching" (matching at any location).  However,
the latter can be turned into the former by prefixing the regexp with ".*".
Hence, the following are equivalent:

        Str.string_match (Str.regexp ".*\\.html$") filename 0
and
        try
          ignore(Str.search_forward (Str.regexp "\\.html$") filename 0);
          true
        with Not_found ->
          false

This said, a much cleaner solution is

        Filename.check_suffix filename ".html"

Not only it is shorter, but under Windows it will perform
case-insensitive matching, like it should.

- Xavier Leroy
-------------------
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