caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ashish Agarwal <agarwal1975@gmail.com>
To: zaid khalid <zaidbenaz@yahoo.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] HELP : with regular expression
Date: Tue, 7 Dec 2010 10:55:47 -0500	[thread overview]
Message-ID: <AANLkTimpkXgKGABaxqgUMMkBSFrWVW2V0SyNBPRwWr2H@mail.gmail.com> (raw)
In-Reply-To: <317197.91134.qm@web65412.mail.ac4.yahoo.com>

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

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 <zaidbenaz@yahoo.com> 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
>
>

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

  reply	other threads:[~2010-12-07 15:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-06 23:29 zaid khalid
2010-12-07 15:55 ` Ashish Agarwal [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-12-06 11:43 help " zaid khalid
2010-12-06 12:03 ` [Caml-list] " David Allsopp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTimpkXgKGABaxqgUMMkBSFrWVW2V0SyNBPRwWr2H@mail.gmail.com \
    --to=agarwal1975@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=zaidbenaz@yahoo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).