Solved

Example of using: regcomp, regexec

Posted on 1997-06-03
3
7,031 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
Comment Utility
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
Comment Utility
Some comment in programme will be more appreciated.
0
 

Author Comment

by:yxw1
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now