caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to use pcre
@ 2001-09-06 13:03 Johann Spies
  2001-09-06 13:29 ` Markus Mottl
  0 siblings, 1 reply; 12+ messages in thread
From: Johann Spies @ 2001-09-06 13:03 UTC (permalink / raw)
  To: ocaml mailing list

I have had very limited exposure to Perl and would like to use pcre
after reading much about it on this list.

I have looked in the documentation that comes with it, but can not
find simple examples on how to use it.  The example programs are very
impressive, but do not help me a lot to know how to use simple things
like string substitution.

Can somebody please give me a simple example on how to do the following:

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
'abcDe'

Regards.

Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Behold, I stand at the door, and knock; if any man 
      hear my voice, and open the door, I will come in to 
      him, and will sup with him, and he with me."       
                                   Revelation 3:20 
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre
  2001-09-06 13:03 [Caml-list] How to use pcre Johann Spies
@ 2001-09-06 13:29 ` Markus Mottl
  2001-09-06 13:44   ` Johann Spies
  2001-09-06 14:21   ` [Caml-list] How to use pcre (2) Johann Spies
  0 siblings, 2 replies; 12+ messages in thread
From: Markus Mottl @ 2001-09-06 13:29 UTC (permalink / raw)
  To: Johann Spies; +Cc: ocaml mailing list

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre
  2001-09-06 13:29 ` Markus Mottl
@ 2001-09-06 13:44   ` Johann Spies
  2001-09-06 14:21   ` [Caml-list] How to use pcre (2) Johann Spies
  1 sibling, 0 replies; 12+ messages in thread
From: Johann Spies @ 2001-09-06 13:44 UTC (permalink / raw)
  To: ocaml mailing list


Markus Mottl <markus@mail4.ai.univie.ac.at> writes:

> You could use this:
> 
>   let t = Pcre.replace ~pat:"d" ~templ:"D" s;;
...

Thanks Markus.

> 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.:

OK.  But this was just an example.  I wanted to get the usage of
Pcre.replace. Your map-function is fine as long as the replacement and
the pattern has the same length.

I suppose one can write an map function for a case where there
replacement has a different length, but why should I if I can use
pcre?  Maybe I will try some day to get some more insight into ocaml;)

Regards.

Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Behold, I stand at the door, and knock; if any man 
      hear my voice, and open the door, I will come in to 
      him, and will sup with him, and he with me."       
                                   Revelation 3:20 
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (2)
  2001-09-06 13:29 ` Markus Mottl
  2001-09-06 13:44   ` Johann Spies
@ 2001-09-06 14:21   ` Johann Spies
  2001-09-06 14:45     ` Markus Mottl
  1 sibling, 1 reply; 12+ messages in thread
From: Johann Spies @ 2001-09-06 14:21 UTC (permalink / raw)
  To: ocaml mailing list

I suspect there is something wrong with my installation.  I have
compiled pcre in /usr/local/src and put the following files in
/usr/local/lib/ocaml:

libpcre.a
pcre.a
pcre.cma
pcre.cmi
pcre.cmxa
pcre.mli

Working on Debian Linux, that did not work.  So I symlinked all those
files to /usr/lib/ocaml.  Then I could open pcre in the toplevel.  But
there is something else wrong here.

Trying Markus' solution I came across the following new problem
regarding pcre both in the toplevel and when I try to compile it:

-------------------------
$ ocaml
        Objective Caml version 3.01

# let s = "abcde";;
let s = "abcde";;
val s : string = "abcde"
# let t = Pcre.replace ~pat:"d" ~templ:"D" s;;
let t = Pcre.replace ~pat:"d" ~templ:"D" s;;
Reference to undefined global `Pcre'

---------------------
$ ocaml
        Objective Caml version 3.01

# open Pcre;;
open Pcre;;
# let s = "abcde";;
let s = "abcde";;
val s : string = "abcde"
#   let t = replace ~pat:"d" ~templ:"D" s;;
  let t = replace ~pat:"d" ~templ:"D" s;;
Reference to undefined global `Pcre'
----------------------------------

I tried to compile the file in which I want to use this type of
function with

ocamlfind ocamlopt -o v1 -package postgres,threads -thread -linkpkg
v3.ml
-------------
No implementations provided for the following modules:
  Pcre referenced from v3.cmx
------------
and also with 
ocamlfind ocamlopt -o v1 -package postgres,pcre,threads -thread
-linkpkg v3.ml
-----------
ocamlfind: package 'pcre' not found
------------
There was no META file in the pcre-sources. How do I go forward from
here? 


Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "Behold, I stand at the door, and knock; if any man 
      hear my voice, and open the door, I will come in to 
      him, and will sup with him, and he with me."       
                                   Revelation 3:20 
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (2)
  2001-09-06 14:21   ` [Caml-list] How to use pcre (2) Johann Spies
@ 2001-09-06 14:45     ` Markus Mottl
  2001-09-06 19:51       ` [Caml-list] How to use pcre (3) Johann Spies
  0 siblings, 1 reply; 12+ messages in thread
From: Markus Mottl @ 2001-09-06 14:45 UTC (permalink / raw)
  To: Johann Spies; +Cc: ocaml mailing list

On Thu, 06 Sep 2001, Johann Spies wrote:
> I suspect there is something wrong with my installation.  I have
> compiled pcre in /usr/local/src and put the following files in
> /usr/local/lib/ocaml:

This is an FAQ and not related to the Pcre in particular: the problem
is that

  a) you have to build a "customized" toplevel, because a C-library
     has to be linked in. This is very easy:

       ocamlmktop -I {dir with Pcre} pcre.cma -o mytoplevel

  b) "mytoplevel" doesn't "see" the interface file "pcre.cmi" if you don't
     tell it the include path. Therefore launch it as follows:

       mytoplevel -I {dir with Pcre}

If you do this all, things should work fine.

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 14:45     ` Markus Mottl
@ 2001-09-06 19:51       ` Johann Spies
  2001-09-06 21:12         ` Nicolas George
                           ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Johann Spies @ 2001-09-06 19:51 UTC (permalink / raw)
  To: ocaml

Markus Mottl <markus@mail4.ai.univie.ac.at> writes:

> This is an FAQ and not related to the Pcre in particular: the problem
> is that

Maybe a FAQ, but difficult for newbies to know when and how.  

> 
>   a) you have to build a "customized" toplevel, because a C-library
>      has to be linked in. This is very easy:

After a bit of experimentation I got this to work.

After about another hour and a half I did not succeed in compiling the
following program:

-------------------------
let s = "abcde";;
let t = Pcre.replace ~pat:"c" ~templ:" Hier gebeur 'n ding " s;;
print_string t;;
print_newline();;
--------------------------

I have read the man page for ocamlopt as well as the ocaml manual and
still do not know how to compile it!?  Maybe I am just dumb.

My latest effort was:

$ocamlfind ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt
-L/usr/lib/ocaml/contrib  -cclib -llibpcre.a -linkall /tmp/rex.ml

No implementations provided for the following modules:
  Pcre referenced from /tmp/rex.cmx

The usage of ocamlfind made no difference.


Johann
-- 
J.H. Spies - Tel. 082 782 0336 / 021-808 4036(w)  
             Posbus 4668, Tygervallei 7536
     "Behold, I stand at the door, and knock; if any man 
      hear my voice, and open the door, I will come in to 
      him, and will sup with him, and he with me."       
                                   Revelation 3:20 
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 19:51       ` [Caml-list] How to use pcre (3) Johann Spies
@ 2001-09-06 21:12         ` Nicolas George
  2001-09-06 21:23         ` Markus Mottl
  2001-09-06 21:28         ` Gerd Stolpmann
  2 siblings, 0 replies; 12+ messages in thread
From: Nicolas George @ 2001-09-06 21:12 UTC (permalink / raw)
  To: Johann Spies; +Cc: ocaml

Le jeudi 06 septembre 2001 à 21:51, Johann Spies a écrit :
> $ocamlfind ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt
> -L/usr/lib/ocaml/contrib  -cclib -llibpcre.a -linkall /tmp/rex.ml
>
> No implementations provided for the following modules:
>   Pcre referenced from /tmp/rex.cmx

You provide code for the C-OCaml binding, but not the OCaml functions of
Pcre. You should try something like that:

ocamlopt -o rex -I +contrib pcre.cmxa /tmp/rex.ml

The archives (cma and cmxa) il the latest versions of OCaml, record the
compiler options needed. And the +contrib is a shortcut for
/path/of/ocaml/distribution/contrib.
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 19:51       ` [Caml-list] How to use pcre (3) Johann Spies
  2001-09-06 21:12         ` Nicolas George
@ 2001-09-06 21:23         ` Markus Mottl
  2001-09-07  6:59           ` Johann Spies
  2001-09-06 21:28         ` Gerd Stolpmann
  2 siblings, 1 reply; 12+ messages in thread
From: Markus Mottl @ 2001-09-06 21:23 UTC (permalink / raw)
  To: Johann Spies; +Cc: ocaml

On Thu, 06 Sep 2001, Johann Spies wrote:
> After about another hour and a half I did not succeed in compiling the
> following program:
[snip]
> My latest effort was:
> 
> $ocamlfind ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt
> -L/usr/lib/ocaml/contrib  -cclib -llibpcre.a -linkall /tmp/rex.ml

The problem is that you have to add pcre.cmxa (or pcre.cma for byte code)
before your /tmp/rex.ml. It's not enough to link against the C-library,
or better: you don't even have to do this.

This should suffice:

  ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt \
    -L/usr/lib/ocaml/contrib pcre.cmxa /tmp/rex.ml

I don't know ocamlfind, because I usually use OcamlMakefile for just
about anything. If you want to try the latter, you'd write a Makefile
that might look like this:

---------------------------------------------------------------------------
SOURCES = rex.ml
RESULT = rex
LIBS = pcre
-include OcamlMakefile
---------------------------------------------------------------------------

You'd only have to set an environment variable (e.g. either in
the Makefile itself or globally in you .bashrc or similar) named
OCAML_DEFAULT_DIRS, which can be any number of paths that contain your
favourite OCaml-libraries.

I usually define a shell alias that just copies a suitable template of
such a Makefile into the current directory so that I can experiment fast.
OcamlMakefile is somewhere in my home directory and is automatically
loaded if you start make as "make -I {path to generic Makefiles}". The
latter can, again, be aliased.

Once I find a bit more time, I'll explain such small (but
useful!) development speedups on my upcoming OCaml hint page...

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 19:51       ` [Caml-list] How to use pcre (3) Johann Spies
  2001-09-06 21:12         ` Nicolas George
  2001-09-06 21:23         ` Markus Mottl
@ 2001-09-06 21:28         ` Gerd Stolpmann
  2001-09-06 21:55           ` Markus Mottl
  2001-09-06 22:14           ` Alain Frisch
  2 siblings, 2 replies; 12+ messages in thread
From: Gerd Stolpmann @ 2001-09-06 21:28 UTC (permalink / raw)
  To: ocaml

On Thu, 06 Sep 2001, Johann Spies wrote:
>Markus Mottl <markus@mail4.ai.univie.ac.at> writes:
>
>> This is an FAQ and not related to the Pcre in particular: the problem
>> is that
>
>Maybe a FAQ, but difficult for newbies to know when and how.  
>
>> 
>>   a) you have to build a "customized" toplevel, because a C-library
>>      has to be linked in. This is very easy:
>
>After a bit of experimentation I got this to work.
>
>After about another hour and a half I did not succeed in compiling the
>following program:
>
>-------------------------
>let s = "abcde";;
>let t = Pcre.replace ~pat:"c" ~templ:" Hier gebeur 'n ding " s;;
>print_string t;;
>print_newline();;
>--------------------------
>
>I have read the man page for ocamlopt as well as the ocaml manual and
>still do not know how to compile it!?  Maybe I am just dumb.
>
>My latest effort was:
>
>$ocamlfind ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt
>-L/usr/lib/ocaml/contrib  -cclib -llibpcre.a -linkall /tmp/rex.ml
>
>No implementations provided for the following modules:
>  Pcre referenced from /tmp/rex.cmx
>
>The usage of ocamlfind made no difference.

ocamlopt -o rex -I /usr/lib/ocaml/contrib pcre.cmxa /tmp/rex.ml

SHOULD suffice. Perhaps you need the -ccopt and -cclib options, too, that
depends on how clever pcre is installed (can't remember) (oh, and it's -lpcre,
not -llibpcre.a, if you need it).

You seem to be confused because there are two .a files, arent't you:
libpcre.a and pcre.a. Actually, both are needed in the final executable, i.e.
both contain necessary machine code. Fortutately, there is also the .cmxa file
which only contains management information, and no machine code, and if you
put it onto the command line the compiler will always be able to find the
pcre.a file at least, and if you are lucky, it will also find libpcre.a. (The
latter works only if pcre is installed with "automatic linking", I don't know
whether the current release supports it.) pcre.a contains compiled ocaml code,
and libpcre.a contains compiled C code.

ocamlfind makes only a difference if the pcre installation contains a so-called
META file, and in this case you could do: ocamlfind ocamlopt -o rex -package
pcre -linkpkg /tmp/rex.ml. That's for people who like it comfortable (here in
Germany we say "Warmduscher", don't know how to translate it).

Gerd
-- 
----------------------------------------------------------------------------
Gerd Stolpmann      Telefon: +49 6151 997705 (privat)
Viktoriastr. 45             
64293 Darmstadt     EMail:   gerd@gerd-stolpmann.de
Germany                     
----------------------------------------------------------------------------
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 21:28         ` Gerd Stolpmann
@ 2001-09-06 21:55           ` Markus Mottl
  2001-09-06 22:14           ` Alain Frisch
  1 sibling, 0 replies; 12+ messages in thread
From: Markus Mottl @ 2001-09-06 21:55 UTC (permalink / raw)
  To: Gerd Stolpmann; +Cc: ocaml

Gerd Stolpmann schrieb am Donnerstag, den 06. September 2001:
> ocamlopt -o rex -I /usr/lib/ocaml/contrib pcre.cmxa /tmp/rex.ml
> 
> SHOULD suffice. Perhaps you need the -ccopt and -cclib options, too, that
> depends on how clever pcre is installed (can't remember) (oh, and it's -lpcre,
> not -llibpcre.a, if you need it).

The -I option suffices: OcamlMakefile, which is used in the
Pcre-distribution, always (by default) compiles libraries in such a way
so as to allow "automatic linking".

> ocamlfind makes only a difference if the pcre installation contains a so-called
> META file, and in this case you could do: ocamlfind ocamlopt -o rex -package
> pcre -linkpkg /tmp/rex.ml. That's for people who like it comfortable (here in
> Germany we say "Warmduscher", don't know how to translate it).

Well, I wouldn't recommend OcamlMakefile to people who prefer cold
showers either... ;)

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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 21:28         ` Gerd Stolpmann
  2001-09-06 21:55           ` Markus Mottl
@ 2001-09-06 22:14           ` Alain Frisch
  1 sibling, 0 replies; 12+ messages in thread
From: Alain Frisch @ 2001-09-06 22:14 UTC (permalink / raw)
  To: Gerd Stolpmann; +Cc: ocaml

On Thu, 6 Sep 2001, Gerd Stolpmann wrote:

> ocamlfind makes only a difference if the pcre installation contains a so-called
> META file

This is not the case for the pcre distribution, but it is easy to add this
file after the installation. On the system where I'm in charge of
installing ocaml and related software for many users, I find it
comfortable to findlib-ize all the packages. For instance, a META file
for Pcre is:

version = "4.2.7"
requires = ""
archive(byte) = "pcre.cma"
archive(native) = "pcre.cmxa"

(I do the same for instance for lablgtk)

Of course, it would be easier if all the packages were findlib compliant 
(it's usually strightforward to add the installation/desinstallation
rules and the META file) ...

-- 
  Alain Frisch

-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] How to use pcre (3)
  2001-09-06 21:23         ` Markus Mottl
@ 2001-09-07  6:59           ` Johann Spies
  0 siblings, 0 replies; 12+ messages in thread
From: Johann Spies @ 2001-09-07  6:59 UTC (permalink / raw)
  To: ocaml mailing list

Markus Mottl <markus@mail4.ai.univie.ac.at> writes:

> The problem is that you have to add pcre.cmxa (or pcre.cma for byte code)
> before your /tmp/rex.ml. It's not enough to link against the C-library,
> or better: you don't even have to do this.
> 
> This should suffice:
> 
>   ocamlopt -o rex -I /usr/lib/ocaml/contrib -ccopt \
>     -L/usr/lib/ocaml/contrib pcre.cmxa /tmp/rex.ml
> 
> I don't know ocamlfind, because I usually use OcamlMakefile for just

As with my previous questions to this list I have learnt a lot.  This
time about compilation, OcamlMakefile and Ocamlfind's META.  Thanks to
Marcus, Gerd, Alain and Nicolais for their answers.


Johann
-- 
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "For whosoever shall call upon the name of the Lord 
      shall be saved."         Romans 10:13 
-------------------
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


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2001-09-07  6:59 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-06 13:03 [Caml-list] How to use pcre Johann Spies
2001-09-06 13:29 ` Markus Mottl
2001-09-06 13:44   ` Johann Spies
2001-09-06 14:21   ` [Caml-list] How to use pcre (2) Johann Spies
2001-09-06 14:45     ` Markus Mottl
2001-09-06 19:51       ` [Caml-list] How to use pcre (3) Johann Spies
2001-09-06 21:12         ` Nicolas George
2001-09-06 21:23         ` Markus Mottl
2001-09-07  6:59           ` Johann Spies
2001-09-06 21:28         ` Gerd Stolpmann
2001-09-06 21:55           ` Markus Mottl
2001-09-06 22:14           ` Alain Frisch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).