caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* help with regular expression
@ 2010-12-06 11:43 zaid khalid
  2010-12-06 12:03 ` [Caml-list] " David Allsopp
  2010-12-06 17:31 ` Dawid Toton
  0 siblings, 2 replies; 5+ messages in thread
From: zaid khalid @ 2010-12-06 11:43 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 521 bytes --]

Hi Folks

I want some help in writing regular expressions in Ocaml, as I know how to write it in informal way but in Ocaml syntax I can not. For example I want to write "a* | (aba)* ".

Another question if I want the string to be matched against the regular expression to be matched as whole string not as substring what symbol I need to attach to the substring, i.e if I want only concrete strings accepted (like (" ", a , aa , aaa, aba, abaaba), but not ab or not abaa).


Hint I am using (Str.regexp)
Thanks




      

[-- Attachment #2: Type: text/html, Size: 667 bytes --]

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

* RE: [Caml-list] help with regular expression
  2010-12-06 11:43 help with regular expression zaid khalid
@ 2010-12-06 12:03 ` David Allsopp
  2010-12-06 13:11   ` Sylvain Le Gall
  2010-12-06 17:31 ` Dawid Toton
  1 sibling, 1 reply; 5+ messages in thread
From: David Allsopp @ 2010-12-06 12:03 UTC (permalink / raw)
  To: zaid khalid, caml-list

zaid Khalid wrote:
> Hi Folks
>
> I want some help in writing regular expressions in Ocaml, as I know how to write it
> in informal way but in Ocaml syntax I can not. For example I want to write "a* | (aba)* ".

This question would better be posted on the beginners' list - http://caml.inria.fr/resources/forums.en.html#id2267683

Regular Expressions can be done using the Standard Library with the Str module (as you've found) - see http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html so your expression above (assuming you have loaded/linked str.cm[x]a) is Str.regexp "a*\\|\\(aba\\)*". The language of regexps is given in the docs for Str.regexp function. Remember to escape backslash characters as the regular expression is given in an OCaml string (so to escape a backslash in your regexp you have to write "\\\\").

> Another question if I want the string to be matched against the regular expression
> to be matched as whole string not as substring what symbol I need to attach to the
> substring, i.e if I want only concrete strings accepted (like (" ", a , aa , aaa, 
> aba, abaaba), but not ab or not abaa).

Use ^ and $ at the beginning and end of your regexp to ensure that it matches the entire string only - "^\\(a*\\|\\(aba\\)*\\)$"

> Hint I am using (Str.regexp)

There are other libraries (e.g. pcre-ocaml) which provide different (I would say more powerful, rather than strictly better!) implementations.


David


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

* Re: help with regular expression
  2010-12-06 12:03 ` [Caml-list] " David Allsopp
@ 2010-12-06 13:11   ` Sylvain Le Gall
  2010-12-06 20:41     ` [Caml-list] " Martin Jambon
  0 siblings, 1 reply; 5+ messages in thread
From: Sylvain Le Gall @ 2010-12-06 13:11 UTC (permalink / raw)
  To: caml-list

On 06-12-2010, David Allsopp <dra-news@metastack.com> wrote:
> zaid Khalid wrote:
>>
>
>> Hint I am using (Str.regexp)
>
> There are other libraries (e.g. pcre-ocaml) which provide different (I
> would say more powerful, rather than strictly better!)
> implementations.
>
>

There is also syntax extension like mikmatch, that helps to write regexp
in a very meaningful syntax:

match str with 
| RE bol "a"* | "ab"* eol ->
  true
| _ ->
  false

http://martin.jambon.free.fr/mikmatch-manual.html
http://martin.jambon.free.fr/mikmatch.html

You can use pcre and str with mikmatch.

Regards,
Sylvain Le Gall


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

* Re: help with regular expression
  2010-12-06 11:43 help with regular expression zaid khalid
  2010-12-06 12:03 ` [Caml-list] " David Allsopp
@ 2010-12-06 17:31 ` Dawid Toton
  1 sibling, 0 replies; 5+ messages in thread
From: Dawid Toton @ 2010-12-06 17:31 UTC (permalink / raw)
  To: caml-list

On 12/06/2010 12:43 PM, zaid khalid wrote:
 > I want some help in writing regular expressions in Ocaml, as I know 
how to write it in informal way but in Ocaml syntax I can not. For 
example I want to write "a* | (aba)* ".
 >
 > Another question if I want the string to be matched against the 
regular expression to be matched as whole string not as substring what 
symbol I need to attach to the substring, i.e if I want only concrete 
strings accepted (like (" ", a , aa , aaa, aba, abaaba), but not ab or 
not abaa).
 >

I also had problems with Str (regexp descriptions being unreadable, 
error-prone and hard to generate dynamically) and decided just to stop 
using Str.
I have a tiny module [1] made with clarity in mind. It is pure OCaml. It 
defines operators like $$ to be used in regexp construction. This way 
syntax of the expressions is checked at compile time. Also, it is 
trivial to build them at run time.
The whole "engine" is contained in a relatively short function 
HRegex.subwords_of_subexpressions, so I believe anybody can hack it 
without much effort.

I haven't measured performance of this implementation. I expect it to be 
slow when processing long strings. It's just OK for my needs so far. 
Anyway, the important part is the module interface. It expresses my 
point of view on this topic.

The code is available in a mercurial repository [2].

The exemple "a* | (aba)* " would become:

open HRegex.Operators

let rx = (!* !$ "a") +$ (!* !$ "aba")

Dawid

[1] 
http://hg.ocamlcore.org/cgi-bin/hgwebdir.cgi/hlibrary/hlibrary/raw-file/tip/HRegex.mli
[2] http://hg.ocamlcore.org/cgi-bin/hgwebdir.cgi/hlibrary/hlibrary


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

* Re: [Caml-list] Re: help with regular expression
  2010-12-06 13:11   ` Sylvain Le Gall
@ 2010-12-06 20:41     ` Martin Jambon
  0 siblings, 0 replies; 5+ messages in thread
From: Martin Jambon @ 2010-12-06 20:41 UTC (permalink / raw)
  To: caml-list

On 12/06/10 05:11, Sylvain Le Gall wrote:
> On 06-12-2010, David Allsopp <dra-news@metastack.com> wrote:
>> zaid Khalid wrote:
>>>
>>
>>> Hint I am using (Str.regexp)
>>
>> There are other libraries (e.g. pcre-ocaml) which provide different (I
>> would say more powerful, rather than strictly better!)
>> implementations.
>>
>>
> 
> There is also syntax extension like mikmatch, that helps to write regexp
> in a very meaningful syntax:
> 
> match str with 
> | RE bol "a"* | "ab"* eol ->
>   true
> | _ ->
>   false

If I understand correctly the original problem, the solution is:

match str with
  | RE ("a"* | "aba"*) eos ->
      (* matches always the beginning of the string,
         eos enforces a match at the end of the string,
         and the vertical bar has the lowest priority
         and so parentheses are needed. *)
      true
  | _ ->
      false


> http://martin.jambon.free.fr/mikmatch-manual.html
> http://martin.jambon.free.fr/mikmatch.html
> 
> You can use pcre and str with mikmatch.

I would recommend the pcre variant mostly for one feature that is not
provided by str:  lazy quantifiers, i.e. "repeat as little as possible
before trying to match what comes next".


Martin


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

end of thread, other threads:[~2010-12-06 20:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-06 11:43 help with regular expression zaid khalid
2010-12-06 12:03 ` [Caml-list] " David Allsopp
2010-12-06 13:11   ` Sylvain Le Gall
2010-12-06 20:41     ` [Caml-list] " Martin Jambon
2010-12-06 17:31 ` Dawid Toton

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