English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] How to use pcre
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-09-06 (13:30)
From: Markus Mottl <markus@m...>
Subject: Re: [Caml-list] How to use pcre
On Thu, 06 Sep 2001, Johann Spies wrote:
> How do I change a string (let s = "abcde";;) to "abcDe"?
> In other words what would be the pcre equivalent of python's 
> >>> t = re.sub('d','D',s)
> >>> t

You could use this:

  let t = Pcre.replace ~pat:"d" ~templ:"D" s;;

You may also use "qreplace" in place of "replace", because the template
does not contain any special patterns that might need translation.

This example is actually far too simple to be solved with the Pcre. It
does not even make use of regular expressions or substitution patterns.
Some kind of map-function for strings would be more appropriate here,

  let map f str =
    let len = String.length str in
    let res = String.create len in
    for i = 0 to len - 1 do res.[i] <- f str.[i] done;

  let s = "abcde";;
  let res = map (function 'd' -> 'D' | c -> c) s;;

Markus Mottl

Markus Mottl                                             markus@oefai.at
Austrian Research Institute
for Artificial Intelligence                  http://www.oefai.at/~markus
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr