| René Nyffenegger's collection of things on the web | |
|
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
|
Functions in PCRE | ||
|
PCRE has the following functions if pcre.h is included.
#include <pcre.h>
The functions pcre_compile(),
pcre_study() and
pcre_exec()
are used for compiling and matching regular expressions.
See test 1 for a demonstration of pcre_compile and pcre_exec. pcre_compile
pcre *pcre_compile (
const char *pattern,
int options,
const char **errptr,
int *erroffset,
const unsigned char *tableptr);
The following options are available:
pcre_maketables creates a character table that
is passed through the tableptr parameter.
pcre_studypcre_extra *pcre_study( const pcre *code, int options, const char **errptr);
A pattern can be studied for better execution time in pcre_exec. This makes sense if
the same patterns is used several times.
Currently, there are no options available.
If pcre_study does not find out details to improve execution times, it returns null.
The returned pcre_extra* can be passed as the extra parameter in
pcre_exec.
pcre_exec
int pcre_exec(
const pcre *code,
const pcre_extra *extra,
const char *subject,
int length,
int startoffset,
int options,
int *ovector,
int ovecsize);
pcre_exec matches a string against a pattern.
The following options can be specified:
If the pattern had been studied with pcre_study, the returned findings can be passed in extra.
failure if pcre_exec fails, it returns one of the following negative numbers:
pcre_copy_named_substring
int pcre_copy_named_substring(
const pcre *code,
const char *subject,
int *ovector,
int stringcount,
const char *stringname,
char *buffer,
int buffersize);
pcre_copy_substring
int pcre_copy_substring(
const char *subject,
int *ovector,
int stringcount,
int stringnumber,
char *buffer,
int buffersize);
pcre_get_named_substring
int pcre_get_named_substring(
const pcre *code,
const char *subject,
int *ovector,
int stringcount,
const char *stringname,
const char **stringptr);
pcre_get_stringnumber
int pcre_get_stringnumber(
const pcre *code,
const char *name);
pcre_get_substring
int pcre_get_substring(
const char *subject,
int *ovector,
int stringcount,
int stringnumber,
const char **stringptr);
pcre_get_substring_list
int pcre_get_substring_list(
const char *subject,
int *ovector,
int stringcount,
const char ***listptr);
pcre_free_substringvoid pcre_free_substring(const char *stringptr); pcre_free_substring_listvoid pcre_free_substring_list(const char **stringptr); pcre_maketablesconst unsigned char *pcre_maketables(void);
pcre_maketables is (optionally) used to build
a set of character tables in the current locale which is then used in
pcre_compile.
setlocale(LC_CTYPE, "fr"); tables = pcre_maketables(); re = pcre_compile(..., tables); pcre_fullinfo
int pcre_fullinfo(
const pcre *code,
const pcre_extra *extra,
int what,
void *where);
Returns information about compiled patterns. (pcre_info is obsolete).
The following whats can be specified:
pcre_info
int pcre_info(
const pcre *code,
int *optptr,
*firstcharptr);
pcre_info is obsolete, use pcre_fullinfo instead.
pcre_configint pcre_config(int what, void *where);
Used to find out which PCRE features are available. These features were determined at compile time of pcre.
The following features can be detected:
pcre_versionchar *pcre_version(void); pcre_mallocvoid *(*pcre_malloc)(size_t); pcre_freevoid (*pcre_free)(void *); pcre_calloutint (*pcre_callout)(pcre_callout_block *); pcre_extra
typedef struct pcre_extra {
unsigned long int flags; /* Bits for which fields are set */
void *study_data; /* Opaque data from pcre_study() */
unsigned long int match_limit; /* Maximum number of calls to match() */
void *callout_data; /* Data passed back in callouts */
const unsigned char *tables; /* Pointer to character tables */
} pcre_extra;
flags allows to set bits. The following bits are specified:
Thanks
Thanks to Robby Villegas who notified me of an error on this page.
|