Solved

Example of using: regcomp, regexec

Posted on 1997-06-03
3
7,151 Views
Last Modified: 2013-12-26
Hi,

Could anyone give me some examples about how to use regcom, and regexec? Thanks in advance.
0
Comment
Question by:yxw1
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
dhm earned 80 total points
ID: 1293179
I'll append a sample program that lets you enter a regular expression, then checks for matches of the expression in other strings you enter.  When you enter a blank line, you can enter a new regular expression.

This program was compiled on a Sun Ultra running Solaris-2.5.1.  Other systems' versions of regcomp are slightly different, as are the regular expressions they handle.  Still, this should get you started.  If you're using a really odd system, let me know what it is and I'll see if I can figure out how the regcomp works on it.

Here's what the program looks like in action:

dhm@eagleone(18)$ ./regexp
Enter a regular expression (empty line to exit): abc(def|xyz)
Expression contains 1 sub-expressions
Enter a string to compare (empty line to exit): 123abcdef456
Match  0 from  3 to  9: "abcdef"
Match  1 from  6 to  9: "def"
Enter a string to compare (empty line to exit): 123abcxyz456
Match  0 from  3 to  9: "abcxyz"
Match  1 from  6 to  9: "xyz"
Enter a string to compare (empty line to exit): abcxy
No match: failed to match
Enter a string to compare (empty line to exit):
Enter a regular expression (empty line to exit):
Goodbye

And here's the program itself:

/*
 * Regular expression sample program
 *
 * by dave madden <dhm@webvision.com>
 * copyright (c) 1996 by dave madden and webvision inc.  all rights
 * reserved.  unauthorized use, distribution, sale, or modification
 * prohibited.
 */
#include <stdio.h>
#include <sys/types.h>
#include <malloc.h>
#include <regex.h>

int
main( int argc, char **argv )
{
      int                   result;
      regex_t             rx;
      regmatch_t      *matches;
      char             buf[1024];

      for ( ; ; ) {
            fputs( "Enter a regular expression (empty line to exit): ", stderr );
            if (!gets( buf ) || !buf[0]) break;

            result = regcomp( &rx, buf, REG_EXTENDED );
            if (result != 0) {
                  regerror( result, &rx, buf, sizeof(buf) );
                  fprintf( stderr, "Invalid regular expression: %s\n", buf );
                  regfree( &rx );
                  continue;
            }

            fprintf( stderr, "Expression contains %d sub-expressions\n", rx.re_nsub );

            matches = (regmatch_t *)malloc( (rx.re_nsub + 1) * sizeof(regmatch_t) );
            if (!matches) {
                  fputs( "Out of memory!\n", stderr );
                  break;
            }

            for ( ; ; ) {
                  fputs( "Enter a string to compare (empty line to exit): ", stderr );
                  if (!gets( buf ) || !buf[0]) break;

                  result = regexec( &rx, buf, rx.re_nsub + 1, matches, 0 );

                  if (!result) {
                        int       i;
                        for (i = 0; i <= rx.re_nsub; i++) {
                              if (matches[i].rm_so != -1) {
                                    fprintf( stderr, "Match %2d from %2d to %2d: \"%.*s\"\n",
                                                 i, matches[i].rm_so, matches[i].rm_eo,
                                                 matches[i].rm_eo - matches[i].rm_so,
                                                 buf + matches[i].rm_so );
                              }
                        }
                  } else {
                        regerror( result, &rx, buf, sizeof(buf) );
                        fprintf( stderr, "No match: %s\n", buf );
                  }
            }

            free( matches );
            regfree( &rx );
      }

      fputs( "Goodbye\n", stderr );
      exit( 0 );
}

0
 

Author Comment

by:yxw1
ID: 1293180
Some comment in programme will be more appreciated.
0
 

Author Comment

by:yxw1
ID: 1293181
The program runs fine. However, I found that:

1) if regular expression is like a{0,9}, I want to match 0 to 9 "a"s, The system seems can't match it in expression 123aa, only works on aa123.

2) The loop is limited to 255, i.e, expression: a{0,256} will be invalid. Why?

3) I am using Sparc 2 station, so could you please tell me where I can get GNU version of regular expression libs.

Thanks. Please e-mail me at ywang@maingate.net
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 140
sumNumber challenge 16 138
conditional code and condition difference 9 84
Increment column based of a FK 8 31
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question