From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id PAA08637; Thu, 6 Sep 2001 15:30:16 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id PAA08545 for ; Thu, 6 Sep 2001 15:30:15 +0200 (MET DST) Received: from chopin.ai.univie.ac.at (chopin.ai.univie.ac.at [131.130.174.170]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f86DUDL03905 for ; Thu, 6 Sep 2001 15:30:13 +0200 (MET DST) Received: (from markus@localhost) by chopin.ai.univie.ac.at (8.9.3/8.9.3/Debian 8.9.3-21) id PAA26540; Thu, 6 Sep 2001 15:29:58 +0200 Date: Thu, 6 Sep 2001 15:29:58 +0200 From: Markus Mottl To: Johann Spies Cc: ocaml mailing list Subject: Re: [Caml-list] How to use pcre Message-ID: <20010906152958.B24260@chopin.ai.univie.ac.at> References: <87pu94lbdt.fsf@bywoner.sun.ac.za> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <87pu94lbdt.fsf@bywoner.sun.ac.za>; from jspies@maties.sun.ac.za on Thu, Sep 06, 2001 at 15:03:26 +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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, e.g.: 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; res;; let s = "abcde";; let res = map (function 'd' -> 'D' | c -> c) s;; Regards, 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