regcmp, regex - compile and execute regular expression
#include <libgen.h> char *regcmp(const char *string1, /* char *string2 */ ..., int /*(char*)0*/);
char *regex(const char *re, const char *subject, /* char *ret0 */ ...);
extern char *__loc1;
The regcmp() function compiles a regular expression (consisting of the concatenated arguments) and returns a pointer to the compiled form. The malloc(3C) function is used to create space for the compiled form. It is the user's responsibility to free unneeded space so allocated. A NULL return from regcmp() indicates an incorrect argument. regcmp(1) has been written to generally preclude the need for this routine at execution time.
The regex() function executes a compiled pattern against the subject string. Additional arguments are passed to receive values back. The regex() function returns NULL on failure or a pointer to the next unmatched character on success. A global character pointer __loc1 points to where the match began. The regcmp() and regex() functions were mostly borrowed from the editor ed(1); however, the syntax and semantics have been changed slightly. The following are the valid symbols and associated meanings.
[]*.^
$
-
+
{m} {m,} {m,u}
( ... )$n
( ... )
Example 1 Example matching a leading newline in the subject string.
The following example matches a leading newline in the subject string pointed at by cursor.
char *cursor, *newcursor, *ptr; ... newcursor = regex((ptr = regcmp("^\n", (char *)0)), cursor); free(ptr);
The following example matches through the string Testing3 and returns the address of the character after the last matched character (the ``4''). The string Testing3 is copied to the character array ret0.
char ret0[9]; char *newcursor, *name; ... name = regcmp("([A-Za-z][A-za-z0-9]{0,7})$0", (char *)0); newcursor = regex(name, "012Testing345", ret0);
The following example applies a precompiled regular expression in file.i (see regcmp(1)) against string.
#include "file.i" char *string, *newcursor; ... newcursor = regex(name, string);
See attributes(5) for descriptions of the following attributes:
|
ed(1), regcmp(1), malloc(3C), attributes(5), regexp(5)
The user program may run out of memory if regcmp() is called iteratively without freeing the vectors no longer required.
When compiling multithreaded applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multithreaded applications.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |