From owner-freebsd-questions@FreeBSD.ORG Wed Jun 22 04:11:30 2005 Return-Path: X-Original-To: questions@freebsd.org Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 35A4216A41C for ; Wed, 22 Jun 2005 04:11:30 +0000 (GMT) (envelope-from on@cs.ait.ac.th) Received: from mail.cs.ait.ac.th (mail.cs.ait.ac.th [192.41.170.16]) by mx1.FreeBSD.org (Postfix) with ESMTP id 211EB43D49 for ; Wed, 22 Jun 2005 04:11:28 +0000 (GMT) (envelope-from on@cs.ait.ac.th) Received: from banyan.cs.ait.ac.th (banyan.cs.ait.ac.th [192.41.170.5]) by mail.cs.ait.ac.th (8.12.11/8.12.11) with ESMTP id j5M4BQOs004497 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 22 Jun 2005 11:11:27 +0700 (ICT) Received: (from on@localhost) by banyan.cs.ait.ac.th (8.13.1/8.12.11) id j5M4BQ6G087888; Wed, 22 Jun 2005 11:11:26 +0700 (ICT) Date: Wed, 22 Jun 2005 11:11:26 +0700 (ICT) Message-Id: <200506220411.j5M4BQ6G087888@banyan.cs.ait.ac.th> From: Olivier Nicole To: questions@freebsd.org X-Virus-Scanned: on CSIM by amavisd-milter (http://www.amavis.org/) Cc: Subject: Using regex(3) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jun 2005 04:11:30 -0000 Hi, I must missunderstand how to use regex(3). >From what I read in the man page, pmatch[i].rm_so is the begining of the i-th match in the regular expression and pmatch[i].rm-so is the end. So if I try to match the regex "a(.)c" on the string "abc" I should have: pamtch[1].rm_so=1 and pmatch[1].rm_eo=2, that is matching the substring "b". I have run the short programm as follow: #include #include #include main() { int ret; regex_t *preg; size_t nmatch; regmatch_t * pmatch; char * buffer="a(.)c"; char * string="abc"; preg=(regex_t*)malloc(sizeof(regex_t)); if(preg==NULL) exit(-1); ret=regcomp(preg, buffer, REG_EXTENDED); printf("number of substrings=%d\n", preg->re_nsub); pmatch=(regmatch_t *)malloc(5000); /* make it big enough */ if (pmatch==NULL) exit(-1); nmatch=0; ret=regexec(preg, string, nmatch, pmatch, 0); printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo ); nmatch=1; ret=regexec(preg, string, nmatch, pmatch, 0); printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo ); nmatch=2; ret=regexec(preg, string, nmatch, pmatch, 0); printf("return from regexec=%d\nnmatch=%d\np0.so=%d p0.eo=%d\np1.so=%d p1.eo=%d\np2.so=%d p2.eo=%d\np3.so=%d p3.eo=%d\n", ret, nmatch, pmatch[0].rm_so, pmatch[0].rm_eo, pmatch[1].rm_so, pmatch[1].rm_eo, pmatch[2].rm_so, pmatch[2].rm_eo, pmatch[3].rm_so, pmatch[3].rm_eo ); } And the results I get are: banyan33: ./test number of substrings=1 return from regexec=0 nmatch=0 p0.so=0 p0.eo=0 p1.so=0 p1.eo=0 p2.so=0 p2.eo=0 p3.so=0 p3.eo=0 return from regexec=0 nmatch=1 p0.so=0 p0.eo=0 p1.so=3 p1.eo=0 p2.so=0 p2.eo=0 p3.so=0 p3.eo=0 return from regexec=0 nmatch=2 p0.so=0 p0.eo=0 p1.so=3 p1.eo=0 p2.so=1 p2.eo=0 p3.so=2 p3.eo=0 banyan34: Both on 4.10 releng and 5.3 releng, rm_eo is always empty and the result is pushed in the next rm_so. Any help appreciated. Olivier