From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 125FEBBAF for ; Tue, 7 Dec 2010 16:56:09 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiYBAN7n/UzRVda0kGdsb2JhbACUfYYvAYgMCBUBAQIJCQwHEQQepj+JaoIYhT0uiFYBAQMFhUQEgV6DBIYPhgU X-IronPort-AV: E=Sophos;i="4.59,311,1288566000"; d="scan'208";a="83326407" Received: from mail-iw0-f180.google.com ([209.85.214.180]) by mail2-smtp-roc.national.inria.fr with ESMTP; 07 Dec 2010 16:56:08 +0100 Received: by iwn37 with SMTP id 37so41724iwn.39 for ; Tue, 07 Dec 2010 07:56:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:cc:content-type; bh=/OIB/PvaeLZOZD1pmpRb0sefSAfI4ctg0+QcqzlX3xQ=; b=BAG6lI9qXDn/XuO8Wl66c5aFBoCzQ05rfR777tS2qYh8J2z18dmbavx+ltzo4x0ef8 qjZj6B/NIvyO+h7IujshRv1iSTTjjBDiy8CB+POrvlvMBb9+VwFH4UKZLunUh5vSBn6j shGbRO0SkEFl9vXleFue3PG4dlGVZ1nkV42Bg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=fFkMbtxBCHvLUl7Lh5h9BQO4UzKoTYcRCL9yFrBKuGRzrKM+UTaaUkUJSDXWwrrHDU qT0K6dWnN62xDFiBu79+bKqCBpcF4ZkrgtWXhQSev1383zWWiLEF9JMWAnju76eUAlrV xYQbkLeHqxVBL93qZFzv3NnycL0YQdszI316I= Received: by 10.231.33.74 with SMTP id g10mr939804ibd.117.1291737367311; Tue, 07 Dec 2010 07:56:07 -0800 (PST) MIME-Version: 1.0 Received: by 10.231.11.132 with HTTP; Tue, 7 Dec 2010 07:55:47 -0800 (PST) In-Reply-To: <317197.91134.qm@web65412.mail.ac4.yahoo.com> References: <317197.91134.qm@web65412.mail.ac4.yahoo.com> From: Ashish Agarwal Date: Tue, 7 Dec 2010 10:55:47 -0500 Message-ID: Subject: Re: [Caml-list] HELP : with regular expression To: zaid khalid Cc: caml-list@yquem.inria.fr Content-Type: multipart/alternative; boundary=000325575aa2ebf8180496d40aad X-Spam: no; 0.00; pcre:01 regexp:01 substrings:01 regexp:01 ocaml:01 pcre:01 bool:01 substrings:01 bool:01 pcre-ocaml:01 cheers:01 beginner's:01 ocaml:01 bug:01 pcre-ocaml:01 --000325575aa2ebf8180496d40aad Content-Type: text/plain; charset=ISO-8859-1 I know you're asking for a solution with Str. Since I can't help with that, let me give you the Pcre solution instead. Hopefully my explanations will make up for the lack of examples to help you start using it. pmatch is the function you care about. Ignore most of the arguments. All you need to do is pass the regular expression and the string to match against. The regular expression can be given in either the ~rex or ~pat named arguments (not both). Use ~pat for hacking, and be sure to compile to ~rex if you're doing lots of matches. Notice that almost every function has the same arguments, so once you learn this one the others will make sense too. extract is the second useful function. pmatch just returns true or false if the regexp matches or not. extract will give you all the matching substrings, which is useful to see what your regexp is actually doing (and of course if you need the matched string for later use). After you use these two functions you can start looking at the numerous other ones that let you do more detailed things. The regular expression I came up with for what you want is "(a+|(aba)+)$". Let's test it: $ ocaml Objective Caml version 3.12.0 # #require "pcre";; # open Pcre;; # pmatch ~pat:"(a+|(aba)+)$" "abaaba";; - : bool = true # extract ~pat:"(a+|(aba)+)$" "abaaba";; - : string array = [|"abaaba"; "abaaba"; "aba"|] Note that extract gives as its first result the "full match at index 0" and then all the substrings that match. I'm actually not sure what the use of this is, and I always set full_match to false to avoid the duplication. # pmatch ~pat:"(a+|(aba)+)$" "abaa";; - : bool = true # extract ~full_match:false ~pat:"(a+|(aba)+)$" "abaa";; - : string array = [|"aa"; ""|] # pmatch ~pat:"(a+|(aba)+)$" "abb";; - : bool = false # extract ~full_match:false ~pat:"(a+|(aba)+)$" "abb";; Exception: Not_found. Hope that helps. On Mon, Dec 6, 2010 at 6:29 PM, zaid khalid wrote: > Hi folks > > Thank you for all your replies. I think I am still struggling to find a > solution to my issue using "Str.regexp", and using Pcre-ocaml needs some > time to be familiar with as there is no enough examples and discussion on > it. > > Ill put my issue again as if someone can help me to find a solution to it > with the "Str" . > > I want to define regular expression and after that I want to check if > particular string is a prefix of the given regular expression. > > Example: a* | (aba)* so when you test "abaaba" the result will be true > (complete match) and when we check "abaa" the result is true as well but > when we check "abb" the result is false. > > I look forward to your suggestions. > > Cheers, > Zaid > > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > --000325575aa2ebf8180496d40aad Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I know you're asking for a solution with Str. Since I can't help wi= th that, let me give you the Pcre solution instead. Hopefully my explanatio= ns will make up for the lack of examples to help you start using it.

pmatch is the function you care about. Ignore most of the ar= guments. All you need to do is pass the regular expression and the string t= o match against. The regular expression can be given in either the ~rex or = ~pat named arguments (not both). Use ~pat for hacking, and be sure to compi= le to ~rex if you're doing lots of matches.=A0Notice that almost every = function has the same arguments, so once you learn this one the others will= make sense too.

extract is the second useful function. pmatch just retu= rns true or false if the regexp matches or not. extract will give you all t= he matching substrings, which is useful to see what your regexp is actually= doing (and of course if you need the matched string for later use).

After you use these two functions you can start looking= at the numerous other ones that let you do more detailed things.

The regular expression I came up with for what you want is = "(a+|(aba)+)$". Let's test it:

$ ocaml
=A0=A0 =A0 =A0 =A0Objective Caml= version 3.12.0

# #require "pcre";;=
# open Pcre;;

# pmatch ~pat:= "(a+|(aba)+)$" "abaaba";;
- : bool =3D true

# extract ~pat:"(a+|(a= ba)+)$" "abaaba";;
- : string array =3D [|"ab= aaba"; "abaaba"; "aba"|]

Note that extract gives as its first result the "full match at in= dex 0" and then all the substrings that match. I'm actually not su= re what the use of this is, and I always set full_match to false to avoid t= he duplication.

# pmatch ~pat:"(a+|(aba)+)$" "abaa&= quot;;; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0
- : bool =3D true<= /div>

# extract ~full_match:false ~pat:"= (a+|(aba)+)$" "abaa";;
- : string array =3D [|"aa"; ""|]
=
# pmatch ~pat:"(a+|(aba)+)$" "abb";;
- : bool =3D false

# extract ~full_mat= ch:false ~pat:"(a+|(aba)+)$" "abb";;=A0
Exception: Not_found.

Hope that helps.<= /div>


On Mon, Dec 6, 2010 at 6= :29 PM, zaid khalid <zaidbenaz@yahoo.com> wrote:
Hi folks =

Thank you for all your replies. I think I am still struggling to fi= nd a solution to my issue using "Str.regexp", and using Pcre-ocam= l needs some time to be familiar with as there is no enough examples and di= scussion on it.

Ill put my issue again as if someone can help me to find a solution to = it with the "Str" .

I want to define regular expression an= d after that I want to check if particular string is a prefix of the given = regular expression.

Example: a* | (aba)* so when you test "abaaba" the result wil= l be true (complete match) and when we check "abaa" the result is= true as well but when we check "abb" the result is false.

I look forward to your suggestions.

Cheers,
Zaid


_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list Archives: http://caml.in= ria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


--000325575aa2ebf8180496d40aad--