C Programming Code Examples
Learn C Language
regexec() Function in C Programming Language
regexec() Function in C
This is function is used for matching the string with the given pattern of the string. This also has 5 arguments like precompiled pattern, the second parameter which will take string which needs to be searched for, the third parameter contains the details of the location of matches, the fourth parameter contains details of searches, and the fifth parameter contains the flag which gives the indication the change in the matching behavior. This regexec() function returns 0 if there is successful matching done and REG_NOMATCH if the string does not match. Compares the NULL-terminated string specified by string against the compiled regular expression, preg.
Syntax for regexec() Function in C
#include <regex.h>
int regexec(const regex_t *preg, const char *string,
size_t nmatch, regmatch_t *pmatch, int eflags);
preg
preg is a pointer to a compiled regular expression to compare against STRING.
string
string is a pointer to a string to be matched.
nmatch
nmatch is the number of sub-expressions to match.
pmatch
pmatch is an array of offsets into STRING which matched the corresponding sub-expressions in preg.
eflags
eflags is a bit flag defining customizable behavior of regexec().
If a match is found, regexec() returns 0.
If unsuccessful, regexec() returns nonzero indicating either no match or an error.
REG_NOTBOL Indicates that the first character of STRING is not the beginning of the line.
REG_NOTEOL Indicates that the first character of STRING is not the end of the line.
If nmatch parameter is 0 or REG_NOSUB was set on the call to regcomp(), regexec() ignores the pmatch argument. Otherwise, the pmatch argument points to an array of at least nmatch elements. The regexec() function fills in the elements of the array with offsets of the substrings of STRING that correspond to the parenthesized sub-expressions of the original pmatch specified to regcomp(). The 0th element of the array corresponds to the entire pattern. If there are more than nmatch sub-expressions, only the first nmatch-1 are recorded.
When matching a basic or extended regular expression, any given parenthesized sub-expression of pmatch might participate in the match of several different substrings of STRING. The following rules determine which substrings are reported in pmatch.
• If a sub-expression participated in a match several times, the offset of the last matching substring is reported in pmatch.
• If a sub-expression did not match in the source STRING, the offset shown in pmatch is set to -1.
• If a sub-expression contains sub-expressions, the data in pmatch refers to the last such sub-expression.
• If a sub-expression matches a zero-length string, the offsets in pmatch refer to the byte immediately following the matching string.
If EREG_NOSUB was set when regcomp() was called, the contents of pmatch are unspecified.
If REG_NEWLINE was set when regcomp() was called, newline characters are allowed in STRING.
With z/OS® XL C/C++, the string passed to the regexec() function is assumed to be in the initial shift state, unless REG_NOTBOL is specified. If REG_NOTBOL is specified, the shift state used is the shift state after the last call to the regexec() function.
The information returned by the regexec() function in the regmatch_t structure has the shift-state at the start and end of the string added. This will assist an application to perform replacements or processing of the partial string. To perform replacements, the application must add the required shift-out and shift-in characters where necessary. No library functions are available to assist the application.
If MB_CUR_MAX is specified as 4, but the charmap file does not specify the DBCS characters, and a collating-element (for example, [:a:]) is specified in the pattern, the DBCS characters will not match against the collating-element even if they have an equivalent weight to the collating-element.
/* This example compiles an extended regular expression, and match against a string. */
#include <regex.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
main() {
regex_t preg;
char *string = "a simple string";
char *pattern = ".*(simple).*";
int rc;
size_t nmatch = 2;
regmatch_t pmatch[2];
if ((rc = regcomp(&preg, pattern, REG_EXTENDED)) != 0) {
printf("regcomp() failed, returning nonzero (%d)\n", rc);
exit(1);
}
if ((rc = regexec(&preg, string, nmatch, pmatch, 0)) != 0) {
printf("failed to ERE match '%s' with '%s',returning %d.\n",
string, pattern, rc);
}
regfree(&preg);
}
C program accept an array of N elements and a key to search. Search is successful, displays the message "Successful Search". Otherwise, the message "Unsuccessful Search" is display.
For loop to print value and address of each element of array. Just to demonstrate that the array elements are stored in contiguous locations, I am displaying the addresses in...
Input length and width of a rectangle and find area of the given rectangle. How to calculate area of a rectangle in C programming. Input length and width of rectangle. Store it in two
Takes the binary number (entered by user) as input and converts it into a decimal number using function. To understand this program, you should be familiar with the following C...
C program example read elements in an array and search whether an element exists in the array or not. Program to search an elements in an array linearly. There are two searching
Input number from user and check whether the number is even or odd using switch case. A number is said even number if it is exactly divisible by 2. In C we use Modulo operator...
The main logic lies in displaying the numbers in the screen. The X & Y coordinate of each function is calculated with respect to mx "maximum of x value" & my "maximum of y