Version française
Home     About     Download     Resources     Contact us    
Browse thread
HELP : with regular expression
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Ashish Agarwal <agarwal1975@g...>
Subject: Re: [Caml-list] HELP : with regular expression
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
>
>