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 mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 351FDBBAF for ; Mon, 6 Dec 2010 13:07:03 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AusBAPtg/ExRZ90wkWdsb2JhbACjMhUBAQIJCwoHEQMfvHSFSQSKbg X-IronPort-AV: E=Sophos;i="4.59,305,1288566000"; d="scan'208";a="69955521" Received: from mtaout02-winn.ispmail.ntl.com ([81.103.221.48]) by mail3-smtp-sop.national.inria.fr with ESMTP; 06 Dec 2010 13:07:02 +0100 Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20101206120701.SPLH19887.mtaout02-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com>; Mon, 6 Dec 2010 12:07:01 +0000 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout02-winn.ispmail.ntl.com (InterMail vG.3.00.04.00 201-2196-133-20080908) with ESMTP id <20101206120701.GNJQ25842.aamtaout02-winn.ispmail.ntl.com@romulus.metastack.com>; Mon, 6 Dec 2010 12:07:01 +0000 Received: from remus.metastack.local ([172.16.0.1]) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id oB6C6vEM010931 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=FAIL); Mon, 6 Dec 2010 12:06:58 GMT Received: from Remus.metastack.local ([fe80::547c:3c42:e1da:eda2]) by Remus.metastack.local ([fe80::547c:3c42:e1da:eda2%11]) with mapi; Mon, 6 Dec 2010 12:03:57 +0000 From: David Allsopp To: zaid khalid , "caml-list@yquem.inria.fr" Subject: RE: [Caml-list] help with regular expression Thread-Topic: [Caml-list] help with regular expression Thread-Index: AQHLlTtSMc1yQK4fqUey/9ZvZWrfH5OTThYw Date: Mon, 6 Dec 2010 12:03:54 +0000 Message-ID: References: <926418.88102.qm@web65410.mail.ac4.yahoo.com> In-Reply-To: <926418.88102.qm@web65410.mail.ac4.yahoo.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.1 cv=X0sWjjQ37bMP4yB/pNNinY3VxVB2n/hmdAjhihaCFGs= c=1 sm=0 a=nfW6s3x8lzwA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=ZOzjf2MOAAAA:8 a=q8F3sgw9YDLZ5C14yRgA:9 a=dXErQhd6s5G5yOObDpFI2hhwSnEA:4 a=CjuIK1q_8ugA:10 a=Fsjrsfmab7wA:10 a=JZDCaEPg8J0A:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 X-Spam: no; 0.00; ocaml:01 ocaml:01 syntax:01 beginners':01 libref:01 regexp:01 regexps:01 regexp:01 backslash:01 backslash:01 pcre-ocaml:01 aba:98 aba:98 wrote:01 matched:01 zaid Khalid wrote: > Hi Folks > > I want some help in writing regular expressions in Ocaml, as I know how t= o write it > in informal way but in Ocaml syntax I can not. For example I want to writ= e "a* | (aba)* ". This question would better be posted on the beginners' list - http://caml.i= nria.fr/resources/forums.en.html#id2267683 Regular Expressions can be done using the Standard Library with the Str mod= ule (as you've found) - see http://caml.inria.fr/pub/docs/manual-ocaml/libr= ef/Str.html so your expression above (assuming you have loaded/linked str.c= m[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 backs= lash in your regexp you have to write "\\\\"). > Another question if I want the string to be matched against the regular e= xpression > to be matched as whole string not as substring what symbol I need to atta= ch to the > substring, i.e if I want only concrete strings accepted (like (" ", a , a= a , aaa,=20 > aba, abaaba), but not ab or not abaa). Use ^ and $ at the beginning and end of your regexp to ensure that it match= es the entire string only - "^\\(a*\\|\\(aba\\)*\\)$" > Hint I am using (Str.regexp) There are other libraries (e.g. pcre-ocaml) which provide different (I woul= d say more powerful, rather than strictly better!) implementations. David