| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 3: Basic Library Functions Oracle Solaris 11 Information Library |
enable_extended_FILE_stdio(3C)
posix_spawnattr_getschedparam(3C)
posix_spawnattr_getschedpolicy(3C)
posix_spawnattr_getsigdefault(3C)
posix_spawnattr_getsigignore_np(3C)
posix_spawnattr_getsigmask(3C)
posix_spawnattr_setschedparam(3C)
posix_spawnattr_setschedpolicy(3C)
posix_spawnattr_setsigdefault(3C)
posix_spawnattr_setsigignore_np(3C)
posix_spawnattr_setsigmask(3C)
posix_spawn_file_actions_addclose(3C)
posix_spawn_file_actions_addclosefrom_np(3C)
posix_spawn_file_actions_adddup2(3C)
posix_spawn_file_actions_addopen(3C)
posix_spawn_file_actions_destroy(3C)
posix_spawn_file_actions_init(3C)
pthread_attr_getdetachstate(3C)
pthread_attr_getinheritsched(3C)
pthread_attr_getschedparam(3C)
pthread_attr_getschedpolicy(3C)
pthread_attr_setdetachstate(3C)
pthread_attr_setinheritsched(3C)
pthread_attr_setschedparam(3C)
pthread_attr_setschedpolicy(3C)
pthread_barrierattr_destroy(3C)
pthread_barrierattr_getpshared(3C)
pthread_barrierattr_setpshared(3C)
pthread_condattr_getpshared(3C)
pthread_condattr_setpshared(3C)
pthread_cond_reltimedwait_np(3C)
pthread_key_create_once_np(3C)
pthread_mutexattr_getprioceiling(3C)
pthread_mutexattr_getprotocol(3C)
pthread_mutexattr_getpshared(3C)
pthread_mutexattr_getrobust(3C)
pthread_mutexattr_setprioceiling(3C)
pthread_mutexattr_setprotocol(3C)
pthread_mutexattr_setpshared(3C)
pthread_mutexattr_setrobust(3C)
pthread_mutex_getprioceiling(3C)
pthread_mutex_reltimedlock_np(3C)
pthread_mutex_setprioceiling(3C)
pthread_rwlockattr_destroy(3C)
pthread_rwlockattr_getpshared(3C)
pthread_rwlockattr_setpshared(3C)
pthread_rwlock_reltimedrdlock_np(3C)
pthread_rwlock_reltimedwrlock_np(3C)
pthread_rwlock_timedrdlock(3C)
pthread_rwlock_timedwrlock(3C)
rctlblk_get_enforced_value(3C)
- regular expression matching
#include <sys/types.h>#include <regex.h>intregcomp(regex_t *restrictpreg,const char *restrictpattern,intcflags);
intregexec(const regex_t *restrictpreg,const char *restrictstring,size_tnmatch,regmatch_tpmatch[restrict],inteflags);
size_tregerror(interrcode,const regex_t *restrictpreg,char *restricterrbuf,size_terrbuf_size);
voidregfree(regex_t *preg);
These functions interpretbasic andextended regular expressions (described on theregex(5)manual page).
The structure typeregex_t contains at least the following member:
Number of parenthesised subexpressions.
The structure typeregmatch_t contains at least the following members:
Byte offset from start ofstring to start of substring.
Byte offset from start ofstring of the first character after the end of substring.
Theregcomp() function will compile the regular expression contained in the stringpointed to by thepattern argument and place the results in thestructure pointed to bypreg. Thecflags argument is the bitwise inclusive ORof zero or more of the following flags, which are defined inthe header<regex.h>:
Use Extended Regular Expressions.
Ignore case in match.
Report only success/fail inregexec().
Change the handling of NEWLINE characters, as described in the text.
The default regular expression type forpattern is a Basic Regular Expression.The application can specify Extended Regular Expressions using theREG_EXTENDEDcflags flag.
If theREG_NOSUB flag was not set incflags, thenregcomp() willsetre_nsub to the number of parenthesised subexpressions (delimited by \(\) inbasic regular expressions or () in extended regular expressions) found in pattern.
Theregexec() function compares the null-terminated string specified bystring with thecompiled regular expressionpreg initialized by a previous call toregcomp(). Theeflags argument is the bitwise inclusive OR of zero or more of thefollowing flags, which are defined in the header <regex.h>:
The first character of the string pointed to bystring is not the beginning of the line. Therefore, the circumflex character (^), when taken as a special character, will not match the beginning ofstring.
The last character of the string pointed to bystring is not the end of the line. Therefore, the dollar sign ($), when taken as a special character, will not match the end ofstring.
Ifnmatch is zero orREG_NOSUB was set in thecflags argumenttoregcomp(), thenregexec() will ignore thepmatch argument. Otherwise, thepmatchargument must point to an array with at leastnmatch elements, andregexec()will fill in the elements of that array with offsets of thesubstrings ofstring that correspond to the parenthesised subexpressions ofpattern:pmatch[i].rm_sowill be the byte offset of the beginning andpmatch[i].rm_eo will beone greater than the byte offset of the end of substringi. (Subexpressioni begins at theith matched open parenthesis, counting from 1.) Offsetsinpmatch[0] identify the substring that corresponds to the entire regular expression. Unusedelements ofpmatch up topmatch[nmatch-1] will be filled with-1. Ifthere are more thannmatch subexpressions inpattern (pattern itself counts as asubexpression), thenregexec() will still do the match, but will record onlythe firstnmatch substrings.
When matching a basic or extended regular expression, any given parenthesised subexpressionofpattern might participate in the match of several different substrings ofstring, or it might not match any substring even though the patternas a whole did match. The following rules are used to determine whichsubstrings to report inpmatch when matching regular expressions:
If subexpressioni in a regular expression is not contained within another subexpression, and it participated in the match several times, then the byte offsets inpmatch[i] will delimit the last such match.
If subexpressioni is not contained within another subexpression, and it did not participate in an otherwise successful match, the byte offsets inpmatch[i] will be-1. A subexpression does not participate in the match when:
* or\{\} appears immediately after the subexpression in a basic regular expression, or*,?, or{ } appears immediately after the subexpression in an extended regular expression, and the subexpression did not match (matched zero times)
or
| is used in an extended regular expression to select this subexpression or another, and the other subexpression matched.
If subexpressioni is contained within another subexpressionj, andi is not contained within any other subexpression that is contained withinj, and a match of subexpressionj is reported inpmatch[j], then the match or non-match of subexpressioni reported inpmatch[i] will be as described in 1. and 2. above, but within the substring reported inpmatch[j] rather than the whole string.
If subexpressioni is contained in subexpressionj, and the byte offsets inpmatch[j] are-1, then the pointers inpmatch[i] also will be-1.
If subexpressioni matched a zero-length string, then both byte offsets inpmatch[i] will be the byte offset of the character orNULL terminator immediately following the zero-length string.
If, whenregexec() is called, the locale is different from when theregular expression was compiled, the result is undefined.
IfREG_NEWLINE is not set incflags, then a NEWLINE character inpattern orstring will be treated as an ordinary character. IfREG_NEWLINEis set, then newline will be treated as an ordinary character except asfollows:
A NEWLINE character instring will not be matched by a period outside a bracket expression or by any form of a non-matching list.
A circumflex (^) inpattern, when used to specify expression anchoring will match the zero-length string immediately after a newline instring, regardless of the setting ofREG_NOTBOL.
A dollar-sign ($) inpattern, when used to specify expression anchoring, will match the zero-length string immediately before a newline instring, regardless of the setting ofREG_NOTEOL.
Theregfree() function frees any memory allocated byregcomp() associated withpreg.
The following constants are defined as error return values:
Theregexec() function failed to match.
Invalid regular expression.
Invalid collating element referenced.
Invalid character class type referenced.
Trailing \ in pattern.
Number in \digit invalid or in error.
[ ] imbalance.
The function is not supported.
\( \) or() imbalance.
\{ \} imbalance.
Content of \{ \} invalid: not a number, number too large, more than two numbers, first larger than second.
Invalid endpoint in range expression.
Out of memory.
?, * or + not preceded by valid regular expression.
Theregerror() function provides a mapping from error codes returned byregcomp()andregexec() to unspecified printable strings. It generates a string corresponding tothe value of theerrcode argument, which must be the last non-zero valuereturned byregcomp() orregexec() with the given value ofpreg. Iferrcode is not such a value, an error message indicating that theerror code is invalid is returned.
Ifpreg is aNULL pointer, buterrcode is a value returnedby a previous call toregexec() orregcomp(), theregerror() still generatesan error string corresponding to the value oferrcode.
If theerrbuf_size argument is not zero,regerror() will place the generatedstring into the buffer of sizeerrbuf_size bytes pointed to byerrbuf.If the string (including the terminatingNULL) cannot fit in the buffer,regerror()will truncate the string and null-terminate the result.
Iferrbuf_size is zero,regerror() ignores theerrbuf argument, and returns thesize of the buffer needed to hold the generated string.
If thepreg argument toregexec() orregfree() is not a compiledregular expression returned byregcomp(), the result is undefined. Apreg isno longer treated as a compiled regular expression after it is given toregfree().
Seeregex(5) for BRE (Basic Regular Expression) Anchoring.
On successful completion, theregcomp() function returns0. Otherwise, it returns aninteger value indicating an error as described in <regex.h>, and the contentofpreg is undefined.
On successful completion, theregexec() function returns0. Otherwise it returnsREG_NOMATCHto indicate no match, orREG_ENOSYS to indicate that the function isnot supported.
Upon successful completion, theregerror() function returns the number of bytes neededto hold the entire generated string. Otherwise, it returns0 to indicatethat the function is not implemented.
Theregfree() function returns no value.
No errors are defined.
An application could use:
regerror(code,preg,(char *)NULL,(size_t)0)
to find out how big a buffer is needed for the generatedstring,malloc a buffer to hold the string, and then callregerror()again to get the string (seemalloc(3C)). Alternately, it could allocate afixed, static buffer that is big enough to hold most strings, andthen usemalloc() to allocate a larger buffer if it finds thatthis is too small.
Example 1 Example to match string against the extended regular expression in pattern.
#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*/intmatch(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);}The following demonstrates how theREG_NOTBOL flag could be used withregexec()to find all substrings in a line that match a pattern suppliedby a user. (For simplicity of the example, very little error checkingis done.)
(void) regcomp (&re, pattern, 0);/* this call to regexec( ) finds the first match on the line */error = regexec (&re, &buffer[0], 1, &pm, 0);while (error == 0) { /* while matches found */ /* substring found between pm.rm_so and pm.rm_eo */ /* This call to regexec( ) finds the next match */ error = regexec (&re, buffer + pm.rm_eo, 1, &pm, REG_NOTBOL);}Seeattributes(5) for descriptions of the following attributes:
|
fnmatch(3C),glob(3C),malloc(3C),setlocale(3C),attributes(5),standards(5),regex(5)
Theregcomp() function can be used safely in a multithreaded application aslong assetlocale(3C) is not being called to change the locale.
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |