Solved

Example of using: regcomp, regexec

Posted on 1997-06-03
3
7,126 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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