Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2002-09-27 (08:14)
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
          ignore(Str.search_forward (Str.regexp "\\.html$") filename 0);
        with Not_found ->

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 Archives:
Bug reports: FAQ:
Beginner's list: