Browse thread
[Caml-list] How to use pcre
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ 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,
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