Clumsy reg comp php. Man regcomp(3): POSIX regular expression functions

> int regcomp(regex_t * preg, const char *regex, int cflags); int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch, int eflags); size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size); void regfree(regex_t *preg);

Description

POSIX regex compilation regcomp() is used to compile a regular expression into a form that is suitable for subsequent regexec()searches.

regcomp() is supplied with preg, a pointer to a pattern buffer storage area; regex, a pointer to the null-terminated string and cflags, flags used to determine the type of compilation.

All regular expression searching must be done via a compiled pattern buffer, thus regexec() must always be supplied with the address of a regcomp() initialized pattern buffer.

cflags may be the bitwise- or of one or more of the following: REG_EXTENDED Use POSIX Extended Regular Expression syntax when interpreting regex. If not set POSIX Basic Regular Expression syntax is used. REG_ICASE Do not differentiate case. Subsequent regexec() searches using this pattern buffer will be case insensitive. REG_NOSUB Do not report position of matches. The nmatch and pmatch arguments to regexec() are ignored if the pattern buffer supplied was compiled with this flag set. REG_NEWLINE Match-any-character operators don"t match a newline.

A nonmatching list ( [^...] ) not containing a newline does not match a newline.

Match-beginning-of-line operator ( ^ ) matches the empty string immediately after a newline, regardless of whether eflags, the execution flags regexec(), contains REG_NOTBOL.

Match-end-of-line operator ( $ ) matches the empty string immediately before a newline, regardless of whether eflags contains REG_NOTEOL.

POSIX regex matching regexec() is used to match a null-terminated string against the precompiled pattern buffer, preg. nmatch and pmatch are used to provide information regarding the location of any matches. eflags may be the bitwise- or of one or both of REG_NOTBOL and REG_NOTEOL which cause changes in matching behavior described below. REG_NOTBOL The match-beginning-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above) This flag may be used when different portions of a string are passed to regexec() and the beginning of the string should not be interpreted as the beginning of the line. REG_NOTEOL The match-end-of-line operator always fails to match (but see the compilation flag REG_NEWLINE above)

Byte offsets Unless REG_NOSUB was set for the compilation of the pattern buffer, it is possible to obtain match addressing information. pmatch must be dimensioned to have at least nmatch elements. These are filled in by regexec() with substring match addresses. The offsets of the subexpression starting at the i th open parenthesis are stored in pmatch[i]. The entire regular expression"s match addresses are stored in pmatch. (Note that to return the offsets of N subexpression matches, nmatch must be at least N+1.) Any unused structure elements will contain the value -1.

The regmatch_t structure which is the type of pmatch is defined in .

Typedef struct ( regoff_t rm_so; regoff_t rm_eo; ) regmatch_t; Each rm_so element that is not -1 indicates the start offset of the next largest substring match within the string. The relative rm_eo element indicates the end offset of the match, which is the offset of the first character after the matching text.

POSIX error reporting regerror() is used to turn the error codes that can be returned by both regcomp() and regexec() into error message strings.

regerror() is passed the error code, errcode,the pattern buffer, preg, a pointer to a character string buffer, errbuf, and the size of the string buffer, errbuf_size. It returns the size of the errbuf required to contain the null-terminated error message string. If both errbuf and errbuf_size are nonzero, errbuf is filled in with the first errbuf_size - 1 characters of the error message and a terminating null byte ("\0").

POSIX pattern buffer freeing Supplying regfree() with a precompiled pattern buffer, preg will free the memory allocated to the pattern buffer by the compiling process, regcomp().

Return Value

regcomp() returns zero for a successful compilation or an error code for failure.

regexec() returns zero for a successful match or REG_NOMATCH for failure.

Errors

The following errors can be returned by regcomp(): REG_BADBR Invalid use of back reference operator. REG_BADPAT Invalid use of pattern operators such as group or list. REG_BADRPT Invalid use of repetition operators such as using "*" as the first character. REG_EBRACE Un-matched brace interval operators. REG_EBRACK Un-matched bracket list operators. REG_ECOLLATE Invalid collating element. REG_ECTYPE Unknown character class name. REG_EEND Nonspecific error. This is not defined by POSIX.2. REG_EESCAPE Trailing backslash. REG_EPAREN Un-matched parenthesis group operators. REG_ERANGE Invalid use of the range operator, e.g., the ending point of the range occurs prior to the starting point. REG_ESIZE Compiled regular expression requires a pattern buffer larger than 64Kb. This is not defined by POSIX.2. REG_ESPACE The regex routines ran out of memory. REG_ESUBREG Invalid back reference to a subexpression.

To work with regular expressions in C/C++, the regex.h library has been created. It predetermines the main functions:

#include int regcomp (regex_t *restrict preg, const char *restrict pattern, int cflags); int regexec (const regex_t *restrict preg, const char *restrict string, size_t nmatch, regmatch_t pmatch , int eflags); void regfree (regex_t *preg); size_t regerror (int errcode, const regex_t *restrict preg, char *restrict errbuf, size_t errbuf_size);

Function regcomp is intended for compiling a regular expression. This function compiles the regular expression pattern given the cflags and places it in a preg structure.
Flags can be composed as a bitwise or of the following elements:

  • REG_EXTENDED - use extended regular expressions
  • REG_ICASE - do not distinguish between uppercase and lowercase letters when matching a string with a regular expression

Function regexec matches a regular expression compiled and placed in a preg structure with the string string. In this case, if the comparison is successful, a zero value is returned; otherwise, an error code is returned. The eflags argument is a bitwise OR of REG_NOTBOL and REG_NOTEOL. It determines whether the chain boundaries are line boundaries (for handling the ^ and $ latches). If the nmatch argument is zero, then pmatch is ignored, otherwise it must point to an nmatch array of elements, which will be filled with substring offsets.

To decrypt errors, use the function regerror(). It converts the errcode returned by matching the regular expression and the string and places the error string value in the errbuf variable, specifying its size.

Function regfree() frees memory requested when compiling the regular expression. A pointer to a regular expression structure cannot be used after this...

Let's look at an example:

#include #include using namespace std; #define PATTERN "^(0,3).(0,3).(0,3).(0,3)$" #define STRING "127.0.0.1" int main() ( regex_t preg; int err,regerr ; err = regcomp (&preg, PATTERN, REG_EXTENDED); if (err != 0) ( char buff; regerror(err, &preg, buff, sizeof(buff)); cout<< buff; } regmatch_t pm; regerr = regexec (&preg, STRING, 0, &pm, 0); if (regerr == 0) cout << "true"; else { cout << "false\n"; char errbuf; regerror(regerr, &preg, errbuf, sizeof(errbuf)); cout << errbuf; } return 0; }

This function is the simplest function for IP address recognition. Using the define directive, we have predefined a string with a regular expression and a string to check. In the main function, the regular expression PATTERN is first compiled and placed into a preg structure. We immediately check for errors and if there are any, display them on the screen. After this, we compare the STRING string with the regular expression in preg. If there is a match, we output "true", otherwise - "false" and decipher the error using regerror.

Format

#include int regcomp(regex_t *_restrict_ preg, const char *_restrict_ pattern, int cflags);

General description

Compiles the regular expression specified by pattern into an executable string of op-codes.

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: REG_EXTENDED Support extended regular expressions. REG_ICASE Ignore case in match. REG_NEWLINE Eliminate any special significance to the newline character. REG_NOSUB 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.

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.

Regular expressions

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: Symbol Description. The period symbol matches any one character except the terminal newline character. [ charactercharacter] The hyphen symbol, within square brackets, means “through”. It fills in the intervening characters according to the current collating sequence. For example, can be equivalent to or, with a different collating sequence, it can be equivalent to . [ string] A string within square brackets specifies any of the characters in string. Thus , 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 .

Note:

  1. Do not use multibyte characters.
  2. 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.
  3. All the preceding symbols are special. You precede them with \to use the symbol itself. For example, a\.e is equivalent to a.e .
  4. 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 \–.

Returned value

If successful, regcomp() returns 0.

If unsuccessful, regcomp() returns nonzero, and the content of preg is undefined.

Example

CELEBR07⁄* CELEBR07 This example compiles an extended regular expression. *⁄ #include #include #include #include main() ( regex_t preg; char *string = "a simple string"; char *pattern = ".*(simple).*"; int rc; if ((rc = regcomp(&preg, pattern, REG_EXTENDED)) != 0) ( printf("regcomp() failed, returning nonzero (%d)", rc); exit(1); ) )

> int regcomp(regex_t *preg, const char *regex, intcflags); int regexec(const regex_t *preg, const char *string, size_tnmatch, regmatch_tpmatch, inteflags); size_t regerror(interrcode, const regex_t *preg, char *errbuf, size_terrbuf_size); void regfree(regex_t *preg);

DESCRIPTION

Compiling POSIX regular expressions

Function regcomp() is used to compile the regular expression into a format that is suitable for subsequent searches using regexec().

regcomp() a pointer to the buffer template storage area is passed preg, pointer to a null-terminated string regex and flags cflags, used to determine the compilation type.

All regular expression searches must be done using a compiled buffer pattern, so regexec() must always be called with the address of the buffer template initialized by the function regcomp().

Meaning cflags may consist of bitwise or zero or more of the following values: REG_EXTENDED Use POSIX extended regular expression syntax during interpretation regex. If this flag is not enabled, POSIX simple regular expression syntax is used. REG_ICASE Ignore case. Subsequent searches regexec() using this buffer pattern will not be case sensitive. REG_NOSUB Do not report the position of matches. Options nmatch And pmatch For regexec() are ignored if the given buffer template was compiled with this flag enabled. REG_NEWLINE Match operators with any character do not match the newline character. List of non-matching characters ( [^...] ) without a newline character does not match a newline. Comparison operator based on the beginning of a string ( ^ ) matches the empty string immediately after the newline, no matter what eflags, execution flags regexec(), contain REG_NOTBOL. The end-of-line comparison operator ($) matches the empty string up to the start of the line, no matter what eflags contains REG_NOTEOL.

Comparison with POSIX regular expression

Function regexec() is used to compare a null-terminated string to a pre-processed buffer pattern preg. Arguments nmatch And pmatch are used to provide information about the location of any matches. Meaning eflags can be bitwise OR one or both values REG_NOTBOL And REG_NOTEOL. These values ​​determine the behavior of the comparison process as described below. REG_NOTBOL The comparison operator at the beginning of the string always fails (see the compilation flag described above REG_NEWLINE). This flag can be used when regexec() separate parts of a string are transmitted, and the beginning of such a string in this case should not be interpreted as the beginning of a new line. REG_NOTEOL The end-of-line comparison operator always fails (but see the compilation flag described above REG_NEWLINE).

Byte offsets

If REG_NOSUB is not set when compiling the buffer template, it is possible to obtain information about the position of matches. Meaning pmatch must be defined to have at least nmatch elements. They're filling up regexec() addresses of inline matches. Offsets of a subexpression starting with i th open bracket, stored in pmatch[i]. The match address of the entire regular expression is stored in pmatch(note that to return the match offsets N subexpressions, meaning nmatch must be no less N+1). Any unused structure elements will contain a value of -1.

Structure regmatch_t, which is a type pmatch, defined in :

typedef struct(
regoff_t rm_so;
regoff_t rm_eo;) regmatch_t;

Each element rm_so, not equal to -1, indicates the starting offset of the next largest substring match within the given string. Relative element rm_eo indicates the offset of the end of the match, which is the first character after the matched text.

POSIX Error Reporting

Function regerror used to convert error codes that may be received from regcomp() And regexec(), in the error message lines.

IN regerror transmitted: error code errcode, buffer pattern preg,pointer to character string buffer errbuf and row buffer size errbuf_size. The function returns the size errbuf, which is required to store the error message as a null-terminated string. If errbuf, And errbuf_size are not equal to zero, then errbuf filled first errbuf_size - 1 symbols of the error message and ends with the null byte(aq\0aq).

Freeing a POSIX template buffer

Function regfree() frees the memory allocated to the buffer template preg during the compilation process using regcomp().

RETURN VALUE

Function regcomp() returns zero on successful compilation or an error code otherwise.

Function regexec() returns zero on match or REG_NOMATCH, if there were no matches.

ERRORS

Function regcomp() may return the following errors: REG_BADBR Incorrect use of the backlink operator. REG_BADPAT Incorrect use of template operators, such as group or list operators. REG_BADRPT Incorrect use of repetition operators, such as using "*" as the first character. REG_EBRACE Unpaired parentheses in interval operators. REG_EBRACK Unpaired square brackets in list statements. REG_ECOLLATE Invalid sort element. REG_ECTYPE Unknown character class name. REG_EEND Potential error. Not defined in POSIX.2. REG_EESCAPE Trailing backslash. REG_EPAREN Unpaired parentheses in grouping operators. REG_ERANGE Incorrect use of the area operator: for example, the end of the area appears before its beginning. REG_ESIZE The compiled regular expression requires a buffer pattern larger than 64 KB. This is not defined in POSIX.2. REG_ESPACE Regular expression routines have run out of memory. REG_ESUBREG Invalid backreference to subexpression.

#include
#include
int regcomp(regex_t *preg, const char *regex, intcflags);
int regexec(const regex_t *preg, const char *string, size_tnmatch,
regmatch_tpmatch, inteflags);
size_t regerror(interrcode, const regex_t *preg, char *errbuf,
size_terrbuf_size);
void regfree(regex_t *preg);

DESCRIPTION

Compiling POSIX regular expressions

Function regcomp() is used to compile the regular expression into a format that is suitable for subsequent searches using regexec().

regcomp() a pointer to the buffer template storage area is passed preg, pointer to a null-terminated string regex and flags cflags, used to determine the compilation type.

All regular expression searches must be done using a compiled buffer pattern, so regexec() must always be called with the address of the buffer template initialized by the function regcomp().

Meaning cflags may consist of bitwise or zero or more of the following values:

REG_EXTENDED Use POSIX extended regular expression syntax during interpretation regex. If this flag is not enabled, POSIX simple regular expression syntax is used. REG_ICASE Ignore case. Subsequent searches regexec() using this buffer pattern will not be case sensitive. REG_NOSUB Do not report the position of matches. Options nmatch And pmatch For regexec() are ignored if the given buffer template was compiled with this flag enabled. REG_NEWLINE Match operators with any character do not match the newline character.

List of non-matching characters ( [^...] ) without a newline character is not the same as a newline.

Comparison operator based on the beginning of a string ( ^ ) matches the empty string immediately after the newline no matter what eflags, execution flags regexec(), contain REG_NOTBOL.

The end-of-line comparison operator ($) matches the empty string up to the start-of-line character, no matter what eflags contains REG_NOTEOL.

Comparison with POSIX regular expression

Function regexec() is used to compare a null-terminated string to a pre-processed buffer pattern preg. Arguments nmatch And pmatch are used to provide information about the location of any matches. Meaning eflags can be bitwise OR one or both values REG_NOTBOL And REG_NOTEOL. These values ​​determine the behavior of the comparison process as described below. REG_NOTBOL The comparison operator at the beginning of the string always fails (but see the compilation flag described above REG_NEWLINE). This flag can be used when regexec() separate parts of a string are transmitted, and the beginning of such a string in this case should not be interpreted as the beginning of a new line. REG_NOTEOL The end-of-line comparison operator always fails (but see the compilation flag described above REG_NEWLINE).

Byte offsets

If REG_NOSUB is not set when compiling the buffer template, it is possible to obtain information about the position of matches. Meaning pmatch must be defined to have at least nmatch elements. They're filling up regexec() addresses of inline matches. Offsets of a subexpression starting with i- open parenthesis, stored in pmatch[i]. The match address of the entire regular expression is stored in pmatch(note that to return the match offsets N subexpressions, meaning nmatch must be no less N+1). Any unused structure elements will contain a value of -1.

Structure regmatch_t, which is a type pmatch, defined in :

Typedef struct ( regoff_t rm_so; regoff_t rm_eo; ) regmatch_t;

Each element rm_so, not equal to -1, indicates the starting offset of the next match of the largest substring within the given string. Relative element rm_eo indicates the offset of the end of the match, which is the first character after the matched text.

POSIX Error Reporting

Function regerror used to convert error codes that may be received from regcomp() And regexec(), into error message lines.

IN regerror transmitted: error code errcode, buffer pattern preg, a pointer to a character string buffer errbuf and row buffer size errbuf_size. The function returns the size errbuf, which is required to store the error message as a null-terminated string. If errbuf, And errbuf_size are not equal to zero, then errbuf filled first errbuf_size - 1 error message characters and ends with a null byte ("\0").

Freeing a POSIX template buffer

Function regfree() frees the memory allocated to the buffer template preg during the compilation process using regcomp().

RETURN VALUE

Function regcomp() returns zero on successful compilation or an error code otherwise.

Function regexec() returns zero on match or REG_NOMATCH, if there were no matches.

ERRORS

Function regcomp() may return the following errors: REG_BADBR Incorrect use of the backlink operator. REG_BADPAT Incorrect use of template operators, such as group or list operators. REG_BADRPT Incorrect use of repetition operators, such as using "*" as the first character. REG_EBRACE Unpaired parentheses in interval operators. REG_EBRACK Unpaired square brackets in list statements. REG_ECOLLATE Invalid sort element. REG_ECTYPE Unknown character class name. REG_EEND Potential error. Not defined in POSIX.2. REG_EESCAPE Trailing backslash. REG_EPAREN Unpaired parentheses in grouping operators. REG_ERANGE Incorrect use of the area operator: for example, the end of the area appears before its beginning. REG_ESIZE The compiled regular expression requires a buffer pattern larger than 64 KB. This is not defined in POSIX.2. REG_ESPACE Regular expression routines have run out of memory. REG_ESUBREG Invalid backreference to subexpression.

Views