Solved

Example of using: regcomp, regexec

Posted on 1997-06-03
3
7,231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 92
substring method in java 1 142
VBA "SendKeys" Syntax for Multiple Keystrokes 7 133
has77  challenge 9 107
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 …
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…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

752 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