Pike v8.0 release 1738

Method Regexp.PCRE._pcre()->exec()


Method exec

int|array exec(string subject, void|int startoffset)

Description

Matches the regexp against subject, starting at startoffset if it is given.

If the match is successful, the return value is an array that holds the offsets of all matches:

Elements 0 and 1 have the start and end offsets, respectively, of the match for the whole regexp. The start offset is inclusive while the end is exclusive, i.e. the matching string is subject[res[0]..res[1]-1].

Elements 2 and 3 have the offsets of the first capturing submatch (if any) in the same way, elements 4 and 5 are for the second capturing submatch, etc. If a submatch didn't match anything, the corresponding elements are set to -1. If a submatch has matched successfully several times, the offsets of the last match are returned.

The returned array is always of length 2*n + 1, where n is the total number of capturing submatches in the regexp.

If there is an error, an integer error code is returned:

ERROR.NOMATCH

The subject string did not match the pattern.

ERROR.NULL

Either code or subject was passed as NULL, or ovector was NULL and oversize was not zero.

ERROR.BADOPTION

An unrecognized bit was set in the options argument.

ERROR.BADMAGIC

PCRE stores a 4-byte "magic number" at the start of the compiled code, to catch the case when it is passed a junk pointer. This is the error it gives when the magic number isn't present.

ERROR.UNKNOWN_NODE

While running the pattern match, an unknown item was encountered in the compiled pattern. This error could be caused by a bug in PCRE or by overwriting of the compiled pattern.

ERROR.NOMEMORY

If a pattern contains back references, but the ovector that is passed to pcre_exec() is not big enough to remember the referenced substrings, PCRE gets a block of memory at the start of matching to use for this purpose. If the call via pcre_malloc() fails, this error is given. The memory is freed at the end of matching.

ERROR.NOSUBSTRING

This error is used by the pcre_copy_substring(), pcre_get_substring(), and pcre_get_substring_list() functions (see below). It is never returned by pcre_exec().

ERROR.MATCHLIMIT

The recursion and backtracking limit, as specified by the match_limit field in a pcre_extra structure (or defaulted) was reached. See the description above.

ERROR.CALLOUT

This error is never generated by pcre_exec() itself. It is provided for use by callout functions that want to yield a distinctive error code. See the pcrecallout documentation for details.