📄 tech.notes
字号:
-------------------------OP_PROP and OP_NOTPROP are used for positive and negative matches of a character by testing its Unicode property (the \p and \P escape sequences).Each is followed by two bytes that encode the desired property as a type and a value.Repeats of these items use the OP_TYPESTAR etc. set of opcodes, followed by three bytes: OP_PROP or OP_NOTPROP and then the desired property type and value.Matching literal characters---------------------------The OP_CHAR opcode is followed by a single character that is to be matched casefully. For caseless matching, OP_CHARNC is used. In UTF-8 mode, the character may be more than one byte long. (Earlier versions of PCRE used multi-character strings, but this was changed to allow some new features to be added.)Character classes-----------------If there is only one character, OP_CHAR or OP_CHARNC is used for a positiveclass, and OP_NOT for a negative one (that is, for something like [^a]).However, in UTF-8 mode, the use of OP_NOT applies only to characters withvalues < 128, because OP_NOT is confined to single bytes.Another set of repeating opcodes (OP_NOTSTAR etc.) are used for a repeated,negated, single-character class. The normal ones (OP_STAR etc.) are used for arepeated positive single-character class.When there's more than one character in a class and all the characters are lessthan 256, OP_CLASS is used for a positive class, and OP_NCLASS for a negativeone. In either case, the opcode is followed by a 32-byte bit map containing a 1bit for every character that is acceptable. The bits are counted from the leastsignificant end of each byte.The reason for having both OP_CLASS and OP_NCLASS is so that, in UTF-8 mode,subject characters with values greater than 256 can be handled correctly. ForOP_CLASS they don't match, whereas for OP_NCLASS they do.For classes containing characters with values > 255, OP_XCLASS is used. Itoptionally uses a bit map (if any characters lie within it), followed by a listof pairs and single characters. There is a flag character than indicateswhether it's a positive or a negative class.Back references---------------OP_REF is followed by two bytes containing the reference number.Repeating character classes and back references-----------------------------------------------Single-character classes are handled specially (see above). This sectionapplies to OP_CLASS and OP_REF. In both cases, the repeat information followsthe base item. The matching code looks at the following opcode to see if it isone of OP_CRSTAR OP_CRMINSTAR OP_CRPLUS OP_CRMINPLUS OP_CRQUERY OP_CRMINQUERY OP_CRRANGE OP_CRMINRANGEAll but the last two are just single-byte items. The others are followed byfour bytes of data, comprising the minimum and maximum repeat counts. There are no special possessive opcodes for these repeats; a possessive repeat is compiled into an atomic group.Brackets and alternation------------------------A pair of non-capturing (round) brackets is wrapped round each expression atcompile time, so alternation always happens in the context of brackets.[Note for North Americans: "bracket" to some English speakers, includingmyself, can be round, square, curly, or pointy. Hence this usage.]Non-capturing brackets use the opcode OP_BRA. Originally PCRE was limited to 99capturing brackets and it used a different opcode for each one. From release3.5, the limit was removed by putting the bracket number into the data forhigher-numbered brackets. From release 7.0 all capturing brackets are handledthis way, using the single opcode OP_CBRA.A bracket opcode is followed by LINK_SIZE bytes which give the offset to thenext alternative OP_ALT or, if there aren't any branches, to the matchingOP_KET opcode. Each OP_ALT is followed by LINK_SIZE bytes giving the offset tothe next one, or to the OP_KET opcode. For capturing brackets, the bracket number immediately follows the offset, always as a 2-byte item.OP_KET is used for subpatterns that do not repeat indefinitely, whileOP_KETRMIN and OP_KETRMAX are used for indefinite repetitions, minimally ormaximally respectively. All three are followed by LINK_SIZE bytes giving (as apositive number) the offset back to the matching bracket opcode.If a subpattern is quantified such that it is permitted to match zero times, itis preceded by one of OP_BRAZERO or OP_BRAMINZERO. These are single-byteopcodes which tell the matcher that skipping this subpattern entirely is avalid branch.A subpattern with an indefinite maximum repetition is replicated in thecompiled data its minimum number of times (or once with OP_BRAZERO if theminimum is zero), with the final copy terminating with OP_KETRMIN or OP_KETRMAXas appropriate.A subpattern with a bounded maximum repetition is replicated in a nestedfashion up to the maximum number of times, with OP_BRAZERO or OP_BRAMINZERObefore each replication after the minimum, so that, for example, (abc){2,5} iscompiled as (abc)(abc)((abc)((abc)(abc)?)?)?, except that each bracketed group has the same number.When a repeated subpattern has an unbounded upper limit, it is checked to see whether it could match an empty string. If this is the case, the opcode in the final replication is changed to OP_SBRA or OP_SCBRA. This tells the matcherthat it needs to check for matching an empty string when it hits OP_KETRMIN orOP_KETRMAX, and if so, to break the loop.Assertions----------Forward assertions are just like other subpatterns, but starting with one ofthe opcodes OP_ASSERT or OP_ASSERT_NOT. Backward assertions use the opcodesOP_ASSERTBACK and OP_ASSERTBACK_NOT, and the first opcode inside the assertionis OP_REVERSE, followed by a two byte count of the number of characters to moveback the pointer in the subject string. When operating in UTF-8 mode, the countis a character count rather than a byte count. A separate count is present ineach alternative of a lookbehind assertion, allowing them to have differentfixed lengths.Once-only (atomic) subpatterns------------------------------These are also just like other subpatterns, but they start with the opcodeOP_ONCE. The check for matching an empty string in an unbounded repeat is handled entirely at runtime, so there is just this one opcode.Conditional subpatterns-----------------------These are like other subpatterns, but they start with the opcode OP_COND, orOP_SCOND for one that might match an empty string in an unbounded repeat. Ifthe condition is a back reference, this is stored at the start of thesubpattern using the opcode OP_CREF followed by two bytes containing thereference number. If the condition is "in recursion" (coded as "(?(R)"), or "inrecursion of group x" (coded as "(?(Rx)"), the group number is stored at thestart of the subpattern using the opcode OP_RREF, and a value of zero for "thewhole pattern". For a DEFINE condition, just the single byte OP_DEF is used (ithas no associated data). Otherwise, a conditional subpattern always starts withone of the assertions.Recursion---------Recursion either matches the current regex, or some subexpression. The opcodeOP_RECURSE is followed by an value which is the offset to the starting bracketfrom the start of the whole pattern. From release 6.5, OP_RECURSE is automatically wrapped inside OP_ONCE brackets (because otherwise some patterns broke it). OP_RECURSE is also used for "subroutine" calls, even though they are not strictly a recursion.Callout-------OP_CALLOUT is followed by one byte of data that holds a callout number in therange 0 to 254 for manual callouts, or 255 for an automatic callout. In both cases there follows a two-byte value giving the offset in the pattern to thestart of the following item, and another two-byte item giving the length of thenext item.Changing options----------------If any of the /i, /m, or /s options are changed within a pattern, an OP_OPTopcode is compiled, followed by one byte containing the new settings of theseflags. If there are several alternatives, there is an occurrence of OP_OPT atthe start of all those following the first options change, to set appropriateoptions for the start of the alternative. Immediately after the end of thegroup there is another such item to reset the flags to their previous values. Achange of flag right at the very start of the pattern can be handled entirelyat compile time, and so does not cause anything to be put into the compileddata.Philip HazelNovember 2006
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -