Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C Programming Code Examples

Learn C Language

regcomp() Function in C Programming Language

regcomp() Function in C
A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C. Patterns in the POSIX library: [] - Used to find any of the characters or numbers specified between the brackets. [:number:] - Used to find any digit. [:lower:] - Used to find lowercase alphabets. [:word:] - Used to find letters numbers and underscores. Compiles the regular expression specified by pattern into an executable string of op-codes.
Syntax for regcomp() Function in C
#include <regex.h> int regcomp(regex_t *_restrict_ preg, const char *_restrict_ pattern, int cflags);
preg is a pointer to a compiled regular expression.
pattern is a pointer to a character string defining a source regular expression (described below).
cflags is a bit flag defining configurable attributes of compilation process:
Support extended regular expressions.
Ignore case in match.
Eliminate any special significance to the newline character.
Report only success or fail in regexec(), that is, verify the syntax of a regular expression. If this flag is set, the regcomp() function sets re_nsub to the number of parenthesized sub-expressions found in pattern. Otherwise, a sub-expression results in an error. If successful, regcomp() returns 0. If unsuccessful, regcomp() returns nonzero, and the content of preg is undefined. The regcomp() function under z/OS XL C/C++ will use the definition of characters according to the current LC_SYNTAX category. The characters, [, ], {, }, |, ^, and $, have varying code points in different encoded character sets. The functions regcomp(), regerror(), regexec(), and regfree() use regular expressions in a similar way to the UNIX awk, ed, grep, and egrep commands. The simplest form of regular expression is a string of characters with no special meaning. The following characters do have special meaning; they are used to form extended regular expressions: . The period symbol matches any one character except the terminal newline character. [character-character] The hyphen symbol, within square brackets, means "through". It fills in the intervening characters according to the current collating sequence. For example, [a-z] can be equivalent to [abc...xyz] or, with a different collating sequence, it can be equivalent to [aAbBcC...xXyYzZ]. [string] A string within square brackets specifies any of the characters in string. Thus [abc], if compared to other strings, would match any that contained a, b, or c. No assumptions are made at compile time about the actual characters contained in the range. {m} {m,} {m,u} Integer values enclosed in {} indicate the number of times to apply the preceding regular expression. m is the minimum number, and u is the maximum number. u must not be greater than RE_DUP_MAX (see limits.h). If you specify only m, it indicates the exact number of times to apply the regular expression. {m,} is equivalent to {m,u}. They both match m or more occurrences of the expression. * The asterisk symbol indicates 0 or more of any characters. For example, [a*e] is equivalent to any of the following: 99ae9, aaaaae, a999e99. $ The dollar symbol matches the end of the string. (Use \n to match a newline character.) character+ The plus symbol specifies one or more occurrences of a character. Thus, smith+ern is equivalent to, for example, smithhhern. [^string] The caret symbol, when inside square brackets, negates the characters within the square brackets. Thus [^abc], if compared to other strings, would fail to match any that contains even one a, b, or c. (expression)$n Stores the value matched by the enclosed regular expression in the (n+1)th ret parameter. Ten enclosed regular expressions are allowed. Assignments are made unconditionally. (expression) Groups a sub-expression allowing an operator, such as *, +, or [].], to work on the sub-expression enclosed in parentheses. For example, (a*(cb+)*)$0. • Do not use multibyte characters. • You can use the ] (right square bracket) alone within a pair of square brackets, but only if it immediately follows either the opening left square bracket or if it immediately follows [^. For example: []-] matches the ] and - characters. • All the preceding symbols are special. You precede them with \ to use the symbol itself. For example, a\.e is equivalent to a.e. • You can use the - (hyphen) by itself, but only if it is the first or last character in the expression. For example, the expression []--0] matches either the ] or else the characters - through 0. Otherwise, use \-.
/* compile regular expression by regcomp() function code example */ #include <regex.h> /* * Match string against the extended regular expression in * pattern, treating errors as no match. * * Return 1 for match, 0 for no match. */ int match(const char *string, char *pattern) { int status; regex_t re; if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) { return(0); /* Report error. */ } status = regexec(&re, string, (size_t) 0, NULL, 0); regfree(&re); if (status != 0) { return(0); /* Report error. */ } return(1); }

Strong number is special number whose sum of Factorial of Digits is equal to the original number. For example: 145 is strong number. Since, 1! + 4! + 5! = 145 Logic to print Strong

Code find 2 Elements in the Array such that Difference between them is Largest. This C Program checks 2 elements in the array such that difference between them is largest and

'Pyramid program' in C Programming. Mirror image of right angled triangle. "space_count" variable is used as 'Subscript Variable' used in for loop. 'noofspaces' variable is used to keep

C Program for Loop to display the Fibonacci series. Program prompts user for the number of terms and displays the series having the same number of terms. Program ask user to