Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Example of using: regcomp, regexec

Posted on 1997-06-03
3
Medium Priority
?
7,405 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 240 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

773 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