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] 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: -- (:)
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                                   
Austrian Research Institute
for Artificial Intelligence        
Bug reports:  FAQ:
To unsubscribe, mail  Archives: