Parent Directory
|
Revision Log
|
Patch
revision 1220 by ph10, Wed Oct 31 17:42:29 2012 UTC | revision 1221 by ph10, Sun Nov 11 20:27:03 2012 UTC | |
---|---|---|
# | Line 98 SECURITY CONSIDERATIONS | Line 98 SECURITY CONSIDERATIONS |
98 | supply arbitrary patterns for compilation, you should be aware of a | supply arbitrary patterns for compilation, you should be aware of a |
99 | feature that allows users to turn on UTF support from within a pattern, | feature that allows users to turn on UTF support from within a pattern, |
100 | provided that PCRE was built with UTF support. For example, an 8-bit | provided that PCRE was built with UTF support. For example, an 8-bit |
101 | pattern that begins with "(*UTF8)" turns on UTF-8 mode. This causes | pattern that begins with "(*UTF8)" or "(*UTF)" turns on UTF-8 mode, |
102 | both the pattern and any data against which it is matched to be checked | which interprets patterns and subjects as strings of UTF-8 characters |
103 | for UTF-8 validity. If the data string is very long, such a check might | instead of individual 8-bit characters. This causes both the pattern |
104 | use sufficiently many resources as to cause your application to lose | and any data against which it is matched to be checked for UTF-8 valid- |
105 | performance. | ity. If the data string is very long, such a check might use suffi- |
106 | ciently many resources as to cause your application to lose perfor- | |
107 | mance. | |
108 | ||
109 | The best way of guarding against this possibility is to use the | The best way of guarding against this possibility is to use the |
110 | pcre_fullinfo() function to check the compiled pattern's options for | pcre_fullinfo() function to check the compiled pattern's options for |
# | Line 172 AUTHOR | Line 174 AUTHOR |
174 | ||
175 | REVISION | REVISION |
176 | ||
177 | Last updated: 30 October 2012 | Last updated: 11 November 2012 |
178 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
179 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
180 | ||
181 | ||
182 | PCRE(3) PCRE(3) | PCRE(3) PCRE(3) |
183 | ||
184 | ||
# | Line 315 THE PCRE 16-BIT LIBRARY | Line 317 THE PCRE 16-BIT LIBRARY |
317 | THE HEADER FILE | THE HEADER FILE |
318 | ||
319 | There is only one header file, pcre.h. It contains prototypes for all | There is only one header file, pcre.h. It contains prototypes for all |
320 | the functions in both libraries, as well as definitions of flags, | the functions in all libraries, as well as definitions of flags, struc- |
321 | structures, error codes, etc. | tures, error codes, etc. |
322 | ||
323 | ||
324 | THE LIBRARY NAME | THE LIBRARY NAME |
# | Line 334 STRING TYPES | Line 336 STRING TYPES |
336 | PCRE_UCHAR16 specifies an appropriate data type, and PCRE_SPTR16 is | PCRE_UCHAR16 specifies an appropriate data type, and PCRE_SPTR16 is |
337 | defined as "const PCRE_UCHAR16 *". In very many environments, "short | defined as "const PCRE_UCHAR16 *". In very many environments, "short |
338 | int" is a 16-bit data type. When PCRE is built, it defines PCRE_UCHAR16 | int" is a 16-bit data type. When PCRE is built, it defines PCRE_UCHAR16 |
339 | as "short int", but checks that it really is a 16-bit data type. If it | as "unsigned short int", but checks that it really is a 16-bit data |
340 | is not, the build fails with an error message telling the maintainer to | type. If it is not, the build fails with an error message telling the |
341 | modify the definition appropriately. | maintainer to modify the definition appropriately. |
342 | ||
343 | ||
344 | STRUCTURE TYPES | STRUCTURE TYPES |
# | Line 501 AUTHOR | Line 503 AUTHOR |
503 | ||
504 | REVISION | REVISION |
505 | ||
506 | Last updated: 14 April 2012 | Last updated: 08 November 2012 |
507 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
508 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
509 | ||
510 | ||
511 | PCRE(3) PCRE(3) | PCRE(3) PCRE(3) |
512 | ||
513 | ||
# | Line 644 THE PCRE 32-BIT LIBRARY | Line 646 THE PCRE 32-BIT LIBRARY |
646 | THE HEADER FILE | THE HEADER FILE |
647 | ||
648 | There is only one header file, pcre.h. It contains prototypes for all | There is only one header file, pcre.h. It contains prototypes for all |
649 | the functions in both libraries, as well as definitions of flags, | the functions in all libraries, as well as definitions of flags, struc- |
650 | structures, error codes, etc. | tures, error codes, etc. |
651 | ||
652 | ||
653 | THE LIBRARY NAME | THE LIBRARY NAME |
# | Line 827 AUTHOR | Line 829 AUTHOR |
829 | ||
830 | REVISION | REVISION |
831 | ||
832 | Last updated: 24 June 2012 | Last updated: 08 November 2012 |
833 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
834 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
835 | ||
836 | ||
837 | PCREBUILD(3) PCREBUILD(3) | PCREBUILD(3) PCREBUILD(3) |
838 | ||
839 | ||
# | Line 1320 REVISION | Line 1322 REVISION |
1322 | Last updated: 30 October 2012 | Last updated: 30 October 2012 |
1323 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
1324 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
1325 | ||
1326 | ||
1327 | PCREMATCHING(3) PCREMATCHING(3) | PCREMATCHING(3) PCREMATCHING(3) |
1328 | ||
1329 | ||
# | Line 1529 REVISION | Line 1531 REVISION |
1531 | Last updated: 08 January 2012 | Last updated: 08 January 2012 |
1532 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
1533 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
1534 | ||
1535 | ||
1536 | PCREAPI(3) PCREAPI(3) | PCREAPI(3) PCREAPI(3) |
1537 | ||
1538 | ||
# | Line 1642 PCRE NATIVE API INDIRECTED FUNCTIONS | Line 1644 PCRE NATIVE API INDIRECTED FUNCTIONS |
1644 | int (*pcre_callout)(pcre_callout_block *); | int (*pcre_callout)(pcre_callout_block *); |
1645 | ||
1646 | ||
1647 | PCRE 8-BIT, 16-BIT AND 32-BIT LIBRARIES | PCRE 8-BIT, 16-BIT, AND 32-BIT LIBRARIES |
1648 | ||
1649 | From release 8.30, PCRE can be compiled as a library for handling | As well as support for 8-bit character strings, PCRE also supports |
1650 | 16-bit character strings as well as, or instead of, the original | 16-bit strings (from release 8.30) and 32-bit strings (from release |
1651 | library that handles 8-bit character strings. From release 8.32, PCRE | 8.32), by means of two additional libraries. They can be built as well |
1652 | can also be compiled as a library for handling 32-bit character | as, or instead of, the 8-bit library. To avoid too much complication, |
1653 | strings. To avoid too much complication, this document describes the | this document describes the 8-bit versions of the functions, with only |
1654 | 8-bit versions of the functions, with only occasional references to the | occasional references to the 16-bit and 32-bit libraries. |
1655 | 16-bit and 32-bit libraries. | |
1656 | The 16-bit and 32-bit functions operate in the same way as their 8-bit | |
1657 | The 16-bit and 32-bit functions operate in the same way as their 8-bit | counterparts; they just use different data types for their arguments |
1658 | counterparts; they just use different data types for their arguments | and results, and their names start with pcre16_ or pcre32_ instead of |
1659 | and results, and their names start with pcre16_ or pcre32_ instead of | pcre_. For every option that has UTF8 in its name (for example, |
1660 | pcre_. For every option that has UTF8 in its name (for example, | PCRE_UTF8), there are corresponding 16-bit and 32-bit names with UTF8 |
PCRE_UTF8), there are corresponding 16-bit and 32-bit names with UTF8 | ||
1661 | replaced by UTF16 or UTF32, respectively. This facility is in fact just | replaced by UTF16 or UTF32, respectively. This facility is in fact just |
1662 | cosmetic; the 16-bit and 32-bit option names define the same bit val- | cosmetic; the 16-bit and 32-bit option names define the same bit val- |
1663 | ues. | ues. |
1664 | ||
1665 | References to bytes and UTF-8 in this document should be read as refer- | References to bytes and UTF-8 in this document should be read as refer- |
1666 | ences to 16-bit data quantities and UTF-16 when using the 16-bit | ences to 16-bit data quantities and UTF-16 when using the 16-bit |
1667 | library, or 32-bit data quantities and UTF-32 when using the 32-bit | library, or 32-bit data quantities and UTF-32 when using the 32-bit |
1668 | library, unless specified otherwise. More details of the specific dif- | library, unless specified otherwise. More details of the specific dif- |
1669 | ferences for the 16-bit and 32-bit libraries are given in the pcre16 | ferences for the 16-bit and 32-bit libraries are given in the pcre16 |
1670 | and pcre32 pages. | and pcre32 pages. |
1671 | ||
1672 | ||
1673 | PCRE API OVERVIEW | PCRE API OVERVIEW |
1674 | ||
1675 | PCRE has its own native API, which is described in this document. There | PCRE has its own native API, which is described in this document. There |
1676 | are also some wrapper functions (for the 8-bit library only) that cor- | are also some wrapper functions (for the 8-bit library only) that cor- |
1677 | respond to the POSIX regular expression API, but they do not give | respond to the POSIX regular expression API, but they do not give |
1678 | access to all the functionality. They are described in the pcreposix | access to all the functionality. They are described in the pcreposix |
1679 | documentation. Both of these APIs define a set of C function calls. A | documentation. Both of these APIs define a set of C function calls. A |
1680 | C++ wrapper (again for the 8-bit library only) is also distributed with | C++ wrapper (again for the 8-bit library only) is also distributed with |
1681 | PCRE. It is documented in the pcrecpp page. | PCRE. It is documented in the pcrecpp page. |
1682 | ||
1683 | The native API C function prototypes are defined in the header file | The native API C function prototypes are defined in the header file |
1684 | pcre.h, and on Unix-like systems the (8-bit) library itself is called | pcre.h, and on Unix-like systems the (8-bit) library itself is called |
1685 | libpcre. It can normally be accessed by adding -lpcre to the command | libpcre. It can normally be accessed by adding -lpcre to the command |
1686 | for linking an application that uses PCRE. The header file defines the | for linking an application that uses PCRE. The header file defines the |
1687 | macros PCRE_MAJOR and PCRE_MINOR to contain the major and minor release | macros PCRE_MAJOR and PCRE_MINOR to contain the major and minor release |
1688 | numbers for the library. Applications can use these to include support | numbers for the library. Applications can use these to include support |
1689 | for different releases of PCRE. | for different releases of PCRE. |
1690 | ||
1691 | In a Windows environment, if you want to statically link an application | In a Windows environment, if you want to statically link an application |
1692 | program against a non-dll pcre.a file, you must define PCRE_STATIC | program against a non-dll pcre.a file, you must define PCRE_STATIC |
1693 | before including pcre.h or pcrecpp.h, because otherwise the pcre_mal- | before including pcre.h or pcrecpp.h, because otherwise the pcre_mal- |
1694 | loc() and pcre_free() exported functions will be declared | loc() and pcre_free() exported functions will be declared |
1695 | __declspec(dllimport), with unwanted results. | __declspec(dllimport), with unwanted results. |
1696 | ||
1697 | The functions pcre_compile(), pcre_compile2(), pcre_study(), and | The functions pcre_compile(), pcre_compile2(), pcre_study(), and |
1698 | pcre_exec() are used for compiling and matching regular expressions in | pcre_exec() are used for compiling and matching regular expressions in |
1699 | a Perl-compatible manner. A sample program that demonstrates the sim- | a Perl-compatible manner. A sample program that demonstrates the sim- |
1700 | plest way of using them is provided in the file called pcredemo.c in | plest way of using them is provided in the file called pcredemo.c in |
1701 | the PCRE source distribution. A listing of this program is given in the | the PCRE source distribution. A listing of this program is given in the |
1702 | pcredemo documentation, and the pcresample documentation describes how | pcredemo documentation, and the pcresample documentation describes how |
1703 | to compile and run it. | to compile and run it. |
1704 | ||
1705 | Just-in-time compiler support is an optional feature of PCRE that can | Just-in-time compiler support is an optional feature of PCRE that can |
1706 | be built in appropriate hardware environments. It greatly speeds up the | be built in appropriate hardware environments. It greatly speeds up the |
1707 | matching performance of many patterns. Simple programs can easily | matching performance of many patterns. Simple programs can easily |
1708 | request that it be used if available, by setting an option that is | request that it be used if available, by setting an option that is |
1709 | ignored when it is not relevant. More complicated programs might need | ignored when it is not relevant. More complicated programs might need |
1710 | to make use of the functions pcre_jit_stack_alloc(), | to make use of the functions pcre_jit_stack_alloc(), |
1711 | pcre_jit_stack_free(), and pcre_assign_jit_stack() in order to control | pcre_jit_stack_free(), and pcre_assign_jit_stack() in order to control |
1712 | the JIT code's memory usage. | the JIT code's memory usage. |
1713 | ||
1714 | From release 8.32 there is also a direct interface for JIT execution, | From release 8.32 there is also a direct interface for JIT execution, |
1715 | which gives improved performance. The JIT-specific functions are dis- | which gives improved performance. The JIT-specific functions are dis- |
1716 | cussed in the pcrejit documentation. | cussed in the pcrejit documentation. |
1717 | ||
1718 | A second matching function, pcre_dfa_exec(), which is not Perl-compati- | A second matching function, pcre_dfa_exec(), which is not Perl-compati- |
1719 | ble, is also provided. This uses a different algorithm for the match- | ble, is also provided. This uses a different algorithm for the match- |
1720 | ing. The alternative algorithm finds all possible matches (at a given | ing. The alternative algorithm finds all possible matches (at a given |
1721 | point in the subject), and scans the subject just once (unless there | point in the subject), and scans the subject just once (unless there |
1722 | are lookbehind assertions). However, this algorithm does not return | are lookbehind assertions). However, this algorithm does not return |
1723 | captured substrings. A description of the two matching algorithms and | captured substrings. A description of the two matching algorithms and |
1724 | their advantages and disadvantages is given in the pcrematching docu- | their advantages and disadvantages is given in the pcrematching docu- |
1725 | mentation. | mentation. |
1726 | ||
1727 | In addition to the main compiling and matching functions, there are | In addition to the main compiling and matching functions, there are |
1728 | convenience functions for extracting captured substrings from a subject | convenience functions for extracting captured substrings from a subject |
1729 | string that is matched by pcre_exec(). They are: | string that is matched by pcre_exec(). They are: |
1730 | ||
# | Line 1738 PCRE API OVERVIEW | Line 1739 PCRE API OVERVIEW |
1739 | pcre_free_substring() and pcre_free_substring_list() are also provided, | pcre_free_substring() and pcre_free_substring_list() are also provided, |
1740 | to free the memory used for extracted strings. | to free the memory used for extracted strings. |
1741 | ||
1742 | The function pcre_maketables() is used to build a set of character | The function pcre_maketables() is used to build a set of character |
1743 | tables in the current locale for passing to pcre_compile(), | tables in the current locale for passing to pcre_compile(), |
1744 | pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is | pcre_exec(), or pcre_dfa_exec(). This is an optional facility that is |
1745 | provided for specialist use. Most commonly, no special tables are | provided for specialist use. Most commonly, no special tables are |
1746 | passed, in which case internal tables that are generated when PCRE is | passed, in which case internal tables that are generated when PCRE is |
1747 | built are used. | built are used. |
1748 | ||
1749 | The function pcre_fullinfo() is used to find out information about a | The function pcre_fullinfo() is used to find out information about a |
1750 | compiled pattern. The function pcre_version() returns a pointer to a | compiled pattern. The function pcre_version() returns a pointer to a |
1751 | string containing the version of PCRE and its date of release. | string containing the version of PCRE and its date of release. |
1752 | ||
1753 | The function pcre_refcount() maintains a reference count in a data | The function pcre_refcount() maintains a reference count in a data |
1754 | block containing a compiled pattern. This is provided for the benefit | block containing a compiled pattern. This is provided for the benefit |
1755 | of object-oriented applications. | of object-oriented applications. |
1756 | ||
1757 | The global variables pcre_malloc and pcre_free initially contain the | The global variables pcre_malloc and pcre_free initially contain the |
1758 | entry points of the standard malloc() and free() functions, respec- | entry points of the standard malloc() and free() functions, respec- |
1759 | tively. PCRE calls the memory management functions via these variables, | tively. PCRE calls the memory management functions via these variables, |
1760 | so a calling program can replace them if it wishes to intercept the | so a calling program can replace them if it wishes to intercept the |
1761 | calls. This should be done before calling any PCRE functions. | calls. This should be done before calling any PCRE functions. |
1762 | ||
1763 | The global variables pcre_stack_malloc and pcre_stack_free are also | The global variables pcre_stack_malloc and pcre_stack_free are also |
1764 | indirections to memory management functions. These special functions | indirections to memory management functions. These special functions |
1765 | are used only when PCRE is compiled to use the heap for remembering | are used only when PCRE is compiled to use the heap for remembering |
1766 | data, instead of recursive function calls, when running the pcre_exec() | data, instead of recursive function calls, when running the pcre_exec() |
1767 | function. See the pcrebuild documentation for details of how to do | function. See the pcrebuild documentation for details of how to do |
1768 | this. It is a non-standard way of building PCRE, for use in environ- | this. It is a non-standard way of building PCRE, for use in environ- |
1769 | ments that have limited stacks. Because of the greater use of memory | ments that have limited stacks. Because of the greater use of memory |
1770 | management, it runs more slowly. Separate functions are provided so | management, it runs more slowly. Separate functions are provided so |
1771 | that special-purpose external code can be used for this case. When | that special-purpose external code can be used for this case. When |
1772 | used, these functions are always called in a stack-like manner (last | used, these functions are always called in a stack-like manner (last |
1773 | obtained, first freed), and always for memory blocks of the same size. | obtained, first freed), and always for memory blocks of the same size. |
1774 | There is a discussion about PCRE's stack usage in the pcrestack docu- | There is a discussion about PCRE's stack usage in the pcrestack docu- |
1775 | mentation. | mentation. |
1776 | ||
1777 | The global variable pcre_callout initially contains NULL. It can be set | The global variable pcre_callout initially contains NULL. It can be set |
1778 | by the caller to a "callout" function, which PCRE will then call at | by the caller to a "callout" function, which PCRE will then call at |
1779 | specified points during a matching operation. Details are given in the | specified points during a matching operation. Details are given in the |
1780 | pcrecallout documentation. | pcrecallout documentation. |
1781 | ||
1782 | ||
1783 | NEWLINES | NEWLINES |
1784 | ||
1785 | PCRE supports five different conventions for indicating line breaks in | PCRE supports five different conventions for indicating line breaks in |
1786 | strings: a single CR (carriage return) character, a single LF (line- | strings: a single CR (carriage return) character, a single LF (line- |
1787 | feed) character, the two-character sequence CRLF, any of the three pre- | feed) character, the two-character sequence CRLF, any of the three pre- |
1788 | ceding, or any Unicode newline sequence. The Unicode newline sequences | ceding, or any Unicode newline sequence. The Unicode newline sequences |
1789 | are the three just mentioned, plus the single characters VT (vertical | are the three just mentioned, plus the single characters VT (vertical |
1790 | tab, U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line | tab, U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line |
1791 | separator, U+2028), and PS (paragraph separator, U+2029). | separator, U+2028), and PS (paragraph separator, U+2029). |
1792 | ||
1793 | Each of the first three conventions is used by at least one operating | Each of the first three conventions is used by at least one operating |
1794 | system as its standard newline sequence. When PCRE is built, a default | system as its standard newline sequence. When PCRE is built, a default |
1795 | can be specified. The default default is LF, which is the Unix stan- | can be specified. The default default is LF, which is the Unix stan- |
1796 | dard. When PCRE is run, the default can be overridden, either when a | dard. When PCRE is run, the default can be overridden, either when a |
1797 | pattern is compiled, or when it is matched. | pattern is compiled, or when it is matched. |
1798 | ||
1799 | At compile time, the newline convention can be specified by the options | At compile time, the newline convention can be specified by the options |
1800 | argument of pcre_compile(), or it can be specified by special text at | argument of pcre_compile(), or it can be specified by special text at |
1801 | the start of the pattern itself; this overrides any other settings. See | the start of the pattern itself; this overrides any other settings. See |
1802 | the pcrepattern page for details of the special character sequences. | the pcrepattern page for details of the special character sequences. |
1803 | ||
1804 | In the PCRE documentation the word "newline" is used to mean "the char- | In the PCRE documentation the word "newline" is used to mean "the char- |
1805 | acter or pair of characters that indicate a line break". The choice of | acter or pair of characters that indicate a line break". The choice of |
1806 | newline convention affects the handling of the dot, circumflex, and | newline convention affects the handling of the dot, circumflex, and |
1807 | dollar metacharacters, the handling of #-comments in /x mode, and, when | dollar metacharacters, the handling of #-comments in /x mode, and, when |
1808 | CRLF is a recognized line ending sequence, the match position advance- | CRLF is a recognized line ending sequence, the match position advance- |
1809 | ment for a non-anchored pattern. There is more detail about this in the | ment for a non-anchored pattern. There is more detail about this in the |
1810 | section on pcre_exec() options below. | section on pcre_exec() options below. |
1811 | ||
1812 | The choice of newline convention does not affect the interpretation of | The choice of newline convention does not affect the interpretation of |
1813 | the \n or \r escape sequences, nor does it affect what \R matches, | the \n or \r escape sequences, nor does it affect what \R matches, |
1814 | which is controlled in a similar way, but by separate options. | which is controlled in a similar way, but by separate options. |
1815 | ||
1816 | ||
1817 | MULTITHREADING | MULTITHREADING |
1818 | ||
1819 | The PCRE functions can be used in multi-threading applications, with | The PCRE functions can be used in multi-threading applications, with |
1820 | the proviso that the memory management functions pointed to by | the proviso that the memory management functions pointed to by |
1821 | pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the | pcre_malloc, pcre_free, pcre_stack_malloc, and pcre_stack_free, and the |
1822 | callout function pointed to by pcre_callout, are shared by all threads. | callout function pointed to by pcre_callout, are shared by all threads. |
1823 | ||
1824 | The compiled form of a regular expression is not altered during match- | The compiled form of a regular expression is not altered during match- |
1825 | ing, so the same compiled pattern can safely be used by several threads | ing, so the same compiled pattern can safely be used by several threads |
1826 | at once. | at once. |
1827 | ||
1828 | If the just-in-time optimization feature is being used, it needs sepa- | If the just-in-time optimization feature is being used, it needs sepa- |
1829 | rate memory stack areas for each thread. See the pcrejit documentation | rate memory stack areas for each thread. See the pcrejit documentation |
1830 | for more details. | for more details. |
1831 | ||
1832 | ||
1833 | SAVING PRECOMPILED PATTERNS FOR LATER USE | SAVING PRECOMPILED PATTERNS FOR LATER USE |
1834 | ||
1835 | The compiled form of a regular expression can be saved and re-used at a | The compiled form of a regular expression can be saved and re-used at a |
1836 | later time, possibly by a different program, and even on a host other | later time, possibly by a different program, and even on a host other |
1837 | than the one on which it was compiled. Details are given in the | than the one on which it was compiled. Details are given in the |
1838 | pcreprecompile documentation, which includes a description of the | pcreprecompile documentation, which includes a description of the |
1839 | pcre_pattern_to_host_byte_order() function. However, compiling a regu- | pcre_pattern_to_host_byte_order() function. However, compiling a regu- |
1840 | lar expression with one version of PCRE for use with a different ver- | lar expression with one version of PCRE for use with a different ver- |
1841 | sion is not guaranteed to work and may cause crashes. | sion is not guaranteed to work and may cause crashes. |
1842 | ||
1843 | ||
# | Line 1844 CHECKING BUILD-TIME OPTIONS | Line 1845 CHECKING BUILD-TIME OPTIONS |
1845 | ||
1846 | int pcre_config(int what, void *where); | int pcre_config(int what, void *where); |
1847 | ||
1848 | The function pcre_config() makes it possible for a PCRE client to dis- | The function pcre_config() makes it possible for a PCRE client to dis- |
1849 | cover which optional features have been compiled into the PCRE library. | cover which optional features have been compiled into the PCRE library. |
1850 | The pcrebuild documentation has more details about these optional fea- | The pcrebuild documentation has more details about these optional fea- |
1851 | tures. | tures. |
1852 | ||
1853 | The first argument for pcre_config() is an integer, specifying which | The first argument for pcre_config() is an integer, specifying which |
1854 | information is required; the second argument is a pointer to a variable | information is required; the second argument is a pointer to a variable |
1855 | into which the information is placed. The returned value is zero on | into which the information is placed. The returned value is zero on |
1856 | success, or the negative error code PCRE_ERROR_BADOPTION if the value | success, or the negative error code PCRE_ERROR_BADOPTION if the value |
1857 | in the first argument is not recognized. The following information is | in the first argument is not recognized. The following information is |
1858 | available: | available: |
1859 | ||
1860 | PCRE_CONFIG_UTF8 | PCRE_CONFIG_UTF8 |
1861 | ||
1862 | The output is an integer that is set to one if UTF-8 support is avail- | The output is an integer that is set to one if UTF-8 support is avail- |
1863 | able; otherwise it is set to zero. This value should normally be given | able; otherwise it is set to zero. This value should normally be given |
1864 | to the 8-bit version of this function, pcre_config(). If it is given to | to the 8-bit version of this function, pcre_config(). If it is given to |
1865 | the 16-bit or 32-bit version of this function, the result is | the 16-bit or 32-bit version of this function, the result is |
1866 | PCRE_ERROR_BADOPTION. | PCRE_ERROR_BADOPTION. |
1867 | ||
1868 | PCRE_CONFIG_UTF16 | PCRE_CONFIG_UTF16 |
1869 | ||
1870 | The output is an integer that is set to one if UTF-16 support is avail- | The output is an integer that is set to one if UTF-16 support is avail- |
1871 | able; otherwise it is set to zero. This value should normally be given | able; otherwise it is set to zero. This value should normally be given |
1872 | to the 16-bit version of this function, pcre16_config(). If it is given | to the 16-bit version of this function, pcre16_config(). If it is given |
1873 | to the 8-bit or 32-bit version of this function, the result is | to the 8-bit or 32-bit version of this function, the result is |
1874 | PCRE_ERROR_BADOPTION. | PCRE_ERROR_BADOPTION. |
1875 | ||
1876 | PCRE_CONFIG_UTF32 | PCRE_CONFIG_UTF32 |
1877 | ||
1878 | The output is an integer that is set to one if UTF-32 support is avail- | The output is an integer that is set to one if UTF-32 support is avail- |
1879 | able; otherwise it is set to zero. This value should normally be given | able; otherwise it is set to zero. This value should normally be given |
1880 | to the 32-bit version of this function, pcre32_config(). If it is given | to the 32-bit version of this function, pcre32_config(). If it is given |
1881 | to the 8-bit or 16-bit version of this function, the result is | to the 8-bit or 16-bit version of this function, the result is |
1882 | PCRE_ERROR_BADOPTION. | PCRE_ERROR_BADOPTION. |
1883 | ||
1884 | PCRE_CONFIG_UNICODE_PROPERTIES | PCRE_CONFIG_UNICODE_PROPERTIES |
1885 | ||
1886 | The output is an integer that is set to one if support for Unicode | The output is an integer that is set to one if support for Unicode |
1887 | character properties is available; otherwise it is set to zero. | character properties is available; otherwise it is set to zero. |
1888 | ||
1889 | PCRE_CONFIG_JIT | PCRE_CONFIG_JIT |
# | Line 1892 CHECKING BUILD-TIME OPTIONS | Line 1893 CHECKING BUILD-TIME OPTIONS |
1893 | ||
1894 | PCRE_CONFIG_JITTARGET | PCRE_CONFIG_JITTARGET |
1895 | ||
1896 | The output is a pointer to a zero-terminated "const char *" string. If | The output is a pointer to a zero-terminated "const char *" string. If |
1897 | JIT support is available, the string contains the name of the architec- | JIT support is available, the string contains the name of the architec- |
1898 | ture for which the JIT compiler is configured, for example "x86 32bit | ture for which the JIT compiler is configured, for example "x86 32bit |
1899 | (little endian + unaligned)". If JIT support is not available, the | (little endian + unaligned)". If JIT support is not available, the |
1900 | result is NULL. | result is NULL. |
1901 | ||
1902 | PCRE_CONFIG_NEWLINE | PCRE_CONFIG_NEWLINE |
1903 | ||
1904 | The output is an integer whose value specifies the default character | The output is an integer whose value specifies the default character |
1905 | sequence that is recognized as meaning "newline". The values that are | sequence that is recognized as meaning "newline". The values that are |
1906 | supported in ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338 | supported in ASCII/Unicode environments are: 10 for LF, 13 for CR, 3338 |
1907 | for CRLF, -2 for ANYCRLF, and -1 for ANY. In EBCDIC environments, CR, | for CRLF, -2 for ANYCRLF, and -1 for ANY. In EBCDIC environments, CR, |
1908 | ANYCRLF, and ANY yield the same values. However, the value for LF is | ANYCRLF, and ANY yield the same values. However, the value for LF is |
1909 | normally 21, though some EBCDIC environments use 37. The corresponding | normally 21, though some EBCDIC environments use 37. The corresponding |
1910 | values for CRLF are 3349 and 3365. The default should normally corre- | values for CRLF are 3349 and 3365. The default should normally corre- |
1911 | spond to the standard sequence for your operating system. | spond to the standard sequence for your operating system. |
1912 | ||
1913 | PCRE_CONFIG_BSR | PCRE_CONFIG_BSR |
1914 | ||
1915 | The output is an integer whose value indicates what character sequences | The output is an integer whose value indicates what character sequences |
1916 | the \R escape sequence matches by default. A value of 0 means that \R | the \R escape sequence matches by default. A value of 0 means that \R |
1917 | matches any Unicode line ending sequence; a value of 1 means that \R | matches any Unicode line ending sequence; a value of 1 means that \R |
1918 | matches only CR, LF, or CRLF. The default can be overridden when a pat- | matches only CR, LF, or CRLF. The default can be overridden when a pat- |
1919 | tern is compiled or matched. | tern is compiled or matched. |
1920 | ||
1921 | PCRE_CONFIG_LINK_SIZE | PCRE_CONFIG_LINK_SIZE |
1922 | ||
1923 | The output is an integer that contains the number of bytes used for | The output is an integer that contains the number of bytes used for |
1924 | internal linkage in compiled regular expressions. For the 8-bit | internal linkage in compiled regular expressions. For the 8-bit |
1925 | library, the value can be 2, 3, or 4. For the 16-bit library, the value | library, the value can be 2, 3, or 4. For the 16-bit library, the value |
1926 | is either 2 or 4 and is still a number of bytes. For the 32-bit | is either 2 or 4 and is still a number of bytes. For the 32-bit |
1927 | library, the value is either 2 or 4 and is still a number of bytes. The | library, the value is either 2 or 4 and is still a number of bytes. The |
1928 | default value of 2 is sufficient for all but the most massive patterns, | default value of 2 is sufficient for all but the most massive patterns, |
1929 | since it allows the compiled pattern to be up to 64K in size. Larger | since it allows the compiled pattern to be up to 64K in size. Larger |
1930 | values allow larger regular expressions to be compiled, at the expense | values allow larger regular expressions to be compiled, at the expense |
1931 | of slower matching. | of slower matching. |
1932 | ||
1933 | PCRE_CONFIG_POSIX_MALLOC_THRESHOLD | PCRE_CONFIG_POSIX_MALLOC_THRESHOLD |
1934 | ||
1935 | The output is an integer that contains the threshold above which the | The output is an integer that contains the threshold above which the |
1936 | POSIX interface uses malloc() for output vectors. Further details are | POSIX interface uses malloc() for output vectors. Further details are |
1937 | given in the pcreposix documentation. | given in the pcreposix documentation. |
1938 | ||
1939 | PCRE_CONFIG_MATCH_LIMIT | PCRE_CONFIG_MATCH_LIMIT |
1940 | ||
1941 | The output is a long integer that gives the default limit for the num- | The output is a long integer that gives the default limit for the num- |
1942 | ber of internal matching function calls in a pcre_exec() execution. | ber of internal matching function calls in a pcre_exec() execution. |
1943 | Further details are given with pcre_exec() below. | Further details are given with pcre_exec() below. |
1944 | ||
1945 | PCRE_CONFIG_MATCH_LIMIT_RECURSION | PCRE_CONFIG_MATCH_LIMIT_RECURSION |
1946 | ||
1947 | The output is a long integer that gives the default limit for the depth | The output is a long integer that gives the default limit for the depth |
1948 | of recursion when calling the internal matching function in a | of recursion when calling the internal matching function in a |
1949 | pcre_exec() execution. Further details are given with pcre_exec() | pcre_exec() execution. Further details are given with pcre_exec() |
1950 | below. | below. |
1951 | ||
1952 | PCRE_CONFIG_STACKRECURSE | PCRE_CONFIG_STACKRECURSE |
1953 | ||
1954 | The output is an integer that is set to one if internal recursion when | The output is an integer that is set to one if internal recursion when |
1955 | running pcre_exec() is implemented by recursive function calls that use | running pcre_exec() is implemented by recursive function calls that use |
1956 | the stack to remember their state. This is the usual way that PCRE is | the stack to remember their state. This is the usual way that PCRE is |
1957 | compiled. The output is zero if PCRE was compiled to use blocks of data | compiled. The output is zero if PCRE was compiled to use blocks of data |
1958 | on the heap instead of recursive function calls. In this case, | on the heap instead of recursive function calls. In this case, |
1959 | pcre_stack_malloc and pcre_stack_free are called to manage memory | pcre_stack_malloc and pcre_stack_free are called to manage memory |
1960 | blocks on the heap, thus avoiding the use of the stack. | blocks on the heap, thus avoiding the use of the stack. |
1961 | ||
1962 | ||
# | Line 1972 COMPILING A PATTERN | Line 1973 COMPILING A PATTERN |
1973 | ||
1974 | Either of the functions pcre_compile() or pcre_compile2() can be called | Either of the functions pcre_compile() or pcre_compile2() can be called |
1975 | to compile a pattern into an internal form. The only difference between | to compile a pattern into an internal form. The only difference between |
1976 | the two interfaces is that pcre_compile2() has an additional argument, | the two interfaces is that pcre_compile2() has an additional argument, |
1977 | errorcodeptr, via which a numerical error code can be returned. To | errorcodeptr, via which a numerical error code can be returned. To |
1978 | avoid too much repetition, we refer just to pcre_compile() below, but | avoid too much repetition, we refer just to pcre_compile() below, but |
1979 | the information applies equally to pcre_compile2(). | the information applies equally to pcre_compile2(). |
1980 | ||
1981 | The pattern is a C string terminated by a binary zero, and is passed in | The pattern is a C string terminated by a binary zero, and is passed in |
1982 | the pattern argument. A pointer to a single block of memory that is | the pattern argument. A pointer to a single block of memory that is |
1983 | obtained via pcre_malloc is returned. This contains the compiled code | obtained via pcre_malloc is returned. This contains the compiled code |
1984 | and related data. The pcre type is defined for the returned block; this | and related data. The pcre type is defined for the returned block; this |
1985 | is a typedef for a structure whose contents are not externally defined. | is a typedef for a structure whose contents are not externally defined. |
1986 | It is up to the caller to free the memory (via pcre_free) when it is no | It is up to the caller to free the memory (via pcre_free) when it is no |
1987 | longer required. | longer required. |
1988 | ||
1989 | Although the compiled code of a PCRE regex is relocatable, that is, it | Although the compiled code of a PCRE regex is relocatable, that is, it |
1990 | does not depend on memory location, the complete pcre data block is not | does not depend on memory location, the complete pcre data block is not |
1991 | fully relocatable, because it may contain a copy of the tableptr argu- | fully relocatable, because it may contain a copy of the tableptr argu- |
1992 | ment, which is an address (see below). | ment, which is an address (see below). |
1993 | ||
1994 | The options argument contains various bit settings that affect the com- | The options argument contains various bit settings that affect the com- |
1995 | pilation. It should be zero if no options are required. The available | pilation. It should be zero if no options are required. The available |
1996 | options are described below. Some of them (in particular, those that | options are described below. Some of them (in particular, those that |
1997 | are compatible with Perl, but some others as well) can also be set and | are compatible with Perl, but some others as well) can also be set and |
1998 | unset from within the pattern (see the detailed description in the | unset from within the pattern (see the detailed description in the |
1999 | pcrepattern documentation). For those options that can be different in | pcrepattern documentation). For those options that can be different in |
2000 | different parts of the pattern, the contents of the options argument | different parts of the pattern, the contents of the options argument |
2001 | specifies their settings at the start of compilation and execution. The | specifies their settings at the start of compilation and execution. The |
2002 | PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and | PCRE_ANCHORED, PCRE_BSR_xxx, PCRE_NEWLINE_xxx, PCRE_NO_UTF8_CHECK, and |
2003 | PCRE_NO_START_OPTIMIZE options can be set at the time of matching as | PCRE_NO_START_OPTIMIZE options can be set at the time of matching as |
2004 | well as at compile time. | well as at compile time. |
2005 | ||
2006 | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, | If errptr is NULL, pcre_compile() returns NULL immediately. Otherwise, |
2007 | if compilation of a pattern fails, pcre_compile() returns NULL, and | if compilation of a pattern fails, pcre_compile() returns NULL, and |
2008 | sets the variable pointed to by errptr to point to a textual error mes- | sets the variable pointed to by errptr to point to a textual error mes- |
2009 | sage. This is a static string that is part of the library. You must not | sage. This is a static string that is part of the library. You must not |
2010 | try to free it. Normally, the offset from the start of the pattern to | try to free it. Normally, the offset from the start of the pattern to |
2011 | the byte that was being processed when the error was discovered is | the byte that was being processed when the error was discovered is |
2012 | placed in the variable pointed to by erroffset, which must not be NULL | placed in the variable pointed to by erroffset, which must not be NULL |
2013 | (if it is, an immediate error is given). However, for an invalid UTF-8 | (if it is, an immediate error is given). However, for an invalid UTF-8 |
2014 | string, the offset is that of the first byte of the failing character. | string, the offset is that of the first byte of the failing character. |
2015 | ||
2016 | Some errors are not detected until the whole pattern has been scanned; | Some errors are not detected until the whole pattern has been scanned; |
2017 | in these cases, the offset passed back is the length of the pattern. | in these cases, the offset passed back is the length of the pattern. |
2018 | Note that the offset is in bytes, not characters, even in UTF-8 mode. | Note that the offset is in bytes, not characters, even in UTF-8 mode. |
2019 | It may sometimes point into the middle of a UTF-8 character. | It may sometimes point into the middle of a UTF-8 character. |
2020 | ||
2021 | If pcre_compile2() is used instead of pcre_compile(), and the error- | If pcre_compile2() is used instead of pcre_compile(), and the error- |
2022 | codeptr argument is not NULL, a non-zero error code number is returned | codeptr argument is not NULL, a non-zero error code number is returned |
2023 | via this argument in the event of an error. This is in addition to the | via this argument in the event of an error. This is in addition to the |
2024 | textual error message. Error codes and messages are listed below. | textual error message. Error codes and messages are listed below. |
2025 | ||
2026 | If the final argument, tableptr, is NULL, PCRE uses a default set of | If the final argument, tableptr, is NULL, PCRE uses a default set of |
2027 | character tables that are built when PCRE is compiled, using the | character tables that are built when PCRE is compiled, using the |
2028 | default C locale. Otherwise, tableptr must be an address that is the | default C locale. Otherwise, tableptr must be an address that is the |
2029 | result of a call to pcre_maketables(). This value is stored with the | result of a call to pcre_maketables(). This value is stored with the |
2030 | compiled pattern, and used again by pcre_exec(), unless another table | compiled pattern, and used again by pcre_exec(), unless another table |
2031 | pointer is passed to it. For more discussion, see the section on locale | pointer is passed to it. For more discussion, see the section on locale |
2032 | support below. | support below. |
2033 | ||
2034 | This code fragment shows a typical straightforward call to pcre_com- | This code fragment shows a typical straightforward call to pcre_com- |
2035 | pile(): | pile(): |
2036 | ||
2037 | pcre *re; | pcre *re; |
# | Line 2043 COMPILING A PATTERN | Line 2044 COMPILING A PATTERN |
2044 | &erroffset, /* for error offset */ | &erroffset, /* for error offset */ |
2045 | NULL); /* use default character tables */ | NULL); /* use default character tables */ |
2046 | ||
2047 | The following names for option bits are defined in the pcre.h header | The following names for option bits are defined in the pcre.h header |
2048 | file: | file: |
2049 | ||
2050 | PCRE_ANCHORED | PCRE_ANCHORED |
2051 | ||
2052 | If this bit is set, the pattern is forced to be "anchored", that is, it | If this bit is set, the pattern is forced to be "anchored", that is, it |
2053 | is constrained to match only at the first matching point in the string | is constrained to match only at the first matching point in the string |
2054 | that is being searched (the "subject string"). This effect can also be | that is being searched (the "subject string"). This effect can also be |
2055 | achieved by appropriate constructs in the pattern itself, which is the | achieved by appropriate constructs in the pattern itself, which is the |
2056 | only way to do it in Perl. | only way to do it in Perl. |
2057 | ||
2058 | PCRE_AUTO_CALLOUT | PCRE_AUTO_CALLOUT |
2059 | ||
2060 | If this bit is set, pcre_compile() automatically inserts callout items, | If this bit is set, pcre_compile() automatically inserts callout items, |
2061 | all with number 255, before each pattern item. For discussion of the | all with number 255, before each pattern item. For discussion of the |
2062 | callout facility, see the pcrecallout documentation. | callout facility, see the pcrecallout documentation. |
2063 | ||
2064 | PCRE_BSR_ANYCRLF | PCRE_BSR_ANYCRLF |
2065 | PCRE_BSR_UNICODE | PCRE_BSR_UNICODE |
2066 | ||
2067 | These options (which are mutually exclusive) control what the \R escape | These options (which are mutually exclusive) control what the \R escape |
2068 | sequence matches. The choice is either to match only CR, LF, or CRLF, | sequence matches. The choice is either to match only CR, LF, or CRLF, |
2069 | or to match any Unicode newline sequence. The default is specified when | or to match any Unicode newline sequence. The default is specified when |
2070 | PCRE is built. It can be overridden from within the pattern, or by set- | PCRE is built. It can be overridden from within the pattern, or by set- |
2071 | ting an option when a compiled pattern is matched. | ting an option when a compiled pattern is matched. |
2072 | ||
2073 | PCRE_CASELESS | PCRE_CASELESS |
2074 | ||
2075 | If this bit is set, letters in the pattern match both upper and lower | If this bit is set, letters in the pattern match both upper and lower |
2076 | case letters. It is equivalent to Perl's /i option, and it can be | case letters. It is equivalent to Perl's /i option, and it can be |
2077 | changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE | changed within a pattern by a (?i) option setting. In UTF-8 mode, PCRE |
2078 | always understands the concept of case for characters whose values are | always understands the concept of case for characters whose values are |
2079 | less than 128, so caseless matching is always possible. For characters | less than 128, so caseless matching is always possible. For characters |
2080 | with higher values, the concept of case is supported if PCRE is com- | with higher values, the concept of case is supported if PCRE is com- |
2081 | piled with Unicode property support, but not otherwise. If you want to | piled with Unicode property support, but not otherwise. If you want to |
2082 | use caseless matching for characters 128 and above, you must ensure | use caseless matching for characters 128 and above, you must ensure |
2083 | that PCRE is compiled with Unicode property support as well as with | that PCRE is compiled with Unicode property support as well as with |
2084 | UTF-8 support. | UTF-8 support. |
2085 | ||
2086 | PCRE_DOLLAR_ENDONLY | PCRE_DOLLAR_ENDONLY |
2087 | ||
2088 | If this bit is set, a dollar metacharacter in the pattern matches only | If this bit is set, a dollar metacharacter in the pattern matches only |
2089 | at the end of the subject string. Without this option, a dollar also | at the end of the subject string. Without this option, a dollar also |
2090 | matches immediately before a newline at the end of the string (but not | matches immediately before a newline at the end of the string (but not |
2091 | before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored | before any other newlines). The PCRE_DOLLAR_ENDONLY option is ignored |
2092 | if PCRE_MULTILINE is set. There is no equivalent to this option in | if PCRE_MULTILINE is set. There is no equivalent to this option in |
2093 | Perl, and no way to set it within a pattern. | Perl, and no way to set it within a pattern. |
2094 | ||
2095 | PCRE_DOTALL | PCRE_DOTALL |
2096 | ||
2097 | If this bit is set, a dot metacharacter in the pattern matches a char- | If this bit is set, a dot metacharacter in the pattern matches a char- |
2098 | acter of any value, including one that indicates a newline. However, it | acter of any value, including one that indicates a newline. However, it |
2099 | only ever matches one character, even if newlines are coded as CRLF. | only ever matches one character, even if newlines are coded as CRLF. |
2100 | Without this option, a dot does not match when the current position is | Without this option, a dot does not match when the current position is |
2101 | at a newline. This option is equivalent to Perl's /s option, and it can | at a newline. This option is equivalent to Perl's /s option, and it can |
2102 | be changed within a pattern by a (?s) option setting. A negative class | be changed within a pattern by a (?s) option setting. A negative class |
2103 | such as [^a] always matches newline characters, independent of the set- | such as [^a] always matches newline characters, independent of the set- |
2104 | ting of this option. | ting of this option. |
2105 | ||
2106 | PCRE_DUPNAMES | PCRE_DUPNAMES |
2107 | ||
2108 | If this bit is set, names used to identify capturing subpatterns need | If this bit is set, names used to identify capturing subpatterns need |
2109 | not be unique. This can be helpful for certain types of pattern when it | not be unique. This can be helpful for certain types of pattern when it |
2110 | is known that only one instance of the named subpattern can ever be | is known that only one instance of the named subpattern can ever be |
2111 | matched. There are more details of named subpatterns below; see also | matched. There are more details of named subpatterns below; see also |
2112 | the pcrepattern documentation. | the pcrepattern documentation. |
2113 | ||
2114 | PCRE_EXTENDED | PCRE_EXTENDED |
2115 | ||
2116 | If this bit is set, white space data characters in the pattern are | If this bit is set, white space data characters in the pattern are |
2117 | totally ignored except when escaped or inside a character class. White | totally ignored except when escaped or inside a character class. White |
2118 | space does not include the VT character (code 11). In addition, charac- | space does not include the VT character (code 11). In addition, charac- |
2119 | ters between an unescaped # outside a character class and the next new- | ters between an unescaped # outside a character class and the next new- |
2120 | line, inclusive, are also ignored. This is equivalent to Perl's /x | line, inclusive, are also ignored. This is equivalent to Perl's /x |
2121 | option, and it can be changed within a pattern by a (?x) option set- | option, and it can be changed within a pattern by a (?x) option set- |
2122 | ting. | ting. |
2123 | ||
2124 | Which characters are interpreted as newlines is controlled by the | Which characters are interpreted as newlines is controlled by the |
2125 | options passed to pcre_compile() or by a special sequence at the start | options passed to pcre_compile() or by a special sequence at the start |
2126 | of the pattern, as described in the section entitled "Newline conven- | of the pattern, as described in the section entitled "Newline conven- |
2127 | tions" in the pcrepattern documentation. Note that the end of this type | tions" in the pcrepattern documentation. Note that the end of this type |
2128 | of comment is a literal newline sequence in the pattern; escape | of comment is a literal newline sequence in the pattern; escape |
2129 | sequences that happen to represent a newline do not count. | sequences that happen to represent a newline do not count. |
2130 | ||
2131 | This option makes it possible to include comments inside complicated | This option makes it possible to include comments inside complicated |
2132 | patterns. Note, however, that this applies only to data characters. | patterns. Note, however, that this applies only to data characters. |
2133 | White space characters may never appear within special character | White space characters may never appear within special character |
2134 | sequences in a pattern, for example within the sequence (?( that intro- | sequences in a pattern, for example within the sequence (?( that intro- |
2135 | duces a conditional subpattern. | duces a conditional subpattern. |
2136 | ||
2137 | PCRE_EXTRA | PCRE_EXTRA |
2138 | ||
2139 | This option was invented in order to turn on additional functionality | This option was invented in order to turn on additional functionality |
2140 | of PCRE that is incompatible with Perl, but it is currently of very | of PCRE that is incompatible with Perl, but it is currently of very |
2141 | little use. When set, any backslash in a pattern that is followed by a | little use. When set, any backslash in a pattern that is followed by a |
2142 | letter that has no special meaning causes an error, thus reserving | letter that has no special meaning causes an error, thus reserving |
2143 | these combinations for future expansion. By default, as in Perl, a | these combinations for future expansion. By default, as in Perl, a |
2144 | backslash followed by a letter with no special meaning is treated as a | backslash followed by a letter with no special meaning is treated as a |
2145 | literal. (Perl can, however, be persuaded to give an error for this, by | literal. (Perl can, however, be persuaded to give an error for this, by |
2146 | running it with the -w option.) There are at present no other features | running it with the -w option.) There are at present no other features |
2147 | controlled by this option. It can also be set by a (?X) option setting | controlled by this option. It can also be set by a (?X) option setting |
2148 | within a pattern. | within a pattern. |
2149 | ||
2150 | PCRE_FIRSTLINE | PCRE_FIRSTLINE |
2151 | ||
2152 | If this option is set, an unanchored pattern is required to match | If this option is set, an unanchored pattern is required to match |
2153 | before or at the first newline in the subject string, though the | before or at the first newline in the subject string, though the |
2154 | matched text may continue over the newline. | matched text may continue over the newline. |
2155 | ||
2156 | PCRE_JAVASCRIPT_COMPAT | PCRE_JAVASCRIPT_COMPAT |
2157 | ||
2158 | If this option is set, PCRE's behaviour is changed in some ways so that | If this option is set, PCRE's behaviour is changed in some ways so that |
2159 | it is compatible with JavaScript rather than Perl. The changes are as | it is compatible with JavaScript rather than Perl. The changes are as |
2160 | follows: | follows: |
2161 | ||
2162 | (1) A lone closing square bracket in a pattern causes a compile-time | (1) A lone closing square bracket in a pattern causes a compile-time |
2163 | error, because this is illegal in JavaScript (by default it is treated | error, because this is illegal in JavaScript (by default it is treated |
2164 | as a data character). Thus, the pattern AB]CD becomes illegal when this | as a data character). Thus, the pattern AB]CD becomes illegal when this |
2165 | option is set. | option is set. |
2166 | ||
2167 | (2) At run time, a back reference to an unset subpattern group matches | (2) At run time, a back reference to an unset subpattern group matches |
2168 | an empty string (by default this causes the current matching alterna- | an empty string (by default this causes the current matching alterna- |
2169 | tive to fail). A pattern such as (\1)(a) succeeds when this option is | tive to fail). A pattern such as (\1)(a) succeeds when this option is |
2170 | set (assuming it can find an "a" in the subject), whereas it fails by | set (assuming it can find an "a" in the subject), whereas it fails by |
2171 | default, for Perl compatibility. | default, for Perl compatibility. |
2172 | ||
2173 | (3) \U matches an upper case "U" character; by default \U causes a com- | (3) \U matches an upper case "U" character; by default \U causes a com- |
2174 | pile time error (Perl uses \U to upper case subsequent characters). | pile time error (Perl uses \U to upper case subsequent characters). |
2175 | ||
2176 | (4) \u matches a lower case "u" character unless it is followed by four | (4) \u matches a lower case "u" character unless it is followed by four |
2177 | hexadecimal digits, in which case the hexadecimal number defines the | hexadecimal digits, in which case the hexadecimal number defines the |
2178 | code point to match. By default, \u causes a compile time error (Perl | code point to match. By default, \u causes a compile time error (Perl |
2179 | uses it to upper case the following character). | uses it to upper case the following character). |
2180 | ||
2181 | (5) \x matches a lower case "x" character unless it is followed by two | (5) \x matches a lower case "x" character unless it is followed by two |
2182 | hexadecimal digits, in which case the hexadecimal number defines the | hexadecimal digits, in which case the hexadecimal number defines the |
2183 | code point to match. By default, as in Perl, a hexadecimal number is | code point to match. By default, as in Perl, a hexadecimal number is |
2184 | always expected after \x, but it may have zero, one, or two digits (so, | always expected after \x, but it may have zero, one, or two digits (so, |
2185 | for example, \xz matches a binary zero character followed by z). | for example, \xz matches a binary zero character followed by z). |
2186 | ||
2187 | PCRE_MULTILINE | PCRE_MULTILINE |
2188 | ||
2189 | By default, PCRE treats the subject string as consisting of a single | By default, PCRE treats the subject string as consisting of a single |
2190 | line of characters (even if it actually contains newlines). The "start | line of characters (even if it actually contains newlines). The "start |
2191 | of line" metacharacter (^) matches only at the start of the string, | of line" metacharacter (^) matches only at the start of the string, |
2192 | while the "end of line" metacharacter ($) matches only at the end of | while the "end of line" metacharacter ($) matches only at the end of |
2193 | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY | the string, or before a terminating newline (unless PCRE_DOLLAR_ENDONLY |
2194 | is set). This is the same as Perl. | is set). This is the same as Perl. |
2195 | ||
2196 | When PCRE_MULTILINE it is set, the "start of line" and "end of line" | When PCRE_MULTILINE it is set, the "start of line" and "end of line" |
2197 | constructs match immediately following or immediately before internal | constructs match immediately following or immediately before internal |
2198 | newlines in the subject string, respectively, as well as at the very | newlines in the subject string, respectively, as well as at the very |
2199 | start and end. This is equivalent to Perl's /m option, and it can be | start and end. This is equivalent to Perl's /m option, and it can be |
2200 | changed within a pattern by a (?m) option setting. If there are no new- | changed within a pattern by a (?m) option setting. If there are no new- |
2201 | lines in a subject string, or no occurrences of ^ or $ in a pattern, | lines in a subject string, or no occurrences of ^ or $ in a pattern, |
2202 | setting PCRE_MULTILINE has no effect. | setting PCRE_MULTILINE has no effect. |
2203 | ||
2204 | PCRE_NEWLINE_CR | PCRE_NEWLINE_CR |
# | Line 2206 COMPILING A PATTERN | Line 2207 COMPILING A PATTERN |
2207 | PCRE_NEWLINE_ANYCRLF | PCRE_NEWLINE_ANYCRLF |
2208 | PCRE_NEWLINE_ANY | PCRE_NEWLINE_ANY |
2209 | ||
2210 | These options override the default newline definition that was chosen | These options override the default newline definition that was chosen |
2211 | when PCRE was built. Setting the first or the second specifies that a | when PCRE was built. Setting the first or the second specifies that a |
2212 | newline is indicated by a single character (CR or LF, respectively). | newline is indicated by a single character (CR or LF, respectively). |
2213 | Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the | Setting PCRE_NEWLINE_CRLF specifies that a newline is indicated by the |
2214 | two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies | two-character CRLF sequence. Setting PCRE_NEWLINE_ANYCRLF specifies |
2215 | that any of the three preceding sequences should be recognized. Setting | that any of the three preceding sequences should be recognized. Setting |
2216 | PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be | PCRE_NEWLINE_ANY specifies that any Unicode newline sequence should be |
2217 | recognized. | recognized. |
2218 | ||
2219 | In an ASCII/Unicode environment, the Unicode newline sequences are the | In an ASCII/Unicode environment, the Unicode newline sequences are the |
2220 | three just mentioned, plus the single characters VT (vertical tab, | three just mentioned, plus the single characters VT (vertical tab, |
2221 | U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line sep- | U+000B), FF (form feed, U+000C), NEL (next line, U+0085), LS (line sep- |
2222 | arator, U+2028), and PS (paragraph separator, U+2029). For the 8-bit | arator, U+2028), and PS (paragraph separator, U+2029). For the 8-bit |
2223 | library, the last two are recognized only in UTF-8 mode. | library, the last two are recognized only in UTF-8 mode. |
2224 | ||
2225 | When PCRE is compiled to run in an EBCDIC (mainframe) environment, the | When PCRE is compiled to run in an EBCDIC (mainframe) environment, the |
2226 | code for CR is 0x0d, the same as ASCII. However, the character code for | code for CR is 0x0d, the same as ASCII. However, the character code for |
2227 | LF is normally 0x15, though in some EBCDIC environments 0x25 is used. | LF is normally 0x15, though in some EBCDIC environments 0x25 is used. |
2228 | Whichever of these is not LF is made to correspond to Unicode's NEL | Whichever of these is not LF is made to correspond to Unicode's NEL |
2229 | character. EBCDIC codes are all less than 256. For more details, see | character. EBCDIC codes are all less than 256. For more details, see |
2230 | the pcrebuild documentation. | the pcrebuild documentation. |
2231 | ||
2232 | The newline setting in the options word uses three bits that are | The newline setting in the options word uses three bits that are |
2233 | treated as a number, giving eight possibilities. Currently only six are | treated as a number, giving eight possibilities. Currently only six are |
2234 | used (default plus the five values above). This means that if you set | used (default plus the five values above). This means that if you set |
2235 | more than one newline option, the combination may or may not be sensi- | more than one newline option, the combination may or may not be sensi- |
2236 | ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to | ble. For example, PCRE_NEWLINE_CR with PCRE_NEWLINE_LF is equivalent to |
2237 | PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and | PCRE_NEWLINE_CRLF, but other combinations may yield unused numbers and |
2238 | cause an error. | cause an error. |
2239 | ||
2240 | The only time that a line break in a pattern is specially recognized | The only time that a line break in a pattern is specially recognized |
2241 | when compiling is when PCRE_EXTENDED is set. CR and LF are white space | when compiling is when PCRE_EXTENDED is set. CR and LF are white space |
2242 | characters, and so are ignored in this mode. Also, an unescaped # out- | characters, and so are ignored in this mode. Also, an unescaped # out- |
2243 | side a character class indicates a comment that lasts until after the | side a character class indicates a comment that lasts until after the |
2244 | next line break sequence. In other circumstances, line break sequences | next line break sequence. In other circumstances, line break sequences |
2245 | in patterns are treated as literal data. | in patterns are treated as literal data. |
2246 | ||
2247 | The newline option that is set at compile time becomes the default that | The newline option that is set at compile time becomes the default that |
# | Line 2249 COMPILING A PATTERN | Line 2250 COMPILING A PATTERN |
2250 | PCRE_NO_AUTO_CAPTURE | PCRE_NO_AUTO_CAPTURE |
2251 | ||
2252 | If this option is set, it disables the use of numbered capturing paren- | If this option is set, it disables the use of numbered capturing paren- |
2253 | theses in the pattern. Any opening parenthesis that is not followed by | theses in the pattern. Any opening parenthesis that is not followed by |
2254 | ? behaves as if it were followed by ?: but named parentheses can still | ? behaves as if it were followed by ?: but named parentheses can still |
2255 | be used for capturing (and they acquire numbers in the usual way). | be used for capturing (and they acquire numbers in the usual way). |
2256 | There is no equivalent of this option in Perl. | There is no equivalent of this option in Perl. |
2257 | ||
2258 | NO_START_OPTIMIZE | NO_START_OPTIMIZE |
2259 | ||
2260 | This is an option that acts at matching time; that is, it is really an | This is an option that acts at matching time; that is, it is really an |
2261 | option for pcre_exec() or pcre_dfa_exec(). If it is set at compile | option for pcre_exec() or pcre_dfa_exec(). If it is set at compile |
2262 | time, it is remembered with the compiled pattern and assumed at match- | time, it is remembered with the compiled pattern and assumed at match- |
2263 | ing time. For details see the discussion of PCRE_NO_START_OPTIMIZE | ing time. For details see the discussion of PCRE_NO_START_OPTIMIZE |
2264 | below. | below. |
2265 | ||
2266 | PCRE_UCP | PCRE_UCP |
2267 | ||
2268 | This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W, | This option changes the way PCRE processes \B, \b, \D, \d, \S, \s, \W, |
2269 | \w, and some of the POSIX character classes. By default, only ASCII | \w, and some of the POSIX character classes. By default, only ASCII |
2270 | characters are recognized, but if PCRE_UCP is set, Unicode properties | characters are recognized, but if PCRE_UCP is set, Unicode properties |
2271 | are used instead to classify characters. More details are given in the | are used instead to classify characters. More details are given in the |
2272 | section on generic character types in the pcrepattern page. If you set | section on generic character types in the pcrepattern page. If you set |
2273 | PCRE_UCP, matching one of the items it affects takes much longer. The | PCRE_UCP, matching one of the items it affects takes much longer. The |
2274 | option is available only if PCRE has been compiled with Unicode prop- | option is available only if PCRE has been compiled with Unicode prop- |
2275 | erty support. | erty support. |
2276 | ||
2277 | PCRE_UNGREEDY | PCRE_UNGREEDY |
2278 | ||
2279 | This option inverts the "greediness" of the quantifiers so that they | This option inverts the "greediness" of the quantifiers so that they |
2280 | are not greedy by default, but become greedy if followed by "?". It is | are not greedy by default, but become greedy if followed by "?". It is |
2281 | not compatible with Perl. It can also be set by a (?U) option setting | not compatible with Perl. It can also be set by a (?U) option setting |
2282 | within the pattern. | within the pattern. |
2283 | ||
2284 | PCRE_UTF8 | PCRE_UTF8 |
2285 | ||
2286 | This option causes PCRE to regard both the pattern and the subject as | This option causes PCRE to regard both the pattern and the subject as |
2287 | strings of UTF-8 characters instead of single-byte strings. However, it | strings of UTF-8 characters instead of single-byte strings. However, it |
2288 | is available only when PCRE is built to include UTF support. If not, | is available only when PCRE is built to include UTF support. If not, |
2289 | the use of this option provokes an error. Details of how this option | the use of this option provokes an error. Details of how this option |
2290 | changes the behaviour of PCRE are given in the pcreunicode page. | changes the behaviour of PCRE are given in the pcreunicode page. |
2291 | ||
2292 | PCRE_NO_UTF8_CHECK | PCRE_NO_UTF8_CHECK |
2293 | ||
2294 | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is | When PCRE_UTF8 is set, the validity of the pattern as a UTF-8 string is |
2295 | automatically checked. There is a discussion about the validity of | automatically checked. There is a discussion about the validity of |
2296 | UTF-8 strings in the pcreunicode page. If an invalid UTF-8 sequence is | UTF-8 strings in the pcreunicode page. If an invalid UTF-8 sequence is |
2297 | found, pcre_compile() returns an error. If you already know that your | found, pcre_compile() returns an error. If you already know that your |
2298 | pattern is valid, and you want to skip this check for performance rea- | pattern is valid, and you want to skip this check for performance rea- |
2299 | sons, you can set the PCRE_NO_UTF8_CHECK option. When it is set, the | sons, you can set the PCRE_NO_UTF8_CHECK option. When it is set, the |
2300 | effect of passing an invalid UTF-8 string as a pattern is undefined. It | effect of passing an invalid UTF-8 string as a pattern is undefined. It |
2301 | may cause your program to crash. Note that this option can also be | may cause your program to crash. Note that this option can also be |
2302 | passed to pcre_exec() and pcre_dfa_exec(), to suppress the validity | passed to pcre_exec() and pcre_dfa_exec(), to suppress the validity |
2303 | checking of subject strings only. If the same string is being matched | checking of subject strings only. If the same string is being matched |
2304 | many times, the option can be safely set for the second and subsequent | many times, the option can be safely set for the second and subsequent |
2305 | matchings to improve performance. | matchings to improve performance. |
2306 | ||
2307 | ||
2308 | COMPILATION ERROR CODES | COMPILATION ERROR CODES |
2309 | ||
2310 | The following table lists the error codes than may be returned by | The following table lists the error codes than may be returned by |
2311 | pcre_compile2(), along with the error messages that may be returned by | pcre_compile2(), along with the error messages that may be returned by |
2312 | both compiling functions. Note that error messages are always 8-bit | both compiling functions. Note that error messages are always 8-bit |
2313 | ASCII strings, even in 16-bit or 32-bit mode. As PCRE has developed, | ASCII strings, even in 16-bit or 32-bit mode. As PCRE has developed, |
2314 | some error codes have fallen out of use. To avoid confusion, they have | some error codes have fallen out of use. To avoid confusion, they have |
2315 | not been re-used. | not been re-used. |
2316 | ||
2317 | 0 no error | 0 no error |
# | Line 2396 COMPILATION ERROR CODES | Line 2397 COMPILATION ERROR CODES |
2397 | 76 character value in \u.... sequence is too large | 76 character value in \u.... sequence is too large |
2398 | 77 invalid UTF-32 string (specifically UTF-32) | 77 invalid UTF-32 string (specifically UTF-32) |
2399 | ||
2400 | The numbers 32 and 10000 in errors 48 and 49 are defaults; different | The numbers 32 and 10000 in errors 48 and 49 are defaults; different |
2401 | values may be used if the limits were changed when PCRE was built. | values may be used if the limits were changed when PCRE was built. |
2402 | ||
2403 | ||
# | Line 2405 STUDYING A PATTERN | Line 2406 STUDYING A PATTERN |
2406 | pcre_extra *pcre_study(const pcre *code, int options | pcre_extra *pcre_study(const pcre *code, int options |
2407 | const char **errptr); | const char **errptr); |
2408 | ||
2409 | If a compiled pattern is going to be used several times, it is worth | If a compiled pattern is going to be used several times, it is worth |
2410 | spending more time analyzing it in order to speed up the time taken for | spending more time analyzing it in order to speed up the time taken for |
2411 | matching. The function pcre_study() takes a pointer to a compiled pat- | matching. The function pcre_study() takes a pointer to a compiled pat- |
2412 | tern as its first argument. If studying the pattern produces additional | tern as its first argument. If studying the pattern produces additional |
2413 | information that will help speed up matching, pcre_study() returns a | information that will help speed up matching, pcre_study() returns a |
2414 | pointer to a pcre_extra block, in which the study_data field points to | pointer to a pcre_extra block, in which the study_data field points to |
2415 | the results of the study. | the results of the study. |
2416 | ||
2417 | The returned value from pcre_study() can be passed directly to | The returned value from pcre_study() can be passed directly to |
2418 | pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con- | pcre_exec() or pcre_dfa_exec(). However, a pcre_extra block also con- |
2419 | tains other fields that can be set by the caller before the block is | tains other fields that can be set by the caller before the block is |
2420 | passed; these are described below in the section on matching a pattern. | passed; these are described below in the section on matching a pattern. |
2421 | ||
2422 | If studying the pattern does not produce any useful information, | If studying the pattern does not produce any useful information, |
2423 | pcre_study() returns NULL by default. In that circumstance, if the | pcre_study() returns NULL by default. In that circumstance, if the |
2424 | calling program wants to pass any of the other fields to pcre_exec() or | calling program wants to pass any of the other fields to pcre_exec() or |
2425 | pcre_dfa_exec(), it must set up its own pcre_extra block. However, if | pcre_dfa_exec(), it must set up its own pcre_extra block. However, if |
2426 | pcre_study() is called with the PCRE_STUDY_EXTRA_NEEDED option, it | pcre_study() is called with the PCRE_STUDY_EXTRA_NEEDED option, it |
2427 | returns a pcre_extra block even if studying did not find any additional | returns a pcre_extra block even if studying did not find any additional |
2428 | information. It may still return NULL, however, if an error occurs in | information. It may still return NULL, however, if an error occurs in |
2429 | pcre_study(). | pcre_study(). |
2430 | ||
2431 | The second argument of pcre_study() contains option bits. There are | The second argument of pcre_study() contains option bits. There are |
2432 | three further options in addition to PCRE_STUDY_EXTRA_NEEDED: | three further options in addition to PCRE_STUDY_EXTRA_NEEDED: |
2433 | ||
2434 | PCRE_STUDY_JIT_COMPILE | PCRE_STUDY_JIT_COMPILE |
2435 | PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE | PCRE_STUDY_JIT_PARTIAL_HARD_COMPILE |
2436 | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE | PCRE_STUDY_JIT_PARTIAL_SOFT_COMPILE |
2437 | ||
2438 | If any of these are set, and the just-in-time compiler is available, | If any of these are set, and the just-in-time compiler is available, |
2439 | the pattern is further compiled into machine code that executes much | the pattern is further compiled into machine code that executes much |
2440 | faster than the pcre_exec() interpretive matching function. If the | faster than the pcre_exec() interpretive matching function. If the |
2441 | just-in-time compiler is not available, these options are ignored. All | just-in-time compiler is not available, these options are ignored. All |
2442 | undefined bits in the options argument must be zero. | undefined bits in the options argument must be zero. |
2443 | ||
2444 | JIT compilation is a heavyweight optimization. It can take some time | JIT compilation is a heavyweight optimization. It can take some time |
2445 | for patterns to be analyzed, and for one-off matches and simple pat- | for patterns to be analyzed, and for one-off matches and simple pat- |
2446 | terns the benefit of faster execution might be offset by a much slower | terns the benefit of faster execution might be offset by a much slower |
2447 | study time. Not all patterns can be optimized by the JIT compiler. For | study time. Not all patterns can be optimized by the JIT compiler. For |
2448 | those that cannot be handled, matching automatically falls back to the | those that cannot be handled, matching automatically falls back to the |
2449 | pcre_exec() interpreter. For more details, see the pcrejit documenta- | pcre_exec() interpreter. For more details, see the pcrejit documenta- |
2450 | tion. | tion. |
2451 | ||
2452 | The third argument for pcre_study() is a pointer for an error message. | The third argument for pcre_study() is a pointer for an error message. |
2453 | If studying succeeds (even if no data is returned), the variable it | If studying succeeds (even if no data is returned), the variable it |
2454 | points to is set to NULL. Otherwise it is set to point to a textual | points to is set to NULL. Otherwise it is set to point to a textual |
2455 | error message. This is a static string that is part of the library. You | error message. This is a static string that is part of the library. You |
2456 | must not try to free it. You should test the error pointer for NULL | must not try to free it. You should test the error pointer for NULL |
2457 | after calling pcre_study(), to be sure that it has run successfully. | after calling pcre_study(), to be sure that it has run successfully. |
2458 | ||
2459 | When you are finished with a pattern, you can free the memory used for | When you are finished with a pattern, you can free the memory used for |
2460 | the study data by calling pcre_free_study(). This function was added to | the study data by calling pcre_free_study(). This function was added to |
2461 | the API for release 8.20. For earlier versions, the memory could be | the API for release 8.20. For earlier versions, the memory could be |
2462 | freed with pcre_free(), just like the pattern itself. This will still | freed with pcre_free(), just like the pattern itself. This will still |
2463 | work in cases where JIT optimization is not used, but it is advisable | work in cases where JIT optimization is not used, but it is advisable |
2464 | to change to the new function when convenient. | to change to the new function when convenient. |
2465 | ||
2466 | This is a typical way in which pcre_study() is used (except that in a | This is a typical way in which pcre_study() is used (except that in a |
2467 | real application there should be tests for errors): | real application there should be tests for errors): |
2468 | ||
2469 | int rc; | int rc; |
# | Line 2482 STUDYING A PATTERN | Line 2483 STUDYING A PATTERN |
2483 | Studying a pattern does two things: first, a lower bound for the length | Studying a pattern does two things: first, a lower bound for the length |
2484 | of subject string that is needed to match the pattern is computed. This | of subject string that is needed to match the pattern is computed. This |
2485 | does not mean that there are any strings of that length that match, but | does not mean that there are any strings of that length that match, but |
2486 | it does guarantee that no shorter strings match. The value is used to | it does guarantee that no shorter strings match. The value is used to |
2487 | avoid wasting time by trying to match strings that are shorter than the | avoid wasting time by trying to match strings that are shorter than the |
2488 | lower bound. You can find out the value in a calling program via the | lower bound. You can find out the value in a calling program via the |
2489 | pcre_fullinfo() function. | pcre_fullinfo() function. |
2490 | ||
2491 | Studying a pattern is also useful for non-anchored patterns that do not | Studying a pattern is also useful for non-anchored patterns that do not |
2492 | have a single fixed starting character. A bitmap of possible starting | have a single fixed starting character. A bitmap of possible starting |
2493 | bytes is created. This speeds up finding a position in the subject at | bytes is created. This speeds up finding a position in the subject at |
2494 | which to start matching. (In 16-bit mode, the bitmap is used for 16-bit | which to start matching. (In 16-bit mode, the bitmap is used for 16-bit |
2495 | values less than 256. In 32-bit mode, the bitmap is used for 32-bit | values less than 256. In 32-bit mode, the bitmap is used for 32-bit |
2496 | values less than 256.) | values less than 256.) |
2497 | ||
2498 | These two optimizations apply to both pcre_exec() and pcre_dfa_exec(), | These two optimizations apply to both pcre_exec() and pcre_dfa_exec(), |
2499 | and the information is also used by the JIT compiler. The optimiza- | and the information is also used by the JIT compiler. The optimiza- |
2500 | tions can be disabled by setting the PCRE_NO_START_OPTIMIZE option when | tions can be disabled by setting the PCRE_NO_START_OPTIMIZE option when |
2501 | calling pcre_exec() or pcre_dfa_exec(), but if this is done, JIT execu- | calling pcre_exec() or pcre_dfa_exec(), but if this is done, JIT execu- |
2502 | tion is also disabled. You might want to do this if your pattern con- | tion is also disabled. You might want to do this if your pattern con- |
2503 | tains callouts or (*MARK) and you want to make use of these facilities | tains callouts or (*MARK) and you want to make use of these facilities |
2504 | in cases where matching fails. See the discussion of | in cases where matching fails. See the discussion of |
2505 | PCRE_NO_START_OPTIMIZE below. | PCRE_NO_START_OPTIMIZE below. |
2506 | ||
2507 | ||
2508 | LOCALE SUPPORT | LOCALE SUPPORT |
2509 | ||
2510 | PCRE handles caseless matching, and determines whether characters are | PCRE handles caseless matching, and determines whether characters are |
2511 | letters, digits, or whatever, by reference to a set of tables, indexed | letters, digits, or whatever, by reference to a set of tables, indexed |
2512 | by character value. When running in UTF-8 mode, this applies only to | by character value. When running in UTF-8 mode, this applies only to |
2513 | characters with codes less than 128. By default, higher-valued codes | characters with codes less than 128. By default, higher-valued codes |
2514 | never match escapes such as \w or \d, but they can be tested with \p if | never match escapes such as \w or \d, but they can be tested with \p if |
2515 | PCRE is built with Unicode character property support. Alternatively, | PCRE is built with Unicode character property support. Alternatively, |
2516 | the PCRE_UCP option can be set at compile time; this causes \w and | the PCRE_UCP option can be set at compile time; this causes \w and |
2517 | friends to use Unicode property support instead of built-in tables. The | friends to use Unicode property support instead of built-in tables. The |
2518 | use of locales with Unicode is discouraged. If you are handling charac- | use of locales with Unicode is discouraged. If you are handling charac- |
2519 | ters with codes greater than 128, you should either use UTF-8 and Uni- | ters with codes greater than 128, you should either use UTF-8 and Uni- |
2520 | code, or use locales, but not try to mix the two. | code, or use locales, but not try to mix the two. |
2521 | ||
2522 | PCRE contains an internal set of tables that are used when the final | PCRE contains an internal set of tables that are used when the final |
2523 | argument of pcre_compile() is NULL. These are sufficient for many | argument of pcre_compile() is NULL. These are sufficient for many |
2524 | applications. Normally, the internal tables recognize only ASCII char- | applications. Normally, the internal tables recognize only ASCII char- |
2525 | acters. However, when PCRE is built, it is possible to cause the inter- | acters. However, when PCRE is built, it is possible to cause the inter- |
2526 | nal tables to be rebuilt in the default "C" locale of the local system, | nal tables to be rebuilt in the default "C" locale of the local system, |
2527 | which may cause them to be different. | which may cause them to be different. |
2528 | ||
2529 | The internal tables can always be overridden by tables supplied by the | The internal tables can always be overridden by tables supplied by the |
2530 | application that calls PCRE. These may be created in a different locale | application that calls PCRE. These may be created in a different locale |
2531 | from the default. As more and more applications change to using Uni- | from the default. As more and more applications change to using Uni- |
2532 | code, the need for this locale support is expected to die away. | code, the need for this locale support is expected to die away. |
2533 | ||
2534 | External tables are built by calling the pcre_maketables() function, | External tables are built by calling the pcre_maketables() function, |
2535 | which has no arguments, in the relevant locale. The result can then be | which has no arguments, in the relevant locale. The result can then be |
2536 | passed to pcre_compile() or pcre_exec() as often as necessary. For | passed to pcre_compile() or pcre_exec() as often as necessary. For |
2537 | example, to build and use tables that are appropriate for the French | example, to build and use tables that are appropriate for the French |
2538 | locale (where accented characters with values greater than 128 are | locale (where accented characters with values greater than 128 are |
2539 | treated as letters), the following code could be used: | treated as letters), the following code could be used: |
2540 | ||
2541 | setlocale(LC_CTYPE, "fr_FR"); | setlocale(LC_CTYPE, "fr_FR"); |
2542 | tables = pcre_maketables(); | tables = pcre_maketables(); |
2543 | re = pcre_compile(..., tables); | re = pcre_compile(..., tables); |
2544 | ||
2545 | The locale name "fr_FR" is used on Linux and other Unix-like systems; | The locale name "fr_FR" is used on Linux and other Unix-like systems; |
2546 | if you are using Windows, the name for the French locale is "french". | if you are using Windows, the name for the French locale is "french". |
2547 | ||
2548 | When pcre_maketables() runs, the tables are built in memory that is | When pcre_maketables() runs, the tables are built in memory that is |
2549 | obtained via pcre_malloc. It is the caller's responsibility to ensure | obtained via pcre_malloc. It is the caller's responsibility to ensure |
2550 | that the memory containing the tables remains available for as long as | that the memory containing the tables remains available for as long as |
2551 | it is needed. | it is needed. |
2552 | ||
2553 | The pointer that is passed to pcre_compile() is saved with the compiled | The pointer that is passed to pcre_compile() is saved with the compiled |
2554 | pattern, and the same tables are used via this pointer by pcre_study() | pattern, and the same tables are used via this pointer by pcre_study() |
2555 | and normally also by pcre_exec(). Thus, by default, for any single pat- | and normally also by pcre_exec(). Thus, by default, for any single pat- |
2556 | tern, compilation, studying and matching all happen in the same locale, | tern, compilation, studying and matching all happen in the same locale, |
2557 | but different patterns can be compiled in different locales. | but different patterns can be compiled in different locales. |
2558 | ||
2559 | It is possible to pass a table pointer or NULL (indicating the use of | It is possible to pass a table pointer or NULL (indicating the use of |
2560 | the internal tables) to pcre_exec(). Although not intended for this | the internal tables) to pcre_exec(). Although not intended for this |
2561 | purpose, this facility could be used to match a pattern in a different | purpose, this facility could be used to match a pattern in a different |
2562 | locale from the one in which it was compiled. Passing table pointers at | locale from the one in which it was compiled. Passing table pointers at |
2563 | run time is discussed below in the section on matching a pattern. | run time is discussed below in the section on matching a pattern. |
2564 | ||
# | Line 2567 INFORMATION ABOUT A PATTERN | Line 2568 INFORMATION ABOUT A PATTERN |
2568 | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, | int pcre_fullinfo(const pcre *code, const pcre_extra *extra, |
2569 | int what, void *where); | int what, void *where); |
2570 | ||
2571 | The pcre_fullinfo() function returns information about a compiled pat- | The pcre_fullinfo() function returns information about a compiled pat- |
2572 | tern. It replaces the pcre_info() function, which was removed from the | tern. It replaces the pcre_info() function, which was removed from the |
2573 | library at version 8.30, after more than 10 years of obsolescence. | library at version 8.30, after more than 10 years of obsolescence. |
2574 | ||
2575 | The first argument for pcre_fullinfo() is a pointer to the compiled | The first argument for pcre_fullinfo() is a pointer to the compiled |
2576 | pattern. The second argument is the result of pcre_study(), or NULL if | pattern. The second argument is the result of pcre_study(), or NULL if |
2577 | the pattern was not studied. The third argument specifies which piece | the pattern was not studied. The third argument specifies which piece |
2578 | of information is required, and the fourth argument is a pointer to a | of information is required, and the fourth argument is a pointer to a |
2579 | variable to receive the data. The yield of the function is zero for | variable to receive the data. The yield of the function is zero for |
2580 | success, or one of the following negative numbers: | success, or one of the following negative numbers: |
2581 | ||
2582 | PCRE_ERROR_NULL the argument code was NULL | PCRE_ERROR_NULL the argument code was NULL |
# | Line 2585 INFORMATION ABOUT A PATTERN | Line 2586 INFORMATION ABOUT A PATTERN |
2586 | endianness | endianness |
2587 | PCRE_ERROR_BADOPTION the value of what was invalid | PCRE_ERROR_BADOPTION the value of what was invalid |
2588 | ||
2589 | The "magic number" is placed at the start of each compiled pattern as | The "magic number" is placed at the start of each compiled pattern as |
2590 | an simple check against passing an arbitrary memory pointer. The endi- | an simple check against passing an arbitrary memory pointer. The endi- |
2591 | anness error can occur if a compiled pattern is saved and reloaded on a | anness error can occur if a compiled pattern is saved and reloaded on a |
2592 | different host. Here is a typical call of pcre_fullinfo(), to obtain | different host. Here is a typical call of pcre_fullinfo(), to obtain |
2593 | the length of the compiled pattern: | the length of the compiled pattern: |
2594 | ||
2595 | int rc; | int rc; |
# | Line 2599 INFORMATION ABOUT A PATTERN | Line 2600 INFORMATION ABOUT A PATTERN |
2600 | PCRE_INFO_SIZE, /* what is required */ | PCRE_INFO_SIZE, /* what is required */ |
2601 | &length); /* where to put the data */ | &length); /* where to put the data */ |
2602 | ||
2603 | The possible values for the third argument are defined in pcre.h, and | The possible values for the third argument are defined in pcre.h, and |
2604 | are as follows: | are as follows: |
2605 | ||
2606 | PCRE_INFO_BACKREFMAX | PCRE_INFO_BACKREFMAX |
2607 | ||
2608 | Return the number of the highest back reference in the pattern. The | Return the number of the highest back reference in the pattern. The |
2609 | fourth argument should point to an int variable. Zero is returned if | fourth argument should point to an int variable. Zero is returned if |
2610 | there are no back references. | there are no back references. |
2611 | ||
2612 | PCRE_INFO_CAPTURECOUNT | PCRE_INFO_CAPTURECOUNT |
2613 | ||
2614 | Return the number of capturing subpatterns in the pattern. The fourth | Return the number of capturing subpatterns in the pattern. The fourth |
2615 | argument should point to an int variable. | argument should point to an int variable. |
2616 | ||
2617 | PCRE_INFO_DEFAULT_TABLES | PCRE_INFO_DEFAULT_TABLES |
2618 | ||
2619 | Return a pointer to the internal default character tables within PCRE. | Return a pointer to the internal default character tables within PCRE. |
2620 | The fourth argument should point to an unsigned char * variable. This | The fourth argument should point to an unsigned char * variable. This |
2621 | information call is provided for internal use by the pcre_study() func- | information call is provided for internal use by the pcre_study() func- |
2622 | tion. External callers can cause PCRE to use its internal tables by | tion. External callers can cause PCRE to use its internal tables by |
2623 | passing a NULL table pointer. | passing a NULL table pointer. |
2624 | ||
2625 | PCRE_INFO_FIRSTBYTE | PCRE_INFO_FIRSTBYTE |
2626 | ||
2627 | Return information about the first data unit of any matched string, for | Return information about the first data unit of any matched string, for |
2628 | a non-anchored pattern. (The name of this option refers to the 8-bit | a non-anchored pattern. (The name of this option refers to the 8-bit |
2629 | library, where data units are bytes.) The fourth argument should point | library, where data units are bytes.) The fourth argument should point |
2630 | to an int variable. | to an int variable. |
2631 | ||
2632 | If there is a fixed first value, for example, the letter "c" from a | If there is a fixed first value, for example, the letter "c" from a |
2633 | pattern such as (cat|cow|coyote), its value is returned. In the 8-bit | pattern such as (cat|cow|coyote), its value is returned. In the 8-bit |
2634 | library, the value is always less than 256. In the 16-bit library the | library, the value is always less than 256. In the 16-bit library the |
2635 | value can be up to 0xffff. In the 32-bit library the value can be up to | value can be up to 0xffff. In the 32-bit library the value can be up to |
2636 | 0x10ffff. | 0x10ffff. |
2637 | ||
2638 | If there is no fixed first value, and if either | If there is no fixed first value, and if either |
2639 | ||
2640 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | (a) the pattern was compiled with the PCRE_MULTILINE option, and every |
2641 | branch starts with "^", or | branch starts with "^", or |
2642 | ||
2643 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
2644 | set (if it were set, the pattern would be anchored), | set (if it were set, the pattern would be anchored), |
2645 | ||
2646 | -1 is returned, indicating that the pattern matches only at the start | -1 is returned, indicating that the pattern matches only at the start |
2647 | of a subject string or after any newline within the string. Otherwise | of a subject string or after any newline within the string. Otherwise |
2648 | -2 is returned. For anchored patterns, -2 is returned. | -2 is returned. For anchored patterns, -2 is returned. |
2649 | ||
2650 | Since for the 32-bit library using the non-UTF-32 mode, this function | Since for the 32-bit library using the non-UTF-32 mode, this function |
2651 | is unable to return the full 32-bit range of the character, this value | is unable to return the full 32-bit range of the character, this value |
2652 | is deprecated; instead the PCRE_INFO_FIRSTCHARACTERFLAGS and | is deprecated; instead the PCRE_INFO_FIRSTCHARACTERFLAGS and |
2653 | PCRE_INFO_FIRSTCHARACTER values should be used. | PCRE_INFO_FIRSTCHARACTER values should be used. |
2654 | ||
2655 | PCRE_INFO_FIRSTTABLE | PCRE_INFO_FIRSTTABLE |
2656 | ||
2657 | If the pattern was studied, and this resulted in the construction of a | If the pattern was studied, and this resulted in the construction of a |
2658 | 256-bit table indicating a fixed set of values for the first data unit | 256-bit table indicating a fixed set of values for the first data unit |
2659 | in any matching string, a pointer to the table is returned. Otherwise | in any matching string, a pointer to the table is returned. Otherwise |
2660 | NULL is returned. The fourth argument should point to an unsigned char | NULL is returned. The fourth argument should point to an unsigned char |
2661 | * variable. | * variable. |
2662 | ||
2663 | PCRE_INFO_HASCRORLF | PCRE_INFO_HASCRORLF |
2664 | ||
2665 | Return 1 if the pattern contains any explicit matches for CR or LF | Return 1 if the pattern contains any explicit matches for CR or LF |
2666 | characters, otherwise 0. The fourth argument should point to an int | characters, otherwise 0. The fourth argument should point to an int |
2667 | variable. An explicit match is either a literal CR or LF character, or | variable. An explicit match is either a literal CR or LF character, or |
2668 | \r or \n. | \r or \n. |
2669 | ||
2670 | PCRE_INFO_JCHANGED | PCRE_INFO_JCHANGED |
2671 | ||
2672 | Return 1 if the (?J) or (?-J) option setting is used in the pattern, | Return 1 if the (?J) or (?-J) option setting is used in the pattern, |
2673 | otherwise 0. The fourth argument should point to an int variable. (?J) | otherwise 0. The fourth argument should point to an int variable. (?J) |
2674 | and (?-J) set and unset the local PCRE_DUPNAMES option, respectively. | and (?-J) set and unset the local PCRE_DUPNAMES option, respectively. |
2675 | ||
2676 | PCRE_INFO_JIT | PCRE_INFO_JIT |
2677 | ||
2678 | Return 1 if the pattern was studied with one of the JIT options, and | Return 1 if the pattern was studied with one of the JIT options, and |
2679 | just-in-time compiling was successful. The fourth argument should point | just-in-time compiling was successful. The fourth argument should point |
2680 | to an int variable. A return value of 0 means that JIT support is not | to an int variable. A return value of 0 means that JIT support is not |
2681 | available in this version of PCRE, or that the pattern was not studied | available in this version of PCRE, or that the pattern was not studied |
2682 | with a JIT option, or that the JIT compiler could not handle this par- | with a JIT option, or that the JIT compiler could not handle this par- |
2683 | ticular pattern. See the pcrejit documentation for details of what can | ticular pattern. See the pcrejit documentation for details of what can |
2684 | and cannot be handled. | and cannot be handled. |
2685 | ||
2686 | PCRE_INFO_JITSIZE | PCRE_INFO_JITSIZE |
2687 | ||
2688 | If the pattern was successfully studied with a JIT option, return the | If the pattern was successfully studied with a JIT option, return the |
2689 | size of the JIT compiled code, otherwise return zero. The fourth argu- | size of the JIT compiled code, otherwise return zero. The fourth argu- |
2690 | ment should point to a size_t variable. | ment should point to a size_t variable. |
2691 | ||
2692 | PCRE_INFO_LASTLITERAL | PCRE_INFO_LASTLITERAL |
2693 | ||
2694 | Return the value of the rightmost literal data unit that must exist in | Return the value of the rightmost literal data unit that must exist in |
2695 | any matched string, other than at its start, if such a value has been | any matched string, other than at its start, if such a value has been |
2696 | recorded. The fourth argument should point to an int variable. If there | recorded. The fourth argument should point to an int variable. If there |
2697 | is no such value, -1 is returned. For anchored patterns, a last literal | is no such value, -1 is returned. For anchored patterns, a last literal |
2698 | value is recorded only if it follows something of variable length. For | value is recorded only if it follows something of variable length. For |
2699 | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for | example, for the pattern /^a\d+z\d+/ the returned value is "z", but for |
2700 | /^a\dz\d/ the returned value is -1. | /^a\dz\d/ the returned value is -1. |
2701 | ||
2702 | Since for the 32-bit library using the non-UTF-32 mode, this function | Since for the 32-bit library using the non-UTF-32 mode, this function |
2703 | is unable to return the full 32-bit range of the character, this value | is unable to return the full 32-bit range of the character, this value |
2704 | is deprecated; instead the PCRE_INFO_REQUIREDCHARFLAGS and | is deprecated; instead the PCRE_INFO_REQUIREDCHARFLAGS and |
2705 | PCRE_INFO_REQUIREDCHAR values should be used. | PCRE_INFO_REQUIREDCHAR values should be used. |
2706 | ||
2707 | PCRE_INFO_MAXLOOKBEHIND | PCRE_INFO_MAXLOOKBEHIND |
2708 | ||
2709 | Return the number of characters (NB not bytes) in the longest lookbe- | Return the number of characters (NB not bytes) in the longest lookbe- |
2710 | hind assertion in the pattern. Note that the simple assertions \b and | hind assertion in the pattern. Note that the simple assertions \b and |
2711 | \B require a one-character lookbehind. This information is useful when | \B require a one-character lookbehind. This information is useful when |
2712 | doing multi-segment matching using the partial matching facilities. | doing multi-segment matching using the partial matching facilities. |
2713 | ||
2714 | PCRE_INFO_MINLENGTH | PCRE_INFO_MINLENGTH |
2715 | ||
2716 | If the pattern was studied and a minimum length for matching subject | If the pattern was studied and a minimum length for matching subject |
2717 | strings was computed, its value is returned. Otherwise the returned | strings was computed, its value is returned. Otherwise the returned |
2718 | value is -1. The value is a number of characters, which in UTF-8 mode | value is -1. The value is a number of characters, which in UTF-8 mode |
2719 | may be different from the number of bytes. The fourth argument should | may be different from the number of bytes. The fourth argument should |
2720 | point to an int variable. A non-negative value is a lower bound to the | point to an int variable. A non-negative value is a lower bound to the |
2721 | length of any matching string. There may not be any strings of that | length of any matching string. There may not be any strings of that |
2722 | length that do actually match, but every string that does match is at | length that do actually match, but every string that does match is at |
2723 | least that long. | least that long. |
2724 | ||
2725 | PCRE_INFO_NAMECOUNT | PCRE_INFO_NAMECOUNT |
2726 | PCRE_INFO_NAMEENTRYSIZE | PCRE_INFO_NAMEENTRYSIZE |
2727 | PCRE_INFO_NAMETABLE | PCRE_INFO_NAMETABLE |
2728 | ||
2729 | PCRE supports the use of named as well as numbered capturing parenthe- | PCRE supports the use of named as well as numbered capturing parenthe- |
2730 | ses. The names are just an additional way of identifying the parenthe- | ses. The names are just an additional way of identifying the parenthe- |
2731 | ses, which still acquire numbers. Several convenience functions such as | ses, which still acquire numbers. Several convenience functions such as |
2732 | pcre_get_named_substring() are provided for extracting captured sub- | pcre_get_named_substring() are provided for extracting captured sub- |
2733 | strings by name. It is also possible to extract the data directly, by | strings by name. It is also possible to extract the data directly, by |
2734 | first converting the name to a number in order to access the correct | first converting the name to a number in order to access the correct |
2735 | pointers in the output vector (described with pcre_exec() below). To do | pointers in the output vector (described with pcre_exec() below). To do |
2736 | the conversion, you need to use the name-to-number map, which is | the conversion, you need to use the name-to-number map, which is |
2737 | described by these three values. | described by these three values. |
2738 | ||
2739 | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT | The map consists of a number of fixed-size entries. PCRE_INFO_NAMECOUNT |
2740 | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size | gives the number of entries, and PCRE_INFO_NAMEENTRYSIZE gives the size |
2741 | of each entry; both of these return an int value. The entry size | of each entry; both of these return an int value. The entry size |
2742 | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns | depends on the length of the longest name. PCRE_INFO_NAMETABLE returns |
2743 | a pointer to the first entry of the table. This is a pointer to char in | a pointer to the first entry of the table. This is a pointer to char in |
2744 | the 8-bit library, where the first two bytes of each entry are the num- | the 8-bit library, where the first two bytes of each entry are the num- |
2745 | ber of the capturing parenthesis, most significant byte first. In the | ber of the capturing parenthesis, most significant byte first. In the |
2746 | 16-bit library, the pointer points to 16-bit data units, the first of | 16-bit library, the pointer points to 16-bit data units, the first of |
2747 | which contains the parenthesis number. In the 32-bit library, the | which contains the parenthesis number. In the 32-bit library, the |
2748 | pointer points to 32-bit data units, the first of which contains the | pointer points to 32-bit data units, the first of which contains the |
2749 | parenthesis number. The rest of the entry is the corresponding name, | parenthesis number. The rest of the entry is the corresponding name, |
2750 | zero terminated. | zero terminated. |
2751 | ||
2752 | The names are in alphabetical order. Duplicate names may appear if (?| | The names are in alphabetical order. Duplicate names may appear if (?| |
2753 | is used to create multiple groups with the same number, as described in | is used to create multiple groups with the same number, as described in |
2754 | the section on duplicate subpattern numbers in the pcrepattern page. | the section on duplicate subpattern numbers in the pcrepattern page. |
2755 | Duplicate names for subpatterns with different numbers are permitted | Duplicate names for subpatterns with different numbers are permitted |
2756 | only if PCRE_DUPNAMES is set. In all cases of duplicate names, they | only if PCRE_DUPNAMES is set. In all cases of duplicate names, they |
2757 | appear in the table in the order in which they were found in the pat- | appear in the table in the order in which they were found in the pat- |
2758 | tern. In the absence of (?| this is the order of increasing number; | tern. In the absence of (?| this is the order of increasing number; |
2759 | when (?| is used this is not necessarily the case because later subpat- | when (?| is used this is not necessarily the case because later subpat- |
2760 | terns may have lower numbers. | terns may have lower numbers. |
2761 | ||
2762 | As a simple example of the name/number table, consider the following | As a simple example of the name/number table, consider the following |
2763 | pattern after compilation by the 8-bit library (assume PCRE_EXTENDED is | pattern after compilation by the 8-bit library (assume PCRE_EXTENDED is |
2764 | set, so white space - including newlines - is ignored): | set, so white space - including newlines - is ignored): |
2765 | ||
2766 | (?<date> (?<year>(\d\d)?\d\d) - | (?<date> (?<year>(\d\d)?\d\d) - |
2767 | (?<month>\d\d) - (?<day>\d\d) ) | (?<month>\d\d) - (?<day>\d\d) ) |
2768 | ||
2769 | There are four named subpatterns, so the table has four entries, and | There are four named subpatterns, so the table has four entries, and |
2770 | each entry in the table is eight bytes long. The table is as follows, | each entry in the table is eight bytes long. The table is as follows, |
2771 | with non-printing bytes shows in hexadecimal, and undefined bytes shown | with non-printing bytes shows in hexadecimal, and undefined bytes shown |
2772 | as ??: | as ??: |
2773 | ||
# | Line 2775 INFORMATION ABOUT A PATTERN | Line 2776 INFORMATION ABOUT A PATTERN |
2776 | 00 04 m o n t h 00 | 00 04 m o n t h 00 |
2777 | 00 02 y e a r 00 ?? | 00 02 y e a r 00 ?? |
2778 | ||
2779 | When writing code to extract data from named subpatterns using the | When writing code to extract data from named subpatterns using the |
2780 | name-to-number map, remember that the length of the entries is likely | name-to-number map, remember that the length of the entries is likely |
2781 | to be different for each compiled pattern. | to be different for each compiled pattern. |
2782 | ||
2783 | PCRE_INFO_OKPARTIAL | PCRE_INFO_OKPARTIAL |
2784 | ||
2785 | Return 1 if the pattern can be used for partial matching with | Return 1 if the pattern can be used for partial matching with |
2786 | pcre_exec(), otherwise 0. The fourth argument should point to an int | pcre_exec(), otherwise 0. The fourth argument should point to an int |
2787 | variable. From release 8.00, this always returns 1, because the | variable. From release 8.00, this always returns 1, because the |
2788 | restrictions that previously applied to partial matching have been | restrictions that previously applied to partial matching have been |
2789 | lifted. The pcrepartial documentation gives details of partial match- | lifted. The pcrepartial documentation gives details of partial match- |
2790 | ing. | ing. |
2791 | ||
2792 | PCRE_INFO_OPTIONS | PCRE_INFO_OPTIONS |
2793 | ||
2794 | Return a copy of the options with which the pattern was compiled. The | Return a copy of the options with which the pattern was compiled. The |
2795 | fourth argument should point to an unsigned long int variable. These | fourth argument should point to an unsigned long int variable. These |
2796 | option bits are those specified in the call to pcre_compile(), modified | option bits are those specified in the call to pcre_compile(), modified |
2797 | by any top-level option settings at the start of the pattern itself. In | by any top-level option settings at the start of the pattern itself. In |
2798 | other words, they are the options that will be in force when matching | other words, they are the options that will be in force when matching |
2799 | starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with | starts. For example, if the pattern /(?im)abc(?-i)d/ is compiled with |
2800 | the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE, | the PCRE_EXTENDED option, the result is PCRE_CASELESS, PCRE_MULTILINE, |
2801 | and PCRE_EXTENDED. | and PCRE_EXTENDED. |
2802 | ||
2803 | A pattern is automatically anchored by PCRE if all of its top-level | A pattern is automatically anchored by PCRE if all of its top-level |
2804 | alternatives begin with one of the following: | alternatives begin with one of the following: |
2805 | ||
2806 | ^ unless PCRE_MULTILINE is set | ^ unless PCRE_MULTILINE is set |
# | Line 2813 INFORMATION ABOUT A PATTERN | Line 2814 INFORMATION ABOUT A PATTERN |
2814 | ||
2815 | PCRE_INFO_SIZE | PCRE_INFO_SIZE |
2816 | ||
2817 | Return the size of the compiled pattern in bytes (for both libraries). | Return the size of the compiled pattern in bytes (for both libraries). |
2818 | The fourth argument should point to a size_t variable. This value does | The fourth argument should point to a size_t variable. This value does |
2819 | not include the size of the pcre structure that is returned by | not include the size of the pcre structure that is returned by |
2820 | pcre_compile(). The value that is passed as the argument to pcre_mal- | pcre_compile(). The value that is passed as the argument to pcre_mal- |
2821 | loc() when pcre_compile() is getting memory in which to place the com- | loc() when pcre_compile() is getting memory in which to place the com- |
2822 | piled data is the value returned by this option plus the size of the | piled data is the value returned by this option plus the size of the |
2823 | pcre structure. Studying a compiled pattern, with or without JIT, does | pcre structure. Studying a compiled pattern, with or without JIT, does |
2824 | not alter the value returned by this option. | not alter the value returned by this option. |
2825 | ||
2826 | PCRE_INFO_STUDYSIZE | PCRE_INFO_STUDYSIZE |
2827 | ||
2828 | Return the size in bytes of the data block pointed to by the study_data | Return the size in bytes of the data block pointed to by the study_data |
2829 | field in a pcre_extra block. If pcre_extra is NULL, or there is no | field in a pcre_extra block. If pcre_extra is NULL, or there is no |
2830 | study data, zero is returned. The fourth argument should point to a | study data, zero is returned. The fourth argument should point to a |
2831 | size_t variable. The study_data field is set by pcre_study() to record | size_t variable. The study_data field is set by pcre_study() to record |
2832 | information that will speed up matching (see the section entitled | information that will speed up matching (see the section entitled |
2833 | "Studying a pattern" above). The format of the study_data block is pri- | "Studying a pattern" above). The format of the study_data block is pri- |
2834 | vate, but its length is made available via this option so that it can | vate, but its length is made available via this option so that it can |
2835 | be saved and restored (see the pcreprecompile documentation for | be saved and restored (see the pcreprecompile documentation for |
2836 | details). | details). |
2837 | ||
2838 | PCRE_INFO_FIRSTCHARACTERFLAGS | PCRE_INFO_FIRSTCHARACTERFLAGS |
2839 | ||
2840 | Return information about the first data unit of any matched string, for | Return information about the first data unit of any matched string, for |
2841 | a non-anchored pattern. The fourth argument should point to an int | a non-anchored pattern. The fourth argument should point to an int |
2842 | variable. | variable. |
2843 | ||
2844 | If there is a fixed first value, for example, the letter "c" from a | If there is a fixed first value, for example, the letter "c" from a |
2845 | pattern such as (cat|cow|coyote), 1 is returned, and the character | pattern such as (cat|cow|coyote), 1 is returned, and the character |
2846 | value can be retrieved using PCRE_INFO_FIRSTCHARACTER. | value can be retrieved using PCRE_INFO_FIRSTCHARACTER. |
2847 | ||
2848 | If there is no fixed first value, and if either | If there is no fixed first value, and if either |
2849 | ||
2850 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | (a) the pattern was compiled with the PCRE_MULTILINE option, and every |
2851 | branch starts with "^", or | branch starts with "^", or |
2852 | ||
2853 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
# | Line 2858 INFORMATION ABOUT A PATTERN | Line 2859 INFORMATION ABOUT A PATTERN |
2859 | ||
2860 | PCRE_INFO_FIRSTCHARACTER | PCRE_INFO_FIRSTCHARACTER |
2861 | ||
2862 | Return the fixed first character value, if PCRE_INFO_FIRSTCHARACTER- | Return the fixed first character value, if PCRE_INFO_FIRSTCHARACTER- |
2863 | FLAGS returned 1; otherwise returns 0. The fourth argument should point | FLAGS returned 1; otherwise returns 0. The fourth argument should point |
2864 | to an uint_t variable. | to an uint_t variable. |
2865 | ||
2866 | In the 8-bit library, the value is always less than 256. In the 16-bit | In the 8-bit library, the value is always less than 256. In the 16-bit |
2867 | library the value can be up to 0xffff. In the 32-bit library in UTF-32 | library the value can be up to 0xffff. In the 32-bit library in UTF-32 |
2868 | mode the value can be up to 0x10ffff, and up to 0xffffffff when not | mode the value can be up to 0x10ffff, and up to 0xffffffff when not |
2869 | using UTF-32 mode. | using UTF-32 mode. |
2870 | ||
2871 | If there is no fixed first value, and if either | If there is no fixed first value, and if either |
2872 | ||
2873 | (a) the pattern was compiled with the PCRE_MULTILINE option, and every | (a) the pattern was compiled with the PCRE_MULTILINE option, and every |
2874 | branch starts with "^", or | branch starts with "^", or |
2875 | ||
2876 | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not | (b) every branch of the pattern starts with ".*" and PCRE_DOTALL is not |
2877 | set (if it were set, the pattern would be anchored), | set (if it were set, the pattern would be anchored), |
2878 | ||
2879 | -1 is returned, indicating that the pattern matches only at the start | -1 is returned, indicating that the pattern matches only at the start |
2880 | of a subject string or after any newline within the string. Otherwise | of a subject string or after any newline within the string. Otherwise |
2881 | -2 is returned. For anchored patterns, -2 is returned. | -2 is returned. For anchored patterns, -2 is returned. |
2882 | ||
2883 | PCRE_INFO_REQUIREDCHARFLAGS | PCRE_INFO_REQUIREDCHARFLAGS |
2884 | ||
2885 | Returns 1 if there is a rightmost literal data unit that must exist in | Returns 1 if there is a rightmost literal data unit that must exist in |
2886 | any matched string, other than at its start. The fourth argument should | any matched string, other than at its start. The fourth argument should |
2887 | point to an int variable. If there is no such value, 0 is returned. If | point to an int variable. If there is no such value, 0 is returned. If |
2888 | returning 1, the character value itself can be retrieved using | returning 1, the character value itself can be retrieved using |
2889 | PCRE_INFO_REQUIREDCHAR. | PCRE_INFO_REQUIREDCHAR. |
2890 | ||
2891 | For anchored patterns, a last literal value is recorded only if it fol- | For anchored patterns, a last literal value is recorded only if it fol- |
2892 | lows something of variable length. For example, for the pattern | lows something of variable length. For example, for the pattern |
2893 | /^a\d+z\d+/ the returned value 1 (with "z" returned from | /^a\d+z\d+/ the returned value 1 (with "z" returned from |
2894 | PCRE_INFO_REQUIREDCHAR), but for /^a\dz\d/ the returned value is 0. | PCRE_INFO_REQUIREDCHAR), but for /^a\dz\d/ the returned value is 0. |
2895 | ||
2896 | PCRE_INFO_REQUIREDCHAR | PCRE_INFO_REQUIREDCHAR |
2897 | ||
2898 | Return the value of the rightmost literal data unit that must exist in | Return the value of the rightmost literal data unit that must exist in |
2899 | any matched string, other than at its start, if such a value has been | any matched string, other than at its start, if such a value has been |
2900 | recorded. The fourth argument should point to an uint32_t variable. If | recorded. The fourth argument should point to an uint32_t variable. If |
2901 | there is no such value, 0 is returned. | there is no such value, 0 is returned. |
2902 | ||
2903 | ||
# | Line 2904 REFERENCE COUNTS | Line 2905 REFERENCE COUNTS |
2905 | ||
2906 | int pcre_refcount(pcre *code, int adjust); | int pcre_refcount(pcre *code, int adjust); |
2907 | ||
2908 | The pcre_refcount() function is used to maintain a reference count in | The pcre_refcount() function is used to maintain a reference count in |
2909 | the data block that contains a compiled pattern. It is provided for the | the data block that contains a compiled pattern. It is provided for the |
2910 | benefit of applications that operate in an object-oriented manner, | benefit of applications that operate in an object-oriented manner, |
2911 | where different parts of the application may be using the same compiled | where different parts of the application may be using the same compiled |
2912 | pattern, but you want to free the block when they are all done. | pattern, but you want to free the block when they are all done. |
2913 | ||
2914 | When a pattern is compiled, the reference count field is initialized to | When a pattern is compiled, the reference count field is initialized to |
2915 | zero. It is changed only by calling this function, whose action is to | zero. It is changed only by calling this function, whose action is to |
2916 | add the adjust value (which may be positive or negative) to it. The | add the adjust value (which may be positive or negative) to it. The |
2917 | yield of the function is the new value. However, the value of the count | yield of the function is the new value. However, the value of the count |
2918 | is constrained to lie between 0 and 65535, inclusive. If the new value | is constrained to lie between 0 and 65535, inclusive. If the new value |
2919 | is outside these limits, it is forced to the appropriate limit value. | is outside these limits, it is forced to the appropriate limit value. |
2920 | ||
2921 | Except when it is zero, the reference count is not correctly preserved | Except when it is zero, the reference count is not correctly preserved |
2922 | if a pattern is compiled on one host and then transferred to a host | if a pattern is compiled on one host and then transferred to a host |
2923 | whose byte-order is different. (This seems a highly unlikely scenario.) | whose byte-order is different. (This seems a highly unlikely scenario.) |
2924 | ||
2925 | ||
# | Line 2928 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 2929 MATCHING A PATTERN: THE TRADITIONAL FUNC |
2929 | const char *subject, int length, int startoffset, | const char *subject, int length, int startoffset, |
2930 | int options, int *ovector, int ovecsize); | int options, int *ovector, int ovecsize); |
2931 | ||
2932 | The function pcre_exec() is called to match a subject string against a | The function pcre_exec() is called to match a subject string against a |
2933 | compiled pattern, which is passed in the code argument. If the pattern | compiled pattern, which is passed in the code argument. If the pattern |
2934 | was studied, the result of the study should be passed in the extra | was studied, the result of the study should be passed in the extra |
2935 | argument. You can call pcre_exec() with the same code and extra argu- | argument. You can call pcre_exec() with the same code and extra argu- |
2936 | ments as many times as you like, in order to match different subject | ments as many times as you like, in order to match different subject |
2937 | strings with the same pattern. | strings with the same pattern. |
2938 | ||
2939 | This function is the main matching facility of the library, and it | This function is the main matching facility of the library, and it |
2940 | operates in a Perl-like manner. For specialist use there is also an | operates in a Perl-like manner. For specialist use there is also an |
2941 | alternative matching function, which is described below in the section | alternative matching function, which is described below in the section |
2942 | about the pcre_dfa_exec() function. | about the pcre_dfa_exec() function. |
2943 | ||
2944 | In most applications, the pattern will have been compiled (and option- | In most applications, the pattern will have been compiled (and option- |
2945 | ally studied) in the same process that calls pcre_exec(). However, it | ally studied) in the same process that calls pcre_exec(). However, it |
2946 | is possible to save compiled patterns and study data, and then use them | is possible to save compiled patterns and study data, and then use them |
2947 | later in different processes, possibly even on different hosts. For a | later in different processes, possibly even on different hosts. For a |
2948 | discussion about this, see the pcreprecompile documentation. | discussion about this, see the pcreprecompile documentation. |
2949 | ||
2950 | Here is an example of a simple call to pcre_exec(): | Here is an example of a simple call to pcre_exec(): |
# | Line 2962 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 2963 MATCHING A PATTERN: THE TRADITIONAL FUNC |
2963 | ||
2964 | Extra data for pcre_exec() | Extra data for pcre_exec() |
2965 | ||
2966 | If the extra argument is not NULL, it must point to a pcre_extra data | If the extra argument is not NULL, it must point to a pcre_extra data |
2967 | block. The pcre_study() function returns such a block (when it doesn't | block. The pcre_study() function returns such a block (when it doesn't |
2968 | return NULL), but you can also create one for yourself, and pass addi- | return NULL), but you can also create one for yourself, and pass addi- |
2969 | tional information in it. The pcre_extra block contains the following | tional information in it. The pcre_extra block contains the following |
2970 | fields (not necessarily in this order): | fields (not necessarily in this order): |
2971 | ||
2972 | unsigned long int flags; | unsigned long int flags; |
# | Line 2977 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 2978 MATCHING A PATTERN: THE TRADITIONAL FUNC |
2978 | const unsigned char *tables; | const unsigned char *tables; |
2979 | unsigned char **mark; | unsigned char **mark; |
2980 | ||
2981 | In the 16-bit version of this structure, the mark field has type | In the 16-bit version of this structure, the mark field has type |
2982 | "PCRE_UCHAR16 **". | "PCRE_UCHAR16 **". |
2983 | ||
2984 | In the 32-bit version of this structure, the mark field has type | In the 32-bit version of this structure, the mark field has type |
2985 | "PCRE_UCHAR32 **". | "PCRE_UCHAR32 **". |
2986 | ||
2987 | The flags field is used to specify which of the other fields are set. | The flags field is used to specify which of the other fields are set. |
2988 | The flag bits are: | The flag bits are: |
2989 | ||
2990 | PCRE_EXTRA_CALLOUT_DATA | PCRE_EXTRA_CALLOUT_DATA |
# | Line 2994 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 2995 MATCHING A PATTERN: THE TRADITIONAL FUNC |
2995 | PCRE_EXTRA_STUDY_DATA | PCRE_EXTRA_STUDY_DATA |
2996 | PCRE_EXTRA_TABLES | PCRE_EXTRA_TABLES |
2997 | ||
2998 | Other flag bits should be set to zero. The study_data field and some- | Other flag bits should be set to zero. The study_data field and some- |
2999 | times the executable_jit field are set in the pcre_extra block that is | times the executable_jit field are set in the pcre_extra block that is |
3000 | returned by pcre_study(), together with the appropriate flag bits. You | returned by pcre_study(), together with the appropriate flag bits. You |
3001 | should not set these yourself, but you may add to the block by setting | should not set these yourself, but you may add to the block by setting |
3002 | other fields and their corresponding flag bits. | other fields and their corresponding flag bits. |
3003 | ||
3004 | The match_limit field provides a means of preventing PCRE from using up | The match_limit field provides a means of preventing PCRE from using up |
3005 | a vast amount of resources when running patterns that are not going to | a vast amount of resources when running patterns that are not going to |
3006 | match, but which have a very large number of possibilities in their | match, but which have a very large number of possibilities in their |
3007 | search trees. The classic example is a pattern that uses nested unlim- | search trees. The classic example is a pattern that uses nested unlim- |
3008 | ited repeats. | ited repeats. |
3009 | ||
3010 | Internally, pcre_exec() uses a function called match(), which it calls | Internally, pcre_exec() uses a function called match(), which it calls |
3011 | repeatedly (sometimes recursively). The limit set by match_limit is | repeatedly (sometimes recursively). The limit set by match_limit is |
3012 | imposed on the number of times this function is called during a match, | imposed on the number of times this function is called during a match, |
3013 | which has the effect of limiting the amount of backtracking that can | which has the effect of limiting the amount of backtracking that can |
3014 | take place. For patterns that are not anchored, the count restarts from | take place. For patterns that are not anchored, the count restarts from |
3015 | zero for each position in the subject string. | zero for each position in the subject string. |
3016 | ||
3017 | When pcre_exec() is called with a pattern that was successfully studied | When pcre_exec() is called with a pattern that was successfully studied |
3018 | with a JIT option, the way that the matching is executed is entirely | with a JIT option, the way that the matching is executed is entirely |
3019 | different. However, there is still the possibility of runaway matching | different. However, there is still the possibility of runaway matching |
3020 | that goes on for a very long time, and so the match_limit value is also | that goes on for a very long time, and so the match_limit value is also |
3021 | used in this case (but in a different way) to limit how long the match- | used in this case (but in a different way) to limit how long the match- |
3022 | ing can continue. | ing can continue. |
3023 | ||
3024 | The default value for the limit can be set when PCRE is built; the | The default value for the limit can be set when PCRE is built; the |
3025 | default default is 10 million, which handles all but the most extreme | default default is 10 million, which handles all but the most extreme |
3026 | cases. You can override the default by suppling pcre_exec() with a | cases. You can override the default by suppling pcre_exec() with a |
3027 | pcre_extra block in which match_limit is set, and | pcre_extra block in which match_limit is set, and |
3028 | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is | PCRE_EXTRA_MATCH_LIMIT is set in the flags field. If the limit is |
3029 | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. | exceeded, pcre_exec() returns PCRE_ERROR_MATCHLIMIT. |
3030 | ||
3031 | The match_limit_recursion field is similar to match_limit, but instead | The match_limit_recursion field is similar to match_limit, but instead |
3032 | of limiting the total number of times that match() is called, it limits | of limiting the total number of times that match() is called, it limits |
3033 | the depth of recursion. The recursion depth is a smaller number than | the depth of recursion. The recursion depth is a smaller number than |
3034 | the total number of calls, because not all calls to match() are recur- | the total number of calls, because not all calls to match() are recur- |
3035 | sive. This limit is of use only if it is set smaller than match_limit. | sive. This limit is of use only if it is set smaller than match_limit. |
3036 | ||
3037 | Limiting the recursion depth limits the amount of machine stack that | Limiting the recursion depth limits the amount of machine stack that |
3038 | can be used, or, when PCRE has been compiled to use memory on the heap | can be used, or, when PCRE has been compiled to use memory on the heap |
3039 | instead of the stack, the amount of heap memory that can be used. This | instead of the stack, the amount of heap memory that can be used. This |
3040 | limit is not relevant, and is ignored, when matching is done using JIT | limit is not relevant, and is ignored, when matching is done using JIT |
3041 | compiled code. | compiled code. |
3042 | ||
3043 | The default value for match_limit_recursion can be set when PCRE is | The default value for match_limit_recursion can be set when PCRE is |
3044 | built; the default default is the same value as the default for | built; the default default is the same value as the default for |
3045 | match_limit. You can override the default by suppling pcre_exec() with | match_limit. You can override the default by suppling pcre_exec() with |
3046 | a pcre_extra block in which match_limit_recursion is set, and | a pcre_extra block in which match_limit_recursion is set, and |
3047 | PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the | PCRE_EXTRA_MATCH_LIMIT_RECURSION is set in the flags field. If the |
3048 | limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT. | limit is exceeded, pcre_exec() returns PCRE_ERROR_RECURSIONLIMIT. |
3049 | ||
3050 | The callout_data field is used in conjunction with the "callout" fea- | The callout_data field is used in conjunction with the "callout" fea- |
3051 | ture, and is described in the pcrecallout documentation. | ture, and is described in the pcrecallout documentation. |
3052 | ||
3053 | The tables field is used to pass a character tables pointer to | The tables field is used to pass a character tables pointer to |
3054 | pcre_exec(); this overrides the value that is stored with the compiled | pcre_exec(); this overrides the value that is stored with the compiled |
3055 | pattern. A non-NULL value is stored with the compiled pattern only if | pattern. A non-NULL value is stored with the compiled pattern only if |
3056 | custom tables were supplied to pcre_compile() via its tableptr argu- | custom tables were supplied to pcre_compile() via its tableptr argu- |
3057 | ment. If NULL is passed to pcre_exec() using this mechanism, it forces | ment. If NULL is passed to pcre_exec() using this mechanism, it forces |
3058 | PCRE's internal tables to be used. This facility is helpful when re- | PCRE's internal tables to be used. This facility is helpful when re- |
3059 | using patterns that have been saved after compiling with an external | using patterns that have been saved after compiling with an external |
3060 | set of tables, because the external tables might be at a different | set of tables, because the external tables might be at a different |
3061 | address when pcre_exec() is called. See the pcreprecompile documenta- | address when pcre_exec() is called. See the pcreprecompile documenta- |
3062 | tion for a discussion of saving compiled patterns for later use. | tion for a discussion of saving compiled patterns for later use. |
3063 | ||
3064 | If PCRE_EXTRA_MARK is set in the flags field, the mark field must be | If PCRE_EXTRA_MARK is set in the flags field, the mark field must be |
3065 | set to point to a suitable variable. If the pattern contains any back- | set to point to a suitable variable. If the pattern contains any back- |
3066 | tracking control verbs such as (*MARK:NAME), and the execution ends up | tracking control verbs such as (*MARK:NAME), and the execution ends up |
3067 | with a name to pass back, a pointer to the name string (zero termi- | with a name to pass back, a pointer to the name string (zero termi- |
3068 | nated) is placed in the variable pointed to by the mark field. The | nated) is placed in the variable pointed to by the mark field. The |
3069 | names are within the compiled pattern; if you wish to retain such a | names are within the compiled pattern; if you wish to retain such a |
3070 | name you must copy it before freeing the memory of a compiled pattern. | name you must copy it before freeing the memory of a compiled pattern. |
3071 | If there is no name to pass back, the variable pointed to by the mark | If there is no name to pass back, the variable pointed to by the mark |
3072 | field is set to NULL. For details of the backtracking control verbs, | field is set to NULL. For details of the backtracking control verbs, |
3073 | see the section entitled "Backtracking control" in the pcrepattern doc- | see the section entitled "Backtracking control" in the pcrepattern doc- |
3074 | umentation. | umentation. |
3075 | ||
3076 | Option bits for pcre_exec() | Option bits for pcre_exec() |
3077 | ||
3078 | The unused bits of the options argument for pcre_exec() must be zero. | The unused bits of the options argument for pcre_exec() must be zero. |
3079 | The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx, | The only bits that may be set are PCRE_ANCHORED, PCRE_NEWLINE_xxx, |
3080 | PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, | PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, PCRE_NOTEMPTY_ATSTART, |
3081 | PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, and | PCRE_NO_START_OPTIMIZE, PCRE_NO_UTF8_CHECK, PCRE_PARTIAL_HARD, and |
3082 | PCRE_PARTIAL_SOFT. | PCRE_PARTIAL_SOFT. |
3083 | ||
3084 | If the pattern was successfully studied with one of the just-in-time | If the pattern was successfully studied with one of the just-in-time |
3085 | (JIT) compile options, the only supported options for JIT execution are | (JIT) compile options, the only supported options for JIT execution are |
3086 | PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, | PCRE_NO_UTF8_CHECK, PCRE_NOTBOL, PCRE_NOTEOL, PCRE_NOTEMPTY, |
3087 | PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. If an | PCRE_NOTEMPTY_ATSTART, PCRE_PARTIAL_HARD, and PCRE_PARTIAL_SOFT. If an |
3088 | unsupported option is used, JIT execution is disabled and the normal | unsupported option is used, JIT execution is disabled and the normal |
3089 | interpretive code in pcre_exec() is run. | interpretive code in pcre_exec() is run. |
3090 | ||
3091 | PCRE_ANCHORED | PCRE_ANCHORED |
3092 | ||
3093 | The PCRE_ANCHORED option limits pcre_exec() to matching at the first | The PCRE_ANCHORED option limits pcre_exec() to matching at the first |
3094 | matching position. If a pattern was compiled with PCRE_ANCHORED, or | matching position. If a pattern was compiled with PCRE_ANCHORED, or |
3095 | turned out to be anchored by virtue of its contents, it cannot be made | turned out to be anchored by virtue of its contents, it cannot be made |
3096 | unachored at matching time. | unachored at matching time. |
3097 | ||
3098 | PCRE_BSR_ANYCRLF | PCRE_BSR_ANYCRLF |
3099 | PCRE_BSR_UNICODE | PCRE_BSR_UNICODE |
3100 | ||
3101 | These options (which are mutually exclusive) control what the \R escape | These options (which are mutually exclusive) control what the \R escape |
3102 | sequence matches. The choice is either to match only CR, LF, or CRLF, | sequence matches. The choice is either to match only CR, LF, or CRLF, |
3103 | or to match any Unicode newline sequence. These options override the | or to match any Unicode newline sequence. These options override the |
3104 | choice that was made or defaulted when the pattern was compiled. | choice that was made or defaulted when the pattern was compiled. |
3105 | ||
3106 | PCRE_NEWLINE_CR | PCRE_NEWLINE_CR |
# | Line 3108 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3109 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3109 | PCRE_NEWLINE_ANYCRLF | PCRE_NEWLINE_ANYCRLF |
3110 | PCRE_NEWLINE_ANY | PCRE_NEWLINE_ANY |
3111 | ||
3112 | These options override the newline definition that was chosen or | These options override the newline definition that was chosen or |
3113 | defaulted when the pattern was compiled. For details, see the descrip- | defaulted when the pattern was compiled. For details, see the descrip- |
3114 | tion of pcre_compile() above. During matching, the newline choice | tion of pcre_compile() above. During matching, the newline choice |
3115 | affects the behaviour of the dot, circumflex, and dollar metacharac- | affects the behaviour of the dot, circumflex, and dollar metacharac- |
3116 | ters. It may also alter the way the match position is advanced after a | ters. It may also alter the way the match position is advanced after a |
3117 | match failure for an unanchored pattern. | match failure for an unanchored pattern. |
3118 | ||
3119 | When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is | When PCRE_NEWLINE_CRLF, PCRE_NEWLINE_ANYCRLF, or PCRE_NEWLINE_ANY is |
3120 | set, and a match attempt for an unanchored pattern fails when the cur- | set, and a match attempt for an unanchored pattern fails when the cur- |
3121 | rent position is at a CRLF sequence, and the pattern contains no | rent position is at a CRLF sequence, and the pattern contains no |
3122 | explicit matches for CR or LF characters, the match position is | explicit matches for CR or LF characters, the match position is |
3123 | advanced by two characters instead of one, in other words, to after the | advanced by two characters instead of one, in other words, to after the |
3124 | CRLF. | CRLF. |
3125 | ||
3126 | The above rule is a compromise that makes the most common cases work as | The above rule is a compromise that makes the most common cases work as |
3127 | expected. For example, if the pattern is .+A (and the PCRE_DOTALL | expected. For example, if the pattern is .+A (and the PCRE_DOTALL |
3128 | option is not set), it does not match the string "\r\nA" because, after | option is not set), it does not match the string "\r\nA" because, after |
3129 | failing at the start, it skips both the CR and the LF before retrying. | failing at the start, it skips both the CR and the LF before retrying. |
3130 | However, the pattern [\r\n]A does match that string, because it con- | However, the pattern [\r\n]A does match that string, because it con- |
3131 | tains an explicit CR or LF reference, and so advances only by one char- | tains an explicit CR or LF reference, and so advances only by one char- |
3132 | acter after the first failure. | acter after the first failure. |
3133 | ||
3134 | An explicit match for CR of LF is either a literal appearance of one of | An explicit match for CR of LF is either a literal appearance of one of |
3135 | those characters, or one of the \r or \n escape sequences. Implicit | those characters, or one of the \r or \n escape sequences. Implicit |
3136 | matches such as [^X] do not count, nor does \s (which includes CR and | matches such as [^X] do not count, nor does \s (which includes CR and |
3137 | LF in the characters that it matches). | LF in the characters that it matches). |
3138 | ||
3139 | Notwithstanding the above, anomalous effects may still occur when CRLF | Notwithstanding the above, anomalous effects may still occur when CRLF |
3140 | is a valid newline sequence and explicit \r or \n escapes appear in the | is a valid newline sequence and explicit \r or \n escapes appear in the |
3141 | pattern. | pattern. |
3142 | ||
3143 | PCRE_NOTBOL | PCRE_NOTBOL |
3144 | ||
3145 | This option specifies that first character of the subject string is not | This option specifies that first character of the subject string is not |
3146 | the beginning of a line, so the circumflex metacharacter should not | the beginning of a line, so the circumflex metacharacter should not |
3147 | match before it. Setting this without PCRE_MULTILINE (at compile time) | match before it. Setting this without PCRE_MULTILINE (at compile time) |
3148 | causes circumflex never to match. This option affects only the behav- | causes circumflex never to match. This option affects only the behav- |
3149 | iour of the circumflex metacharacter. It does not affect \A. | iour of the circumflex metacharacter. It does not affect \A. |
3150 | ||
3151 | PCRE_NOTEOL | PCRE_NOTEOL |
3152 | ||
3153 | This option specifies that the end of the subject string is not the end | This option specifies that the end of the subject string is not the end |
3154 | of a line, so the dollar metacharacter should not match it nor (except | of a line, so the dollar metacharacter should not match it nor (except |
3155 | in multiline mode) a newline immediately before it. Setting this with- | in multiline mode) a newline immediately before it. Setting this with- |
3156 | out PCRE_MULTILINE (at compile time) causes dollar never to match. This | out PCRE_MULTILINE (at compile time) causes dollar never to match. This |
3157 | option affects only the behaviour of the dollar metacharacter. It does | option affects only the behaviour of the dollar metacharacter. It does |
3158 | not affect \Z or \z. | not affect \Z or \z. |
3159 | ||
3160 | PCRE_NOTEMPTY | PCRE_NOTEMPTY |
3161 | ||
3162 | An empty string is not considered to be a valid match if this option is | An empty string is not considered to be a valid match if this option is |
3163 | set. If there are alternatives in the pattern, they are tried. If all | set. If there are alternatives in the pattern, they are tried. If all |
3164 | the alternatives match the empty string, the entire match fails. For | the alternatives match the empty string, the entire match fails. For |
3165 | example, if the pattern | example, if the pattern |
3166 | ||
3167 | a?b? | a?b? |
3168 | ||
3169 | is applied to a string not beginning with "a" or "b", it matches an | is applied to a string not beginning with "a" or "b", it matches an |
3170 | empty string at the start of the subject. With PCRE_NOTEMPTY set, this | empty string at the start of the subject. With PCRE_NOTEMPTY set, this |
3171 | match is not valid, so PCRE searches further into the string for occur- | match is not valid, so PCRE searches further into the string for occur- |
3172 | rences of "a" or "b". | rences of "a" or "b". |
3173 | ||
3174 | PCRE_NOTEMPTY_ATSTART | PCRE_NOTEMPTY_ATSTART |
3175 | ||
3176 | This is like PCRE_NOTEMPTY, except that an empty string match that is | This is like PCRE_NOTEMPTY, except that an empty string match that is |
3177 | not at the start of the subject is permitted. If the pattern is | not at the start of the subject is permitted. If the pattern is |
3178 | anchored, such a match can occur only if the pattern contains \K. | anchored, such a match can occur only if the pattern contains \K. |
3179 | ||
3180 | Perl has no direct equivalent of PCRE_NOTEMPTY or | Perl has no direct equivalent of PCRE_NOTEMPTY or |
3181 | PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern | PCRE_NOTEMPTY_ATSTART, but it does make a special case of a pattern |
3182 | match of the empty string within its split() function, and when using | match of the empty string within its split() function, and when using |
3183 | the /g modifier. It is possible to emulate Perl's behaviour after | the /g modifier. It is possible to emulate Perl's behaviour after |
3184 | matching a null string by first trying the match again at the same off- | matching a null string by first trying the match again at the same off- |
3185 | set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that | set with PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED, and then if that |
3186 | fails, by advancing the starting offset (see below) and trying an ordi- | fails, by advancing the starting offset (see below) and trying an ordi- |
3187 | nary match again. There is some code that demonstrates how to do this | nary match again. There is some code that demonstrates how to do this |
3188 | in the pcredemo sample program. In the most general case, you have to | in the pcredemo sample program. In the most general case, you have to |
3189 | check to see if the newline convention recognizes CRLF as a newline, | check to see if the newline convention recognizes CRLF as a newline, |
3190 | and if so, and the current character is CR followed by LF, advance the | and if so, and the current character is CR followed by LF, advance the |
3191 | starting offset by two characters instead of one. | starting offset by two characters instead of one. |
3192 | ||
3193 | PCRE_NO_START_OPTIMIZE | PCRE_NO_START_OPTIMIZE |
3194 | ||
3195 | There are a number of optimizations that pcre_exec() uses at the start | There are a number of optimizations that pcre_exec() uses at the start |
3196 | of a match, in order to speed up the process. For example, if it is | of a match, in order to speed up the process. For example, if it is |
3197 | known that an unanchored match must start with a specific character, it | known that an unanchored match must start with a specific character, it |
3198 | searches the subject for that character, and fails immediately if it | searches the subject for that character, and fails immediately if it |
3199 | cannot find it, without actually running the main matching function. | cannot find it, without actually running the main matching function. |
3200 | This means that a special item such as (*COMMIT) at the start of a pat- | This means that a special item such as (*COMMIT) at the start of a pat- |
3201 | tern is not considered until after a suitable starting point for the | tern is not considered until after a suitable starting point for the |
3202 | match has been found. When callouts or (*MARK) items are in use, these | match has been found. When callouts or (*MARK) items are in use, these |
3203 | "start-up" optimizations can cause them to be skipped if the pattern is | "start-up" optimizations can cause them to be skipped if the pattern is |
3204 | never actually used. The start-up optimizations are in effect a pre- | never actually used. The start-up optimizations are in effect a pre- |
3205 | scan of the subject that takes place before the pattern is run. | scan of the subject that takes place before the pattern is run. |
3206 | ||
3207 | The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations, | The PCRE_NO_START_OPTIMIZE option disables the start-up optimizations, |
3208 | possibly causing performance to suffer, but ensuring that in cases | possibly causing performance to suffer, but ensuring that in cases |
3209 | where the result is "no match", the callouts do occur, and that items | where the result is "no match", the callouts do occur, and that items |
3210 | such as (*COMMIT) and (*MARK) are considered at every possible starting | such as (*COMMIT) and (*MARK) are considered at every possible starting |
3211 | position in the subject string. If PCRE_NO_START_OPTIMIZE is set at | position in the subject string. If PCRE_NO_START_OPTIMIZE is set at |
3212 | compile time, it cannot be unset at matching time. The use of | compile time, it cannot be unset at matching time. The use of |
3213 | PCRE_NO_START_OPTIMIZE disables JIT execution; when it is set, matching | PCRE_NO_START_OPTIMIZE disables JIT execution; when it is set, matching |
3214 | is always done using interpretively. | is always done using interpretively. |
3215 | ||
3216 | Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching | Setting PCRE_NO_START_OPTIMIZE can change the outcome of a matching |
3217 | operation. Consider the pattern | operation. Consider the pattern |
3218 | ||
3219 | (*COMMIT)ABC | (*COMMIT)ABC |
3220 | ||
3221 | When this is compiled, PCRE records the fact that a match must start | When this is compiled, PCRE records the fact that a match must start |
3222 | with the character "A". Suppose the subject string is "DEFABC". The | with the character "A". Suppose the subject string is "DEFABC". The |
3223 | start-up optimization scans along the subject, finds "A" and runs the | start-up optimization scans along the subject, finds "A" and runs the |
3224 | first match attempt from there. The (*COMMIT) item means that the pat- | first match attempt from there. The (*COMMIT) item means that the pat- |
3225 | tern must match the current starting position, which in this case, it | tern must match the current starting position, which in this case, it |
3226 | does. However, if the same match is run with PCRE_NO_START_OPTIMIZE | does. However, if the same match is run with PCRE_NO_START_OPTIMIZE |
3227 | set, the initial scan along the subject string does not happen. The | set, the initial scan along the subject string does not happen. The |
3228 | first match attempt is run starting from "D" and when this fails, | first match attempt is run starting from "D" and when this fails, |
3229 | (*COMMIT) prevents any further matches being tried, so the overall | (*COMMIT) prevents any further matches being tried, so the overall |
3230 | result is "no match". If the pattern is studied, more start-up opti- | result is "no match". If the pattern is studied, more start-up opti- |
3231 | mizations may be used. For example, a minimum length for the subject | mizations may be used. For example, a minimum length for the subject |
3232 | may be recorded. Consider the pattern | may be recorded. Consider the pattern |
3233 | ||
3234 | (*MARK:A)(X|Y) | (*MARK:A)(X|Y) |
3235 | ||
3236 | The minimum length for a match is one character. If the subject is | The minimum length for a match is one character. If the subject is |
3237 | "ABC", there will be attempts to match "ABC", "BC", "C", and then | "ABC", there will be attempts to match "ABC", "BC", "C", and then |
3238 | finally an empty string. If the pattern is studied, the final attempt | finally an empty string. If the pattern is studied, the final attempt |
3239 | does not take place, because PCRE knows that the subject is too short, | does not take place, because PCRE knows that the subject is too short, |
3240 | and so the (*MARK) is never encountered. In this case, studying the | and so the (*MARK) is never encountered. In this case, studying the |
3241 | pattern does not affect the overall match result, which is still "no | pattern does not affect the overall match result, which is still "no |
3242 | match", but it does affect the auxiliary information that is returned. | match", but it does affect the auxiliary information that is returned. |
3243 | ||
3244 | PCRE_NO_UTF8_CHECK | PCRE_NO_UTF8_CHECK |
3245 | ||
3246 | When PCRE_UTF8 is set at compile time, the validity of the subject as a | When PCRE_UTF8 is set at compile time, the validity of the subject as a |
3247 | UTF-8 string is automatically checked when pcre_exec() is subsequently | UTF-8 string is automatically checked when pcre_exec() is subsequently |
3248 | called. The entire string is checked before any other processing takes | called. The entire string is checked before any other processing takes |
3249 | place. The value of startoffset is also checked to ensure that it | place. The value of startoffset is also checked to ensure that it |
3250 | points to the start of a UTF-8 character. There is a discussion about | points to the start of a UTF-8 character. There is a discussion about |
3251 | the validity of UTF-8 strings in the pcreunicode page. If an invalid | the validity of UTF-8 strings in the pcreunicode page. If an invalid |
3252 | sequence of bytes is found, pcre_exec() returns the error | sequence of bytes is found, pcre_exec() returns the error |
3253 | PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a | PCRE_ERROR_BADUTF8 or, if PCRE_PARTIAL_HARD is set and the problem is a |
3254 | truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In | truncated character at the end of the subject, PCRE_ERROR_SHORTUTF8. In |
3255 | both cases, information about the precise nature of the error may also | both cases, information about the precise nature of the error may also |
3256 | be returned (see the descriptions of these errors in the section enti- | be returned (see the descriptions of these errors in the section enti- |
3257 | tled Error return values from pcre_exec() below). If startoffset con- | tled Error return values from pcre_exec() below). If startoffset con- |
3258 | tains a value that does not point to the start of a UTF-8 character (or | tains a value that does not point to the start of a UTF-8 character (or |
3259 | to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is returned. | to the end of the subject), PCRE_ERROR_BADUTF8_OFFSET is returned. |
3260 | ||
3261 | If you already know that your subject is valid, and you want to skip | If you already know that your subject is valid, and you want to skip |
3262 | these checks for performance reasons, you can set the | these checks for performance reasons, you can set the |
3263 | PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to | PCRE_NO_UTF8_CHECK option when calling pcre_exec(). You might want to |
3264 | do this for the second and subsequent calls to pcre_exec() if you are | do this for the second and subsequent calls to pcre_exec() if you are |
3265 | making repeated calls to find all the matches in a single subject | making repeated calls to find all the matches in a single subject |
3266 | string. However, you should be sure that the value of startoffset | string. However, you should be sure that the value of startoffset |
3267 | points to the start of a character (or the end of the subject). When | points to the start of a character (or the end of the subject). When |
3268 | PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid string as a | PCRE_NO_UTF8_CHECK is set, the effect of passing an invalid string as a |
3269 | subject or an invalid value of startoffset is undefined. Your program | subject or an invalid value of startoffset is undefined. Your program |
3270 | may crash. | may crash. |
3271 | ||
3272 | PCRE_PARTIAL_HARD | PCRE_PARTIAL_HARD |
3273 | PCRE_PARTIAL_SOFT | PCRE_PARTIAL_SOFT |
3274 | ||
3275 | These options turn on the partial matching feature. For backwards com- | These options turn on the partial matching feature. For backwards com- |
3276 | patibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial | patibility, PCRE_PARTIAL is a synonym for PCRE_PARTIAL_SOFT. A partial |
3277 | match occurs if the end of the subject string is reached successfully, | match occurs if the end of the subject string is reached successfully, |
3278 | but there are not enough subject characters to complete the match. If | but there are not enough subject characters to complete the match. If |
3279 | this happens when PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set, | this happens when PCRE_PARTIAL_SOFT (but not PCRE_PARTIAL_HARD) is set, |
3280 | matching continues by testing any remaining alternatives. Only if no | matching continues by testing any remaining alternatives. Only if no |
3281 | complete match can be found is PCRE_ERROR_PARTIAL returned instead of | complete match can be found is PCRE_ERROR_PARTIAL returned instead of |
3282 | PCRE_ERROR_NOMATCH. In other words, PCRE_PARTIAL_SOFT says that the | PCRE_ERROR_NOMATCH. In other words, PCRE_PARTIAL_SOFT says that the |
3283 | caller is prepared to handle a partial match, but only if no complete | caller is prepared to handle a partial match, but only if no complete |
3284 | match can be found. | match can be found. |
3285 | ||
3286 | If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this | If PCRE_PARTIAL_HARD is set, it overrides PCRE_PARTIAL_SOFT. In this |
3287 | case, if a partial match is found, pcre_exec() immediately returns | case, if a partial match is found, pcre_exec() immediately returns |
3288 | PCRE_ERROR_PARTIAL, without considering any other alternatives. In | PCRE_ERROR_PARTIAL, without considering any other alternatives. In |
3289 | other words, when PCRE_PARTIAL_HARD is set, a partial match is consid- | other words, when PCRE_PARTIAL_HARD is set, a partial match is consid- |
3290 | ered to be more important that an alternative complete match. | ered to be more important that an alternative complete match. |
3291 | ||
3292 | In both cases, the portion of the string that was inspected when the | In both cases, the portion of the string that was inspected when the |
3293 | partial match was found is set as the first matching string. There is a | partial match was found is set as the first matching string. There is a |
3294 | more detailed discussion of partial and multi-segment matching, with | more detailed discussion of partial and multi-segment matching, with |
3295 | examples, in the pcrepartial documentation. | examples, in the pcrepartial documentation. |
3296 | ||
3297 | The string to be matched by pcre_exec() | The string to be matched by pcre_exec() |
3298 | ||
3299 | The subject string is passed to pcre_exec() as a pointer in subject, a | The subject string is passed to pcre_exec() as a pointer in subject, a |
3300 | length in bytes in length, and a starting byte offset in startoffset. | length in bytes in length, and a starting byte offset in startoffset. |
3301 | If this is negative or greater than the length of the subject, | If this is negative or greater than the length of the subject, |
3302 | pcre_exec() returns PCRE_ERROR_BADOFFSET. When the starting offset is | pcre_exec() returns PCRE_ERROR_BADOFFSET. When the starting offset is |
3303 | zero, the search for a match starts at the beginning of the subject, | zero, the search for a match starts at the beginning of the subject, |
3304 | and this is by far the most common case. In UTF-8 mode, the byte offset | and this is by far the most common case. In UTF-8 mode, the byte offset |
3305 | must point to the start of a UTF-8 character (or the end of the sub- | must point to the start of a UTF-8 character (or the end of the sub- |
3306 | ject). Unlike the pattern string, the subject may contain binary zero | ject). Unlike the pattern string, the subject may contain binary zero |
3307 | bytes. | bytes. |
3308 | ||
3309 | A non-zero starting offset is useful when searching for another match | A non-zero starting offset is useful when searching for another match |
3310 | in the same subject by calling pcre_exec() again after a previous suc- | in the same subject by calling pcre_exec() again after a previous suc- |
3311 | cess. Setting startoffset differs from just passing over a shortened | cess. Setting startoffset differs from just passing over a shortened |
3312 | string and setting PCRE_NOTBOL in the case of a pattern that begins | string and setting PCRE_NOTBOL in the case of a pattern that begins |
3313 | with any kind of lookbehind. For example, consider the pattern | with any kind of lookbehind. For example, consider the pattern |
3314 | ||
3315 | \Biss\B | \Biss\B |
3316 | ||
3317 | which finds occurrences of "iss" in the middle of words. (\B matches | which finds occurrences of "iss" in the middle of words. (\B matches |
3318 | only if the current position in the subject is not a word boundary.) | only if the current position in the subject is not a word boundary.) |
3319 | When applied to the string "Mississipi" the first call to pcre_exec() | When applied to the string "Mississipi" the first call to pcre_exec() |
3320 | finds the first occurrence. If pcre_exec() is called again with just | finds the first occurrence. If pcre_exec() is called again with just |
3321 | the remainder of the subject, namely "issipi", it does not match, | the remainder of the subject, namely "issipi", it does not match, |
3322 | because \B is always false at the start of the subject, which is deemed | because \B is always false at the start of the subject, which is deemed |
3323 | to be a word boundary. However, if pcre_exec() is passed the entire | to be a word boundary. However, if pcre_exec() is passed the entire |
3324 | string again, but with startoffset set to 4, it finds the second occur- | string again, but with startoffset set to 4, it finds the second occur- |
3325 | rence of "iss" because it is able to look behind the starting point to | rence of "iss" because it is able to look behind the starting point to |
3326 | discover that it is preceded by a letter. | discover that it is preceded by a letter. |
3327 | ||
3328 | Finding all the matches in a subject is tricky when the pattern can | Finding all the matches in a subject is tricky when the pattern can |
3329 | match an empty string. It is possible to emulate Perl's /g behaviour by | match an empty string. It is possible to emulate Perl's /g behaviour by |
3330 | first trying the match again at the same offset, with the | first trying the match again at the same offset, with the |
3331 | PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED options, and then if that | PCRE_NOTEMPTY_ATSTART and PCRE_ANCHORED options, and then if that |
3332 | fails, advancing the starting offset and trying an ordinary match | fails, advancing the starting offset and trying an ordinary match |
3333 | again. There is some code that demonstrates how to do this in the pcre- | again. There is some code that demonstrates how to do this in the pcre- |
3334 | demo sample program. In the most general case, you have to check to see | demo sample program. In the most general case, you have to check to see |
3335 | if the newline convention recognizes CRLF as a newline, and if so, and | if the newline convention recognizes CRLF as a newline, and if so, and |
3336 | the current character is CR followed by LF, advance the starting offset | the current character is CR followed by LF, advance the starting offset |
3337 | by two characters instead of one. | by two characters instead of one. |
3338 | ||
3339 | If a non-zero starting offset is passed when the pattern is anchored, | If a non-zero starting offset is passed when the pattern is anchored, |
3340 | one attempt to match at the given offset is made. This can only succeed | one attempt to match at the given offset is made. This can only succeed |
3341 | if the pattern does not require the match to be at the start of the | if the pattern does not require the match to be at the start of the |
3342 | subject. | subject. |
3343 | ||
3344 | How pcre_exec() returns captured substrings | How pcre_exec() returns captured substrings |
3345 | ||
3346 | In general, a pattern matches a certain portion of the subject, and in | In general, a pattern matches a certain portion of the subject, and in |
3347 | addition, further substrings from the subject may be picked out by | addition, further substrings from the subject may be picked out by |
3348 | parts of the pattern. Following the usage in Jeffrey Friedl's book, | parts of the pattern. Following the usage in Jeffrey Friedl's book, |
3349 | this is called "capturing" in what follows, and the phrase "capturing | this is called "capturing" in what follows, and the phrase "capturing |
3350 | subpattern" is used for a fragment of a pattern that picks out a sub- | subpattern" is used for a fragment of a pattern that picks out a sub- |
3351 | string. PCRE supports several other kinds of parenthesized subpattern | string. PCRE supports several other kinds of parenthesized subpattern |
3352 | that do not cause substrings to be captured. | that do not cause substrings to be captured. |
3353 | ||
3354 | Captured substrings are returned to the caller via a vector of integers | Captured substrings are returned to the caller via a vector of integers |
3355 | whose address is passed in ovector. The number of elements in the vec- | whose address is passed in ovector. The number of elements in the vec- |
3356 | tor is passed in ovecsize, which must be a non-negative number. Note: | tor is passed in ovecsize, which must be a non-negative number. Note: |
3357 | this argument is NOT the size of ovector in bytes. | this argument is NOT the size of ovector in bytes. |
3358 | ||
3359 | The first two-thirds of the vector is used to pass back captured sub- | The first two-thirds of the vector is used to pass back captured sub- |
3360 | strings, each substring using a pair of integers. The remaining third | strings, each substring using a pair of integers. The remaining third |
3361 | of the vector is used as workspace by pcre_exec() while matching cap- | of the vector is used as workspace by pcre_exec() while matching cap- |
3362 | turing subpatterns, and is not available for passing back information. | turing subpatterns, and is not available for passing back information. |
3363 | The number passed in ovecsize should always be a multiple of three. If | The number passed in ovecsize should always be a multiple of three. If |
3364 | it is not, it is rounded down. | it is not, it is rounded down. |
3365 | ||
3366 | When a match is successful, information about captured substrings is | When a match is successful, information about captured substrings is |
3367 | returned in pairs of integers, starting at the beginning of ovector, | returned in pairs of integers, starting at the beginning of ovector, |
3368 | and continuing up to two-thirds of its length at the most. The first | and continuing up to two-thirds of its length at the most. The first |
3369 | element of each pair is set to the byte offset of the first character | element of each pair is set to the byte offset of the first character |
3370 | in a substring, and the second is set to the byte offset of the first | in a substring, and the second is set to the byte offset of the first |
3371 | character after the end of a substring. Note: these values are always | character after the end of a substring. Note: these values are always |
3372 | byte offsets, even in UTF-8 mode. They are not character counts. | byte offsets, even in UTF-8 mode. They are not character counts. |
3373 | ||
3374 | The first pair of integers, ovector[0] and ovector[1], identify the | The first pair of integers, ovector[0] and ovector[1], identify the |
3375 | portion of the subject string matched by the entire pattern. The next | portion of the subject string matched by the entire pattern. The next |
3376 | pair is used for the first capturing subpattern, and so on. The value | pair is used for the first capturing subpattern, and so on. The value |
3377 | returned by pcre_exec() is one more than the highest numbered pair that | returned by pcre_exec() is one more than the highest numbered pair that |
3378 | has been set. For example, if two substrings have been captured, the | has been set. For example, if two substrings have been captured, the |
3379 | returned value is 3. If there are no capturing subpatterns, the return | returned value is 3. If there are no capturing subpatterns, the return |
3380 | value from a successful match is 1, indicating that just the first pair | value from a successful match is 1, indicating that just the first pair |
3381 | of offsets has been set. | of offsets has been set. |
3382 | ||
3383 | If a capturing subpattern is matched repeatedly, it is the last portion | If a capturing subpattern is matched repeatedly, it is the last portion |
3384 | of the string that it matched that is returned. | of the string that it matched that is returned. |
3385 | ||
3386 | If the vector is too small to hold all the captured substring offsets, | If the vector is too small to hold all the captured substring offsets, |
3387 | it is used as far as possible (up to two-thirds of its length), and the | it is used as far as possible (up to two-thirds of its length), and the |
3388 | function returns a value of zero. If neither the actual string matched | function returns a value of zero. If neither the actual string matched |
3389 | nor any captured substrings are of interest, pcre_exec() may be called | nor any captured substrings are of interest, pcre_exec() may be called |
3390 | with ovector passed as NULL and ovecsize as zero. However, if the pat- | with ovector passed as NULL and ovecsize as zero. However, if the pat- |
3391 | tern contains back references and the ovector is not big enough to | tern contains back references and the ovector is not big enough to |
3392 | remember the related substrings, PCRE has to get additional memory for | remember the related substrings, PCRE has to get additional memory for |
3393 | use during matching. Thus it is usually advisable to supply an ovector | use during matching. Thus it is usually advisable to supply an ovector |
3394 | of reasonable size. | of reasonable size. |
3395 | ||
3396 | There are some cases where zero is returned (indicating vector over- | There are some cases where zero is returned (indicating vector over- |
3397 | flow) when in fact the vector is exactly the right size for the final | flow) when in fact the vector is exactly the right size for the final |
3398 | match. For example, consider the pattern | match. For example, consider the pattern |
3399 | ||
3400 | (a)(?:(b)c|bd) | (a)(?:(b)c|bd) |
3401 | ||
3402 | If a vector of 6 elements (allowing for only 1 captured substring) is | If a vector of 6 elements (allowing for only 1 captured substring) is |
3403 | given with subject string "abd", pcre_exec() will try to set the second | given with subject string "abd", pcre_exec() will try to set the second |
3404 | captured string, thereby recording a vector overflow, before failing to | captured string, thereby recording a vector overflow, before failing to |
3405 | match "c" and backing up to try the second alternative. The zero | match "c" and backing up to try the second alternative. The zero |
3406 | return, however, does correctly indicate that the maximum number of | return, however, does correctly indicate that the maximum number of |
3407 | slots (namely 2) have been filled. In similar cases where there is tem- | slots (namely 2) have been filled. In similar cases where there is tem- |
3408 | porary overflow, but the final number of used slots is actually less | porary overflow, but the final number of used slots is actually less |
3409 | than the maximum, a non-zero value is returned. | than the maximum, a non-zero value is returned. |
3410 | ||
3411 | The pcre_fullinfo() function can be used to find out how many capturing | The pcre_fullinfo() function can be used to find out how many capturing |
3412 | subpatterns there are in a compiled pattern. The smallest size for | subpatterns there are in a compiled pattern. The smallest size for |
3413 | ovector that will allow for n captured substrings, in addition to the | ovector that will allow for n captured substrings, in addition to the |
3414 | offsets of the substring matched by the whole pattern, is (n+1)*3. | offsets of the substring matched by the whole pattern, is (n+1)*3. |
3415 | ||
3416 | It is possible for capturing subpattern number n+1 to match some part | It is possible for capturing subpattern number n+1 to match some part |
3417 | of the subject when subpattern n has not been used at all. For example, | of the subject when subpattern n has not been used at all. For example, |
3418 | if the string "abc" is matched against the pattern (a|(z))(bc) the | if the string "abc" is matched against the pattern (a|(z))(bc) the |
3419 | return from the function is 4, and subpatterns 1 and 3 are matched, but | return from the function is 4, and subpatterns 1 and 3 are matched, but |
3420 | 2 is not. When this happens, both values in the offset pairs corre- | 2 is not. When this happens, both values in the offset pairs corre- |
3421 | sponding to unused subpatterns are set to -1. | sponding to unused subpatterns are set to -1. |
3422 | ||
3423 | Offset values that correspond to unused subpatterns at the end of the | Offset values that correspond to unused subpatterns at the end of the |
3424 | expression are also set to -1. For example, if the string "abc" is | expression are also set to -1. For example, if the string "abc" is |
3425 | matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not | matched against the pattern (abc)(x(yz)?)? subpatterns 2 and 3 are not |
3426 | matched. The return from the function is 2, because the highest used | matched. The return from the function is 2, because the highest used |
3427 | capturing subpattern number is 1, and the offsets for for the second | capturing subpattern number is 1, and the offsets for for the second |
3428 | and third capturing subpatterns (assuming the vector is large enough, | and third capturing subpatterns (assuming the vector is large enough, |
3429 | of course) are set to -1. | of course) are set to -1. |
3430 | ||
3431 | Note: Elements in the first two-thirds of ovector that do not corre- | Note: Elements in the first two-thirds of ovector that do not corre- |
3432 | spond to capturing parentheses in the pattern are never changed. That | spond to capturing parentheses in the pattern are never changed. That |
3433 | is, if a pattern contains n capturing parentheses, no more than ovec- | is, if a pattern contains n capturing parentheses, no more than ovec- |
3434 | tor[0] to ovector[2n+1] are set by pcre_exec(). The other elements (in | tor[0] to ovector[2n+1] are set by pcre_exec(). The other elements (in |
3435 | the first two-thirds) retain whatever values they previously had. | the first two-thirds) retain whatever values they previously had. |
3436 | ||
3437 | Some convenience functions are provided for extracting the captured | Some convenience functions are provided for extracting the captured |
3438 | substrings as separate strings. These are described below. | substrings as separate strings. These are described below. |
3439 | ||
3440 | Error return values from pcre_exec() | Error return values from pcre_exec() |
3441 | ||
3442 | If pcre_exec() fails, it returns a negative number. The following are | If pcre_exec() fails, it returns a negative number. The following are |
3443 | defined in the header file: | defined in the header file: |
3444 | ||
3445 | PCRE_ERROR_NOMATCH (-1) | PCRE_ERROR_NOMATCH (-1) |
# | Line 3447 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3448 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3448 | ||
3449 | PCRE_ERROR_NULL (-2) | PCRE_ERROR_NULL (-2) |
3450 | ||
3451 | Either code or subject was passed as NULL, or ovector was NULL and | Either code or subject was passed as NULL, or ovector was NULL and |
3452 | ovecsize was not zero. | ovecsize was not zero. |
3453 | ||
3454 | PCRE_ERROR_BADOPTION (-3) | PCRE_ERROR_BADOPTION (-3) |
# | Line 3456 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3457 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3457 | ||
3458 | PCRE_ERROR_BADMAGIC (-4) | PCRE_ERROR_BADMAGIC (-4) |
3459 | ||
3460 | PCRE stores a 4-byte "magic number" at the start of the compiled code, | PCRE stores a 4-byte "magic number" at the start of the compiled code, |
3461 | to catch the case when it is passed a junk pointer and to detect when a | to catch the case when it is passed a junk pointer and to detect when a |
3462 | pattern that was compiled in an environment of one endianness is run in | pattern that was compiled in an environment of one endianness is run in |
3463 | an environment with the other endianness. This is the error that PCRE | an environment with the other endianness. This is the error that PCRE |
3464 | gives when the magic number is not present. | gives when the magic number is not present. |
3465 | ||
3466 | PCRE_ERROR_UNKNOWN_OPCODE (-5) | PCRE_ERROR_UNKNOWN_OPCODE (-5) |
3467 | ||
3468 | While running the pattern match, an unknown item was encountered in the | While running the pattern match, an unknown item was encountered in the |
3469 | compiled pattern. This error could be caused by a bug in PCRE or by | compiled pattern. This error could be caused by a bug in PCRE or by |
3470 | overwriting of the compiled pattern. | overwriting of the compiled pattern. |
3471 | ||
3472 | PCRE_ERROR_NOMEMORY (-6) | PCRE_ERROR_NOMEMORY (-6) |
3473 | ||
3474 | If a pattern contains back references, but the ovector that is passed | If a pattern contains back references, but the ovector that is passed |
3475 | to pcre_exec() is not big enough to remember the referenced substrings, | to pcre_exec() is not big enough to remember the referenced substrings, |
3476 | PCRE gets a block of memory at the start of matching to use for this | PCRE gets a block of memory at the start of matching to use for this |
3477 | purpose. If the call via pcre_malloc() fails, this error is given. The | purpose. If the call via pcre_malloc() fails, this error is given. The |
3478 | memory is automatically freed at the end of matching. | memory is automatically freed at the end of matching. |
3479 | ||
3480 | This error is also given if pcre_stack_malloc() fails in pcre_exec(). | This error is also given if pcre_stack_malloc() fails in pcre_exec(). |
3481 | This can happen only when PCRE has been compiled with --disable-stack- | This can happen only when PCRE has been compiled with --disable-stack- |
3482 | for-recursion. | for-recursion. |
3483 | ||
3484 | PCRE_ERROR_NOSUBSTRING (-7) | PCRE_ERROR_NOSUBSTRING (-7) |
3485 | ||
3486 | This error is used by the pcre_copy_substring(), pcre_get_substring(), | This error is used by the pcre_copy_substring(), pcre_get_substring(), |
3487 | and pcre_get_substring_list() functions (see below). It is never | and pcre_get_substring_list() functions (see below). It is never |
3488 | returned by pcre_exec(). | returned by pcre_exec(). |
3489 | ||
3490 | PCRE_ERROR_MATCHLIMIT (-8) | PCRE_ERROR_MATCHLIMIT (-8) |
3491 | ||
3492 | The backtracking limit, as specified by the match_limit field in a | The backtracking limit, as specified by the match_limit field in a |
3493 | pcre_extra structure (or defaulted) was reached. See the description | pcre_extra structure (or defaulted) was reached. See the description |
3494 | above. | above. |
3495 | ||
3496 | PCRE_ERROR_CALLOUT (-9) | PCRE_ERROR_CALLOUT (-9) |
3497 | ||
3498 | This error is never generated by pcre_exec() itself. It is provided for | This error is never generated by pcre_exec() itself. It is provided for |
3499 | use by callout functions that want to yield a distinctive error code. | use by callout functions that want to yield a distinctive error code. |
3500 | See the pcrecallout documentation for details. | See the pcrecallout documentation for details. |
3501 | ||
3502 | PCRE_ERROR_BADUTF8 (-10) | PCRE_ERROR_BADUTF8 (-10) |
3503 | ||
3504 | A string that contains an invalid UTF-8 byte sequence was passed as a | A string that contains an invalid UTF-8 byte sequence was passed as a |
3505 | subject, and the PCRE_NO_UTF8_CHECK option was not set. If the size of | subject, and the PCRE_NO_UTF8_CHECK option was not set. If the size of |
3506 | the output vector (ovecsize) is at least 2, the byte offset to the | the output vector (ovecsize) is at least 2, the byte offset to the |
3507 | start of the the invalid UTF-8 character is placed in the first ele- | start of the the invalid UTF-8 character is placed in the first ele- |
3508 | ment, and a reason code is placed in the second element. The reason | ment, and a reason code is placed in the second element. The reason |
3509 | codes are listed in the following section. For backward compatibility, | codes are listed in the following section. For backward compatibility, |
3510 | if PCRE_PARTIAL_HARD is set and the problem is a truncated UTF-8 char- | if PCRE_PARTIAL_HARD is set and the problem is a truncated UTF-8 char- |
3511 | acter at the end of the subject (reason codes 1 to 5), | acter at the end of the subject (reason codes 1 to 5), |
3512 | PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8. | PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8. |
3513 | ||
3514 | PCRE_ERROR_BADUTF8_OFFSET (-11) | PCRE_ERROR_BADUTF8_OFFSET (-11) |
3515 | ||
3516 | The UTF-8 byte sequence that was passed as a subject was checked and | The UTF-8 byte sequence that was passed as a subject was checked and |
3517 | found to be valid (the PCRE_NO_UTF8_CHECK option was not set), but the | found to be valid (the PCRE_NO_UTF8_CHECK option was not set), but the |
3518 | value of startoffset did not point to the beginning of a UTF-8 charac- | value of startoffset did not point to the beginning of a UTF-8 charac- |
3519 | ter or the end of the subject. | ter or the end of the subject. |
3520 | ||
3521 | PCRE_ERROR_PARTIAL (-12) | PCRE_ERROR_PARTIAL (-12) |
3522 | ||
3523 | The subject string did not match, but it did match partially. See the | The subject string did not match, but it did match partially. See the |
3524 | pcrepartial documentation for details of partial matching. | pcrepartial documentation for details of partial matching. |
3525 | ||
3526 | PCRE_ERROR_BADPARTIAL (-13) | PCRE_ERROR_BADPARTIAL (-13) |
3527 | ||
3528 | This code is no longer in use. It was formerly returned when the | This code is no longer in use. It was formerly returned when the |
3529 | PCRE_PARTIAL option was used with a compiled pattern containing items | PCRE_PARTIAL option was used with a compiled pattern containing items |
3530 | that were not supported for partial matching. From release 8.00 | that were not supported for partial matching. From release 8.00 |
3531 | onwards, there are no restrictions on partial matching. | onwards, there are no restrictions on partial matching. |
3532 | ||
3533 | PCRE_ERROR_INTERNAL (-14) | PCRE_ERROR_INTERNAL (-14) |
3534 | ||
3535 | An unexpected internal error has occurred. This error could be caused | An unexpected internal error has occurred. This error could be caused |
3536 | by a bug in PCRE or by overwriting of the compiled pattern. | by a bug in PCRE or by overwriting of the compiled pattern. |
3537 | ||
3538 | PCRE_ERROR_BADCOUNT (-15) | PCRE_ERROR_BADCOUNT (-15) |
# | Line 3541 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3542 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3542 | PCRE_ERROR_RECURSIONLIMIT (-21) | PCRE_ERROR_RECURSIONLIMIT (-21) |
3543 | ||
3544 | The internal recursion limit, as specified by the match_limit_recursion | The internal recursion limit, as specified by the match_limit_recursion |
3545 | field in a pcre_extra structure (or defaulted) was reached. See the | field in a pcre_extra structure (or defaulted) was reached. See the |
3546 | description above. | description above. |
3547 | ||
3548 | PCRE_ERROR_BADNEWLINE (-23) | PCRE_ERROR_BADNEWLINE (-23) |
# | Line 3555 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3556 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3556 | ||
3557 | PCRE_ERROR_SHORTUTF8 (-25) | PCRE_ERROR_SHORTUTF8 (-25) |
3558 | ||
3559 | This error is returned instead of PCRE_ERROR_BADUTF8 when the subject | This error is returned instead of PCRE_ERROR_BADUTF8 when the subject |
3560 | string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD | string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD |
3561 | option is set. Information about the failure is returned as for | option is set. Information about the failure is returned as for |
3562 | PCRE_ERROR_BADUTF8. It is in fact sufficient to detect this case, but | PCRE_ERROR_BADUTF8. It is in fact sufficient to detect this case, but |
3563 | this special error code for PCRE_PARTIAL_HARD precedes the implementa- | this special error code for PCRE_PARTIAL_HARD precedes the implementa- |
3564 | tion of returned information; it is retained for backwards compatibil- | tion of returned information; it is retained for backwards compatibil- |
3565 | ity. | ity. |
3566 | ||
3567 | PCRE_ERROR_RECURSELOOP (-26) | PCRE_ERROR_RECURSELOOP (-26) |
3568 | ||
3569 | This error is returned when pcre_exec() detects a recursion loop within | This error is returned when pcre_exec() detects a recursion loop within |
3570 | the pattern. Specifically, it means that either the whole pattern or a | the pattern. Specifically, it means that either the whole pattern or a |
3571 | subpattern has been called recursively for the second time at the same | subpattern has been called recursively for the second time at the same |
3572 | position in the subject string. Some simple patterns that might do this | position in the subject string. Some simple patterns that might do this |
3573 | are detected and faulted at compile time, but more complicated cases, | are detected and faulted at compile time, but more complicated cases, |
3574 | in particular mutual recursions between two different subpatterns, can- | in particular mutual recursions between two different subpatterns, can- |
3575 | not be detected until run time. | not be detected until run time. |
3576 | ||
3577 | PCRE_ERROR_JIT_STACKLIMIT (-27) | PCRE_ERROR_JIT_STACKLIMIT (-27) |
3578 | ||
3579 | This error is returned when a pattern that was successfully studied | This error is returned when a pattern that was successfully studied |
3580 | using a JIT compile option is being matched, but the memory available | using a JIT compile option is being matched, but the memory available |
3581 | for the just-in-time processing stack is not large enough. See the | for the just-in-time processing stack is not large enough. See the |
3582 | pcrejit documentation for more details. | pcrejit documentation for more details. |
3583 | ||
3584 | PCRE_ERROR_BADMODE (-28) | PCRE_ERROR_BADMODE (-28) |
# | Line 3587 MATCHING A PATTERN: THE TRADITIONAL FUNC | Line 3588 MATCHING A PATTERN: THE TRADITIONAL FUNC |
3588 | ||
3589 | PCRE_ERROR_BADENDIANNESS (-29) | PCRE_ERROR_BADENDIANNESS (-29) |
3590 | ||
3591 | This error is given if a pattern that was compiled and saved is | This error is given if a pattern that was compiled and saved is |
3592 | reloaded on a host with different endianness. The utility function | reloaded on a host with different endianness. The utility function |
3593 | pcre_pattern_to_host_byte_order() can be used to convert such a pattern | pcre_pattern_to_host_byte_order() can be used to convert such a pattern |
3594 | so that it runs on the new host. | so that it runs on the new host. |
3595 | ||
3596 | PCRE_ERROR_JIT_BADOPTION | PCRE_ERROR_JIT_BADOPTION |
3597 | ||
3598 | This error is returned when a pattern that was successfully studied | This error is returned when a pattern that was successfully studied |
3599 | using a JIT compile option is being matched, but the matching mode | using a JIT compile option is being matched, but the matching mode |
3600 | (partial or complete match) does not correspond to any JIT compilation | (partial or complete match) does not correspond to any JIT compilation |
3601 | mode. When the JIT fast path function is used, this error may be also | mode. When the JIT fast path function is used, this error may be also |
3602 | given for invalid options. See the pcrejit documentation for more | given for invalid options. See the pcrejit documentation for more |
3603 | details. | details. |
3604 | ||
3605 | PCRE_ERROR_BADLENGTH (-32) | PCRE_ERROR_BADLENGTH (-32) |
3606 | ||
3607 | This error is given if pcre_exec() is called with a negative value for | This error is given if pcre_exec() is called with a negative value for |
3608 | the length argument. | the length argument. |
3609 | ||
3610 | Error numbers -16 to -20, -22, and 30 are not used by pcre_exec(). | Error numbers -16 to -20, -22, and 30 are not used by pcre_exec(). |
3611 | ||
3612 | Reason codes for invalid UTF-8 strings | Reason codes for invalid UTF-8 strings |
3613 | ||
3614 | This section applies only to the 8-bit library. The corresponding | This section applies only to the 8-bit library. The corresponding |
3615 | information for the 16-bit library is given in the pcre16 page. The | information for the 16-bit and 32-bit libraries is given in the pcre16 |
3616 | corresponding information for the 32-bit library is given in the pcre32 | and pcre32 pages. |
page. | ||
3617 | ||
3618 | When pcre_exec() returns either PCRE_ERROR_BADUTF8 or PCRE_ERROR_SHORT- | When pcre_exec() returns either PCRE_ERROR_BADUTF8 or PCRE_ERROR_SHORT- |
3619 | UTF8, and the size of the output vector (ovecsize) is at least 2, the | UTF8, and the size of the output vector (ovecsize) is at least 2, the |
# | Line 4101 AUTHOR | Line 4101 AUTHOR |
4101 | ||
4102 | REVISION | REVISION |
4103 | ||
4104 | Last updated: 31 October 2012 | Last updated: 08 November 2012 |
4105 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
4106 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
4107 | ||
4108 | ||
4109 | PCRECALLOUT(3) PCRECALLOUT(3) | PCRECALLOUT(3) PCRECALLOUT(3) |
4110 | ||
4111 | ||
# | Line 4313 REVISION | Line 4313 REVISION |
4313 | Last updated: 24 June 2012 | Last updated: 24 June 2012 |
4314 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
4315 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
4316 | ||
4317 | ||
4318 | PCRECOMPAT(3) PCRECOMPAT(3) | PCRECOMPAT(3) PCRECOMPAT(3) |
4319 | ||
4320 | ||
# | Line 4491 REVISION | Line 4491 REVISION |
4491 | Last updated: 25 August 2012 | Last updated: 25 August 2012 |
4492 | Copyright (c) 1997-2012 University of Cambridge. | Copyright (c) 1997-2012 University of Cambridge. |
4493 | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ |
4494 | ||
4495 | ||
4496 | PCREPATTERN(3) PCREPATTERN(3) | PCREPATTERN(3) PCREPATTERN(3) |
4497 | ||
4498 | ||
# | Line 4520 PCRE REGULAR EXPRESSION DETAILS | Line 4520 PCRE REGULAR EXPRESSION DETAILS |
4520 | The original operation of PCRE was on strings of one-byte characters. | The original operation of PCRE was on strings of one-byte characters. |
4521 | However, there is now also support for UTF-8 strings in the original | However, there is now also support for UTF-8 strings in the original |
4522 | library, an extra library that supports 16-bit and UTF-16 character | library, an extra library that supports 16-bit and UTF-16 character |
4523 | strings, and an extra library that supports 32-bit and UTF-32 character | strings, and a third library that supports 32-bit and UTF-32 character |
4524 | strings. To use these features, PCRE must be built to include appropri- | strings. To use these features, PCRE must be built to include appropri- |
4525 | ate support. When using UTF strings you must either call the compiling | ate support. When using UTF strings you must either call the compiling |
4526 | function with the PCRE_UTF8, PCRE_UTF16 or PCRE_UTF32 option, or the | function with the PCRE_UTF8, PCRE_UTF16, or PCRE_UTF32 option, or the |
4527 | pattern must start with one of these special sequences: | pattern must start with one of these special sequences: |
4528 | ||
4529 | (*UTF8) | (*UTF8) |
4530 | (*UTF16) | (*UTF16) |
4531 | (*UTF32) | (*UTF32) |
4532 | (*UTF) | |
4533 | ||
4534 | Starting a pattern with such a sequence is equivalent to setting the | (*UTF) is a generic sequence that can be used with any of the |
4535 | relevant option. This feature is not Perl-compatible. How setting a UTF | libraries. Starting a pattern with such a sequence is equivalent to |
4536 | mode affects pattern matching is mentioned in several places below. | setting the relevant option. This feature is not Perl-compatible. How |
4537 | There is also a summary of features in the pcreunicode page. | setting a UTF mode affects pattern matching is mentioned in several |
4538 | places below. There is also a summary of features in the pcreunicode | |
4539 | page. | |
4540 | ||
4541 | Another special sequence that may appear at the start of a pattern or | Another special sequence that may appear at the start of a pattern or |
4542 | in combination with (*UTF8) or (*UTF16) or (*UTF32) is: | in combination with (*UTF8), (*UTF16), (*UTF32) or (*UTF) is: |
4543 | ||
4544 | (*UCP) | (*UCP) |
4545 | ||
# | Line 4600 NEWLINE CONVENTIONS | Line 4603 NEWLINE CONVENTIONS |
4603 | and that they must be in upper case. If more than one of them is | and that they must be in upper case. If more than one of them is |
4604 | present, the last one is used. | present, the last one is used. |
4605 | ||
4606 | The newline convention affects the interpretation of the dot metachar- | The newline convention affects where the circumflex and dollar asser- |
4607 | acter when PCRE_DOTALL is not set, and also the behaviour of \N. How- | tions are true. It also affects the interpretation of the dot metachar- |
4608 | ever, it does not affect what the \R escape sequence matches. By | acter when PCRE_DOTALL is not set, and the behaviour of \N. However, it |
4609 | default, this is any Unicode newline sequence, for Perl compatibility. | does not affect what the \R escape sequence matches. By default, this |
4610 | However, this can be changed; see the description of \R in the section | is any Unicode newline sequence, for Perl compatibility. However, this |
4611 | entitled "Newline sequences" below. A change of \R setting can be com- | can be changed; see the description of \R in the section entitled "New- |
4612 | bined with a change of newline convention. | line sequences" below. A change of \R setting can be combined with a |
4613 | change of newline convention. | |
4614 | ||
4615 | ||
4616 | CHARACTERS AND METACHARACTERS | CHARACTERS AND METACHARACTERS |
4617 | ||
4618 | A regular expression is a pattern that is matched against a subject | A regular expression is a pattern that is matched against a subject |
4619 | string from left to right. Most characters stand for themselves in a | string from left to right. Most characters stand for themselves in a |
4620 | pattern, and match the corresponding characters in the subject. As a | pattern, and match the corresponding characters in the subject. As a |
4621 | trivial example, the pattern | trivial example, the pattern |
4622 | ||
4623 | The quick brown fox | The quick brown fox |
4624 | ||
4625 | matches a portion of a subject string that is identical to itself. When | matches a portion of a subject string that is identical to itself. When |
4626 | caseless matching is specified (the PCRE_CASELESS option), letters are | caseless matching is specified (the PCRE_CASELESS option), letters are |
4627 | matched independently of case. In a UTF mode, PCRE always understands | matched independently of case. In a UTF mode, PCRE always understands |
4628 | the concept of case for characters whose values are less than 128, so | the concept of case for characters whose values are less than 128, so |
4629 | caseless matching is always possible. For characters with higher val- | caseless matching is always possible. For characters with higher val- |
4630 | ues, the concept of case is supported if PCRE is compiled with Unicode | ues, the concept of case is supported if PCRE is compiled with Unicode |
4631 | property support, but not otherwise. If you want to use caseless | property support, but not otherwise. If you want to use caseless |
4632 | matching for characters 128 and above, you must ensure that PCRE is | matching for characters 128 and above, you must ensure that PCRE is |
4633 | compiled with Unicode property support as well as with UTF support. | compiled with Unicode property support as well as with UTF support. |
4634 | ||
4635 | The power of regular expressions comes from the ability to include | The power of regular expressions comes from the ability to include |
4636 | alternatives and repetitions in the pattern. These are encoded in the | alternatives and repetitions in the pattern. These are encoded in the |
4637 | pattern by the use of metacharacters, which do not stand for themselves | pattern by the use of metacharacters, which do not stand for themselves |
4638 | but instead are interpreted in some special way. | but instead are interpreted in some special way. |
4639 | ||
4640 | There are two different sets of metacharacters: those that are recog- | There are two different sets of metacharacters: those that are recog- |
4641 | nized anywhere in the pattern except within square brackets, and those | nized anywhere in the pattern except within square brackets, and those |
4642 | that are recognized within square brackets. Outside square brackets, | that are recognized within square brackets. Outside square brackets, |
4643 | the metacharacters are as follows: | the metacharacters are as follows: |
4644 | ||
4645 | \ general escape character with several uses | \ general escape character with several uses |
# | Line 4654 CHARACTERS AND METACHARACTERS | Line 4658 CHARACTERS AND METACHARACTERS |
4658 | also "possessive quantifier" | also "possessive quantifier" |
4659 | { start min/max quantifier | { start min/max quantifier |
4660 | ||
4661 | Part of a pattern that is in square brackets is called a "character | Part of a pattern that is in square brackets is called a "character |
4662 | class". In a character class the only metacharacters are: | class". In a character class the only metacharacters are: |
4663 | ||
4664 | \ general escape character | \ general escape character |
# | Line 4671 BACKSLASH | Line 4675 BACKSLASH |
4675 | ||
4676 | The backslash character has several uses. Firstly, if it is followed by | The backslash character has several uses. Firstly, if it is followed by |
4677 | a character that is not a number or a letter, it takes away any special | a character that is not a number or a letter, it takes away any special |
4678 | meaning that character may have. This use of backslash as an escape | meaning that character may have. This use of backslash as an escape |
4679 | character applies both inside and outside character classes. | character applies both inside and outside character classes. |
4680 | ||
4681 | For example, if you want to match a * character, you write \* in the | For example, if you want to match a * character, you write \* in the |
4682 | pattern. This escaping action applies whether or not the following | pattern. This escaping action applies whether or not the following |
4683 | character would otherwise be interpreted as a metacharacter, so it is | character would otherwise be interpreted as a metacharacter, so it is |
4684 | always safe to precede a non-alphanumeric with backslash to specify | always safe to precede a non-alphanumeric with backslash to specify |
4685 | that it stands for itself. In particular, if you want to match a back- | that it stands for itself. In particular, if you want to match a back- |
4686 | slash, you write \\. | slash, you write \\. |
4687 | ||
4688 | In a UTF mode, only ASCII numbers and letters have any special meaning | In a UTF mode, only ASCII numbers and letters have any special meaning |
4689 | after a backslash. All other characters (in particular, those whose | after a backslash. All other characters (in particular, those whose |
4690 | codepoints are greater than 127) are treated as literals. | codepoints are greater than 127) are treated as literals. |
4691 | ||
4692 | If a pattern is compiled with the PCRE_EXTENDED option, white space in | If a pattern is compiled with the PCRE_EXTENDED option, white space in |
4693 | the pattern (other than in a character class) and characters between a | the pattern (other than in a character class) and characters between a |
4694 | # outside a character class and the next newline are ignored. An escap- | # outside a character class and the next newline are ignored. An escap- |
4695 | ing backslash can be used to include a white space or # character as | ing backslash can be used to include a white space or # character as |
4696 | part of the pattern. | part of the pattern. |
4697 | ||
4698 | If you want to remove the special meaning from a sequence of charac- | If you want to remove the special meaning from a sequence of charac- |
4699 | ters, you can do so by putting them between \Q and \E. This is differ- | ters, you can do so by putting them between \Q and \E. This is differ- |
4700 | ent from Perl in that $ and @ are handled as literals in \Q...\E | ent from Perl in that $ and @ are handled as literals in \Q...\E |
4701 | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- | sequences in PCRE, whereas in Perl, $ and @ cause variable interpola- |
4702 | tion. Note the following examples: | tion. Note the following examples: |
4703 | ||
4704 | Pattern PCRE matches Perl matches | Pattern PCRE matches Perl matches |
# | Line 4704 BACKSLASH | Line 4708 BACKSLASH |
4708 | \Qabc\$xyz\E abc\$xyz abc\$xyz | \Qabc\$xyz\E abc\$xyz abc\$xyz |
4709 | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz | \Qabc\E\$\Qxyz\E abc$xyz abc$xyz |
4710 | ||
4711 | The \Q...\E sequence is recognized both inside and outside character | The \Q...\E sequence is recognized both inside and outside character |
4712 | classes. An isolated \E that is not preceded by \Q is ignored. If \Q | classes. An isolated \E that is not preceded by \Q is ignored. If \Q |
4713 | is not followed by \E later in the pattern, the literal interpretation | is not followed by \E later in the pattern, the literal interpretation |
4714 | continues to the end of the pattern (that is, \E is assumed at the | continues to the end of the pattern (that is, \E is assumed at the |
4715 | end). If the isolated \Q is inside a character class, this causes an | end). If the isolated \Q is inside a character class, this causes an |
4716 | error, because the character class is not terminated. | error, because the character class is not terminated. |
4717 | ||
4718 | Non-printing characters | Non-printing characters |
4719 | ||
4720 | A second use of backslash provides a way of encoding non-printing char- | A second use of backslash provides a way of encoding non-printing char- |
4721 | acters in patterns in a visible manner. There is no restriction on the | acters in patterns in a visible manner. There is no restriction on the |
4722 | appearance of non-printing characters, apart from the binary zero that | appearance of non-printing characters, apart from the binary zero that |
4723 | terminates a pattern, but when a pattern is being prepared by text | terminates a pattern, but when a pattern is being prepared by text |
4724 | editing, it is often easier to use one of the following escape | editing, it is often easier to use one of the following escape |
4725 | sequences than the binary character it represents: | sequences than the binary character it represents: |
4726 | ||
4727 | \a alarm, that is, the BEL character (hex 07) | \a alarm, that is, the BEL character (hex 07) |
# | Line 4732 BACKSLASH | Line 4736 BACKSLASH |
4736 | \x{hhh..} character with hex code hhh.. (non-JavaScript mode) | \x{hhh..} character with hex code hhh.. (non-JavaScript mode) |
4737 | \uhhhh character with hex code hhhh (JavaScript mode only) | \uhhhh character with hex code hhhh (JavaScript mode only) |
4738 | ||
4739 | The precise effect of \cx on ASCII characters is as follows: if x is a | The precise effect of \cx on ASCII characters is as follows: if x is a |
4740 | lower case letter, it is converted to upper case. Then bit 6 of the | lower case letter, it is converted to upper case. Then bit 6 of the |
4741 | character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A | character (hex 40) is inverted. Thus \cA to \cZ become hex 01 to hex 1A |
4742 | (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes | (A is 41, Z is 5A), but \c{ becomes hex 3B ({ is 7B), and \c; becomes |
4743 | hex 7B (; is 3B). If the data item (byte or 16-bit value) following \c | hex 7B (; is 3B). If the data item (byte or 16-bit value) following \c |
4744 | has a value greater than 127, a compile-time error occurs. This locks | has a value greater than 127, a compile-time error occurs. This locks |
4745 | out non-ASCII characters in all modes. | out non-ASCII characters in all modes. |
4746 | ||
4747 | The \c facility was designed for use with ASCII characters, but with | The \c facility was designed for use with ASCII characters, but with |
4748 | the extension to Unicode it is even less useful than it once was. It | the extension to Unicode it is even less useful than it once was. It |
4749 | is, however, recognized when PCRE is compiled in EBCDIC mode, where | is, however, recognized when PCRE is compiled in EBCDIC mode, where |
4750 | data items are always bytes. In this mode, all values are valid after | data items are always bytes. In this mode, all values are valid after |
4751 | \c. If the next character is a lower case letter, it is converted to | \c. If the next character is a lower case letter, it is converted to |
4752 | upper case. Then the 0xc0 bits of the byte are inverted. Thus \cA | upper case. Then the 0xc0 bits of the byte are inverted. Thus \cA |
4753 | becomes hex 01, as in ASCII (A is C1), but because the EBCDIC letters | becomes hex 01, as in ASCII (A is C1), but because the EBCDIC letters |
4754 | are disjoint, \cZ becomes hex 29 (Z is E9), and other characters also | are disjoint, \cZ becomes hex 29 (Z is E9), and other characters also |
4755 | generate different values. | generate different values. |
4756 | ||
4757 | By default, after \x, from zero to two hexadecimal digits are read | By default, after \x, from zero to two hexadecimal digits are read |
4758 | (letters can be in upper or lower case). Any number of hexadecimal dig- | (letters can be in upper or lower case). Any number of hexadecimal dig- |
4759 | its may appear between \x{ and }, but the character code is constrained | its may appear between \x{ and }, but the character code is constrained |
4760 | as follows: | as follows: |
# | Line 4762 BACKSLASH | Line 4766 BACKSLASH |
4766 | 32-bit non-UTF mode less than 0x80000000 | 32-bit non-UTF mode less than 0x80000000 |
4767 | 32-bit UTF-32 mode less than 0x10ffff and a valid codepoint | 32-bit UTF-32 mode less than 0x10ffff and a valid codepoint |
4768 | ||
4769 | Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so- | Invalid Unicode codepoints are the range 0xd800 to 0xdfff (the so- |
4770 | called "surrogate" codepoints), and 0xffef. | called "surrogate" codepoints), and 0xffef. |
4771 | ||
4772 | If characters other than hexadecimal digits appear between \x{ and }, | If characters other than hexadecimal digits appear between \x{ and }, |
4773 | or if there is no terminating }, this form of escape is not recognized. | or if there is no terminating }, this form of escape is not recognized. |
4774 | Instead, the initial \x will be interpreted as a basic hexadecimal | Instead, the initial \x will be interpreted as a basic hexadecimal |
4775 | escape, with no following digits, giving a character whose value is | escape, with no following digits, giving a character whose value is |
4776 | zero. | zero. |
4777 | ||
4778 | If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x | If the PCRE_JAVASCRIPT_COMPAT option is set, the interpretation of \x |
4779 | is as just described only when it is followed by two hexadecimal dig- | is as just described only when it is followed by two hexadecimal dig- |
4780 | its. Otherwise, it matches a literal "x" character. In JavaScript | its. Otherwise, it matches a literal "x" character. In JavaScript |
4781 | mode, support for code points greater than 256 is provided by \u, which | mode, support for code points greater than 256 is provided by \u, which |
4782 | must be followed by four hexadecimal digits; otherwise it matches a | must be followed by four hexadecimal digits; otherwise it matches a |
4783 | literal "u" character. Character codes specified by \u in JavaScript | literal "u" character. Character codes specified by \u in JavaScript |
4784 | mode are constrained in the same was as those specified by \x in non- | mode are constrained in the same was as those specified by \x in non- |
4785 | JavaScript mode. | JavaScript mode. |
4786 | ||
4787 | Characters whose value is less than 256 can be defined by either of the | Characters whose value is less than 256 can be defined by either of the |
4788 | two syntaxes for \x (or by \u in JavaScript mode). There is no differ- | two syntaxes for \x (or by \u in JavaScript mode). There is no differ- |
4789 | ence in the way they are handled. For example, \xdc is exactly the same | ence in the way they are handled. For example, \xdc is exactly the same |
4790 | as \x{dc} (or \u00dc in JavaScript mode). | as \x{dc} (or \u00dc in JavaScript mode). |
4791 | ||
4792 | After \0 up to two further octal digits are read. If there are fewer | After \0 up to two further octal digits are read. If there are fewer |
4793 | than two digits, just those that are present are used. Thus the | than two digits, just those that are present are used. Thus the |
4794 | sequence \0\x\07 specifies two binary zeros followed by a BEL character | sequence \0\x\07 specifies two binary zeros followed by a BEL character |
4795 | (code value 7). Make sure you supply two digits after the initial zero | (code value 7). Make sure you supply two digits after the initial zero |
4796 | if the pattern character that follows is itself an octal digit. | if the pattern character that follows is itself an octal digit. |
4797 | ||
4798 | The handling of a backslash followed by a digit other than 0 is compli- | The handling of a backslash followed by a digit other than 0 is compli- |
4799 | cated. Outside a character class, PCRE reads it and any following dig- | cated. Outside a character class, PCRE reads it and any following dig- |
4800 | its as a decimal number. If the number is less than 10, or if there | its as a decimal number. If the number is less than 10, or if there |
4801 | have been at least that many previous capturing left parentheses in the | have been at least that many previous capturing left parentheses in the |
4802 | expression, the entire sequence is taken as a back reference. A | expression, the entire sequence is taken as a back reference. A |
4803 | description of how this works is given later, following the discussion | description of how this works is given later, following the discussion |
4804 | of parenthesized subpatterns. | of parenthesized subpatterns. |
4805 | ||
4806 | Inside a character class, or if the decimal number is greater than 9 | Inside a character class, or if the decimal number is greater than 9 |
4807 | and there have not been that many capturing subpatterns, PCRE re-reads | and there have not been that many capturing subpatterns, PCRE re-reads |
4808 | up to three octal digits following the backslash, and uses them to gen- | up to three octal digits following the backslash, and uses them to gen- |
4809 | erate a data character. Any subsequent digits stand for themselves. The | erate a data character. Any subsequent digits stand for themselves. The |
4810 | value of the character is constrained in the same way as characters | value of the character is constrained in the same way as characters |
4811 | specified in hexadecimal. For example: | specified in hexadecimal. For example: |
4812 | ||
4813 | \040 is another way of writing an ASCII space | \040 is another way of writing an ASCII space |
# | Line 4821 BACKSLASH | Line 4825 BACKSLASH |
4825 | \81 is either a back reference, or a binary zero | \81 is either a back reference, or a binary zero |
4826 | followed by the two characters "8" and "1" | followed by the two characters "8" and "1" |
4827 | ||
4828 | Note that octal values of 100 or greater must not be introduced by a | Note that octal values of 100 or greater must not be introduced by a |
4829 | leading zero, because no more than three octal digits are ever read. | leading zero, because no more than three octal digits are ever read. |
4830 | ||
4831 | All the sequences that define a single character value can be used both | All the sequences that define a single character value can be used both |
4832 | inside and outside character classes. In addition, inside a character | inside and outside character classes. In addition, inside a character |
4833 | class, \b is interpreted as the backspace character (hex 08). | class, \b is interpreted as the backspace character (hex 08). |
4834 | ||
4835 | \N is not allowed in a character class. \B, \R, and \X are not special | \N is not allowed in a character class. \B, \R, and \X are not special |
4836 | inside a character class. Like other unrecognized escape sequences, | inside a character class. Like other unrecognized escape sequences, |
4837 | they are treated as the literal characters "B", "R", and "X" by | they are treated as the literal characters "B", "R", and "X" by |
4838 | default, but cause an error if the PCRE_EXTRA option is set. Outside a | default, but cause an error if the PCRE_EXTRA option is set. Outside a |
4839 | character class, these sequences have different meanings. | character class, these sequences have different meanings. |
4840 | ||
4841 | Unsupported escape sequences | Unsupported escape sequences |
4842 | ||
4843 | In Perl, the sequences \l, \L, \u, and \U are recognized by its string | In Perl, the sequences \l, \L, \u, and \U are recognized by its string |
4844 | handler and used to modify the case of following characters. By | handler and used to modify the case of following characters. By |
4845 | default, PCRE does not support these escape sequences. However, if the | default, PCRE does not support these escape sequences. However, if the |
4846 | PCRE_JAVASCRIPT_COMPAT option is set, \U matches a "U" character, and | PCRE_JAVASCRIPT_COMPAT option is set, \U matches a "U" character, and |
4847 | \u can be used to define a character by code point, as described in the | \u can be used to define a character by code point, as described in the |
4848 | previous section. | previous section. |
4849 | ||
4850 | Absolute and relative back references | Absolute and relative back references |
4851 | ||
4852 | The sequence \g followed by an unsigned or a negative number, option- | The sequence \g followed by an unsigned or a negative number, option- |
4853 | ally enclosed in braces, is an absolute or relative back reference. A | ally enclosed in braces, is an absolute or relative back reference. A |
4854 | named back reference can be coded as \g{name}. Back references are dis- | named back reference can be coded as \g{name}. Back references are dis- |
4855 | cussed later, following the discussion of parenthesized subpatterns. | cussed later, following the discussion of parenthesized subpatterns. |
4856 | ||
4857 | Absolute and relative subroutine calls | Absolute and relative subroutine calls |
4858 | ||
4859 | For compatibility with Oniguruma, the non-Perl syntax \g followed by a | For compatibility with Oniguruma, the non-Perl syntax \g followed by a |
4860 | name or a number enclosed either in angle brackets or single quotes, is | name or a number enclosed either in angle brackets or single quotes, is |
4861 | an alternative syntax for referencing a subpattern as a "subroutine". | an alternative syntax for referencing a subpattern as a "subroutine". |
4862 | Details are discussed later. Note that \g{...} (Perl syntax) and | Details are discussed later. Note that \g{...} (Perl syntax) and |
4863 | \g<...> (Oniguruma syntax) are not synonymous. The former is a back | \g<...> (Oniguruma syntax) are not synonymous. The former is a back |
4864 | reference; the latter is a subroutine call. | reference; the latter is a subroutine call. |
4865 | ||
4866 | Generic character types | Generic character types |
# | Line 4875 BACKSLASH | Line 4879 BACKSLASH |
4879 | \W any "non-word" character | \W any "non-word" character |
4880 | ||
4881 | There is also the single sequence \N, which matches a non-newline char- | There is also the single sequence \N, which matches a non-newline char- |
4882 | acter. This is the same as the "." metacharacter when PCRE_DOTALL is | acter. This is the same as the "." metacharacter when PCRE_DOTALL is |
4883 | not set. Perl also uses \N to match characters by name; PCRE does not | not set. Perl also uses \N to match characters by name; PCRE does not |
4884 | support this. | support this. |
4885 | ||
4886 | Each pair of lower and upper case escape sequences partitions the com- | Each pair of lower and upper case escape sequences partitions the com- |
4887 | plete set of characters into two disjoint sets. Any given character | plete set of characters into two disjoint sets. Any given character |
4888 | matches one, and only one, of each pair. The sequences can appear both | matches one, and only one, of each pair. The sequences can appear both |
4889 | inside and outside character classes. They each match one character of | inside and outside character classes. They each match one character of |
4890 | the appropriate type. If the current matching point is at the end of | the appropriate type. If the current matching point is at the end of |
4891 | the subject string, all of them fail, because there is no character to | the subject string, all of them fail, because there is no character to |
4892 | match. | match. |
4893 | ||
4894 | For compatibility with Perl, \s does not match the VT character (code | For compatibility with Perl, \s does not match the VT character (code |
4895 | 11). This makes it different from the the POSIX "space" class. The \s | 11). This makes it different from the the POSIX "space" class. The \s |
4896 | characters are HT (9), LF (10), FF (12), CR (13), and space (32). If | characters are HT (9), LF (10), FF (12), CR (13), and space (32). If |
4897 | "use locale;" is included in a Perl script, \s may match the VT charac- | "use locale;" is included in a Perl script, \s may match the VT charac- |
4898 | ter. In PCRE, it never does. | ter. In PCRE, it never does. |
4899 | ||
4900 | A "word" character is an underscore or any character that is a letter | A "word" character is an underscore or any character that is a letter |
4901 | or digit. By default, the definition of letters and digits is con- | or digit. By default, the definition of letters and digits is con- |
4902 | trolled by PCRE's low-valued character tables, and may vary if locale- | trolled by PCRE's low-valued character tables, and may vary if locale- |
4903 | specific matching is taking place (see "Locale support" in the pcreapi | specific matching is taking place (see "Locale support" in the pcreapi |
4904 | page). For example, in a French locale such as "fr_FR" in Unix-like | page). For example, in a French locale such as "fr_FR" in Unix-like |
4905 | systems, or "french" in Windows, some character codes greater than 128 | systems, or "french" in Windows, some character codes greater than 128 |
4906 | are used for accented letters, and these are then matched by \w. The | are used for accented letters, and these are then matched by \w. The |
4907 | use of locales with Unicode is discouraged. | use of locales with Unicode is discouraged. |
4908 | ||
4909 | By default, in a UTF mode, characters with values greater than 128 | By default, in a UTF mode, characters with values greater than 128 |
4910 | never match \d, \s, or \w, and always match \D, \S, and \W. These | never match \d, \s, or \w, and always match \D, \S, and \W. These |
4911 | sequences retain their original meanings from before UTF support was | sequences retain their original meanings from before UTF support was |
4912 | available, mainly for efficiency reasons. However, if PCRE is compiled | available, mainly for efficiency reasons. However, if PCRE is compiled |
4913 | with Unicode property support, and the PCRE_UCP option is set, the be- | with Unicode property support, and the PCRE_UCP option is set, the be- |
4914 | haviour is changed so that Unicode properties are used to determine | haviour is changed so that Unicode properties are used to determine |
4915 | character types, as follows: | character types, as follows: |
4916 | ||
4917 | \d any character that \p{Nd} matches (decimal digit) | \d any character that \p{Nd} matches (decimal digit) |
4918 | \s any character that \p{Z} matches, plus HT, LF, FF, CR | \s any character that \p{Z} matches, plus HT, LF, FF, CR |
4919 | \w any character that \p{L} or \p{N} matches, plus underscore | \w any character that \p{L} or \p{N} matches, plus underscore |
4920 | ||
4921 | The upper case escapes match the inverse sets of characters. Note that | The upper case escapes match the inverse sets of characters. Note that |
4922 | \d matches only decimal digits, whereas \w matches any Unicode digit, | \d matches only decimal digits, whereas \w matches any Unicode digit, |
4923 | as well as any Unicode letter, and underscore. Note also that PCRE_UCP | as well as any Unicode letter, and underscore. Note also that PCRE_UCP |
4924 | affects \b, and \B because they are defined in terms of \w and \W. | affects \b, and \B because they are defined in terms of \w and \W. |
4925 | Matching these sequences is noticeably slower when PCRE_UCP is set. | Matching these sequences is noticeably slower when PCRE_UCP is set. |
4926 | ||
4927 | The sequences \h, \H, \v, and \V are features that were added to Perl | The sequences \h, \H, \v, and \V are features that were added to Perl |
4928 | at release 5.10. In contrast to the other sequences, which match only | at release 5.10. In contrast to the other sequences, which match only |
4929 | ASCII characters by default, these always match certain high-valued | ASCII characters by default, these always match certain high-valued |
4930 | codepoints, whether or not PCRE_UCP is set. The horizontal space char- | codepoints, whether or not PCRE_UCP is set. The horizontal space char- |
4931 | acters are: | acters are: |
4932 | ||
4933 | U+0009 Horizontal tab (HT) | U+0009 Horizontal tab (HT) |
# | Line 4961 BACKSLASH | Line 4965 BACKSLASH |
4965 | ||
4966 | Newline sequences | Newline sequences |
4967 | ||
4968 | Outside a character class, by default, the escape sequence \R matches | Outside a character class, by default, the escape sequence \R matches |
4969 | any Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent | any Unicode newline sequence. In 8-bit non-UTF-8 mode \R is equivalent |
4970 | to the following: | to the following: |
4971 | ||
4972 | (?>\r\n|\n|\x0b|\f|\r|\x85) | (?>\r\n|\n|\x0b|\f|\r|\x85) |
4973 | ||
4974 | This is an example of an "atomic group", details of which are given | This is an example of an "atomic group", details of which are given |
4975 | below. This particular group matches either the two-character sequence | below. This particular group matches either the two-character sequence |
4976 | CR followed by LF, or one of the single characters LF (linefeed, | CR followed by LF, or one of the single characters LF (linefeed, |
4977 | U+000A), VT (vertical tab, U+000B), FF (form feed, U+000C), CR (car- | U+000A), VT (vertical tab, U+000B), FF (form feed, U+000C), CR (car- |
4978 | riage return, U+000D), or NEL (next line, U+0085). The two-character | riage return, U+000D), or NEL (next line, U+0085). The two-character |
4979 | sequence is treated as a single unit that cannot be split. | sequence is treated as a single unit that cannot be split. |
4980 | ||
4981 | In other modes, two additional characters whose codepoints are greater | In other modes, two additional characters whose codepoints are greater |
4982 | than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- | than 255 are added: LS (line separator, U+2028) and PS (paragraph sepa- |
4983 | rator, U+2029). Unicode character property support is not needed for | rator, U+2029). Unicode character property support is not needed for |
4984 | these characters to be recognized. | these characters to be recognized. |
4985 | ||
4986 | It is possible to restrict \R to match only CR, LF, or CRLF (instead of | It is possible to restrict \R to match only CR, LF, or CRLF (instead of |
4987 | the complete set of Unicode line endings) by setting the option | the complete set of Unicode line endings) by setting the option |
4988 | PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched. | PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched. |
4989 | (BSR is an abbrevation for "backslash R".) This can be made the default | (BSR is an abbrevation for "backslash R".) This can be made the default |
4990 | when PCRE is built; if this is the case, the other behaviour can be | when PCRE is built; if this is the case, the other behaviour can be |
4991 | requested via the PCRE_BSR_UNICODE option. It is also possible to | requested via the PCRE_BSR_UNICODE option. It is also possible to |
4992 | specify these settings by starting a pattern string with one of the | specify these settings by starting a pattern string with one of the |
4993 | following sequences: | following sequences: |
4994 | ||
4995 | (*BSR_ANYCRLF) CR, LF, or CRLF only | (*BSR_ANYCRLF) CR, LF, or CRLF only |
4996 | (*BSR_UNICODE) any Unicode newline sequence | (*BSR_UNICODE) any Unicode newline sequence |
4997 | ||
4998 | These override the default and the options given to the compiling func- | These override the default and the options given to the compiling func- |
4999 | tion, but they can themselves be overridden by options given to a | tion, but they can themselves be overridden by options given to a |
5000 | matching function. Note that these special settings, which are not | matching function. Note that these special settings, which are not |
5001 | Perl-compatible, are recognized only at the very start of a pattern, | Perl-compatible, are recognized only at the very start of a pattern, |
5002 | and that they must be in upper case. If more than one of them is | and that they must be in upper case. If more than one of them is |
5003 | present, the last one is used. They can be combined with a change of | present, the last one is used. They can be combined with a change of |
5004 | newline convention; for example, a pattern can start with: | newline convention; for example, a pattern can start with: |
5005 | ||
5006 | (*ANY)(*BSR_ANYCRLF) | (*ANY)(*BSR_ANYCRLF) |
5007 | ||
5008 | They can also be combined with the (*UTF8), (*UTF16), (*UTF32) or | They can also be combined with the (*UTF8), (*UTF16), (*UTF32), (*UTF) |
5009 | (*UCP) special sequences. Inside a character class, \R is treated as an | or (*UCP) special sequences. Inside a character class, \R is treated as |
5010 | unrecognized escape sequence, and so matches the letter "R" by default, | an unrecognized escape sequence, and so matches the letter "R" by |
5011 | but causes an error if PCRE_EXTRA is set. | default, but causes an error if PCRE_EXTRA is set. |
5012 | ||
5013 | Unicode character properties | Unicode character properties |
5014 | ||
5015 | When PCRE is built with Unicode character property support, three addi- | When PCRE is built with Unicode character property support, three addi- |
5016 | tional escape sequences that match characters with specific properties | tional escape sequences that match characters with specific properties |
5017 | are available. When in 8-bit non-UTF-8 mode, these sequences are of | are available. When in 8-bit non-UTF-8 mode, these sequences are of |
5018 | course limited to testing characters whose codepoints are less than | course limited to testing characters whose codepoints are less than |
5019 | 256, but they do work in this mode. The extra escape sequences are: | 256, but they do work in this mode. The extra escape sequences are: |
5020 | ||
5021 | \p{xx} a character with the xx property | \p{xx} a character with the xx property |
5022 | \P{xx} a character without the xx property | \P{xx} a character without the xx property |
5023 | \X a Unicode extended grapheme cluster | \X a Unicode extended grapheme cluster |
5024 | ||
5025 | The property names represented by xx above are limited to the Unicode | The property names represented by xx above are limited to the Unicode |
5026 | script names, the general category properties, "Any", which matches any | script names, the general category properties, "Any", which matches any |
5027 | character (including newline), and some special PCRE properties | character (including newline), and some special PCRE properties |
5028 | (described in the next section). Other Perl properties such as "InMu- | (described in the next section). Other Perl properties such as "InMu- |
5029 | sicalSymbols" are not currently supported by PCRE. Note that \P{Any} | sicalSymbols" are not currently supported by PCRE. Note that \P{Any} |
5030 | does not match any characters, so always causes a match failure. | does not match any characters, so always causes a match failure. |
5031 | ||
5032 | Sets of Unicode characters are defined as belonging to certain scripts. | Sets of Unicode characters are defined as belonging to certain scripts. |
5033 | A character from one of these sets can be matched using a script name. | A character from one of these sets can be matched using a script name. |
5034 | For example: | For example: |
5035 | ||
5036 | \p{Greek} | \p{Greek} |
5037 | \P{Han} | \P{Han} |
5038 | ||
5039 | Those that are not part of an identified script are lumped together as | Those that are not part of an identified script are lumped together as |
5040 | "Common". The current list of scripts is: | "Common". The current list of scripts is: |
5041 | ||
5042 | Arabic, Armenian, Avestan, Balinese, Bamum, Batak, Bengali, Bopomofo, | Arabic, Armenian, Avestan, Balinese, Bamum, Batak, Bengali, Bopomofo, |
5043 | Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Carian, Chakma, | Brahmi, Braille, Buginese, Buhid, Canadian_Aboriginal, Carian, Chakma, |
5044 | Cham, Cherokee, Common, Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, | Cham, Cherokee, Common, Coptic, Cuneiform, Cypriot, Cyrillic, Deseret, |
5045 | Devanagari, Egyptian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, | Devanagari, Egyptian_Hieroglyphs, Ethiopic, Georgian, Glagolitic, |
5046 | Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- | Gothic, Greek, Gujarati, Gurmukhi, Han, Hangul, Hanunoo, Hebrew, Hira- |
5047 | gana, Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscrip- | gana, Imperial_Aramaic, Inherited, Inscriptional_Pahlavi, Inscrip- |
5048 | tional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li, | tional_Parthian, Javanese, Kaithi, Kannada, Katakana, Kayah_Li, |
5049 | Kharoshthi, Khmer, Lao, Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, | Kharoshthi, Khmer, Lao, Latin, Lepcha, Limbu, Linear_B, Lisu, Lycian, |
5050 | Lydian, Malayalam, Mandaic, Meetei_Mayek, Meroitic_Cursive, | Lydian, Malayalam, Mandaic, Meetei_Mayek, Meroitic_Cursive, |
5051 | Meroitic_Hieroglyphs, Miao, Mongolian, Myanmar, New_Tai_Lue, Nko, | Meroitic_Hieroglyphs, Miao, Mongolian, Myanmar, New_Tai_Lue, Nko, |
5052 | Ogham, Old_Italic, Old_Persian, Old_South_Arabian, Old_Turkic, | Ogham, Old_Italic, Old_Persian, Old_South_Arabian, Old_Turkic, |
5053 | Ol_Chiki, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic, Samari- | Ol_Chiki, Oriya, Osmanya, Phags_Pa, Phoenician, Rejang, Runic, Samari- |
5054 | tan, Saurashtra, Sharada, Shavian, Sinhala, Sora_Sompeng, Sundanese, | tan, Saurashtra, Sharada, Shavian, Sinhala, Sora_Sompeng, Sundanese, |
5055 | Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, | Syloti_Nagri, Syriac, Tagalog, Tagbanwa, Tai_Le, Tai_Tham, Tai_Viet, |
5056 | Takri, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Vai, | Takri, Tamil, Telugu, Thaana, Thai, Tibetan, Tifinagh, Ugaritic, Vai, |
5057 | Yi. | Yi. |
5058 | ||
5059 | Each character has exactly one Unicode general category property, spec- | Each character has exactly one Unicode general category property, spec- |
5060 | ified by a two-letter abbreviation. For compatibility with Perl, nega- | ified by a two-letter abbreviation. For compatibility with Perl, nega- |
5061 | tion can be specified by including a circumflex between the opening | tion can be specified by including a circumflex between the opening |
5062 | brace and the property name. For example, \p{^Lu} is the same as | brace and the property name. For example, \p{^Lu} is the same as |
5063 | \P{Lu}. | \P{Lu}. |
5064 | ||
5065 | If only one letter is specified with \p or \P, it includes all the gen- | If only one letter is specified with \p or \P, it includes all the gen- |
5066 | eral category properties that start with that letter. In this case, in | eral category properties that start with that letter. In this case, in |
5067 | the absence of negation, the curly brackets in the escape sequence are | the absence of negation, the curly brackets in the escape sequence are |
5068 | optional; these two examples have the same effect: | optional; these two examples have the same effect: |
5069 | ||
5070 | \p{L} | \p{L} |
# | Line 5112 BACKSLASH | Line 5116 BACKSLASH |
5116 | Zp Paragraph separator | Zp Paragraph separator |
5117 | Zs Space separator | Zs Space separator |
5118 | ||
5119 | The special property L& is also supported: it matches a character that | The special property L& is also supported: it matches a character that |
5120 | has the Lu, Ll, or Lt property, in other words, a letter that is not | has the Lu, Ll, or Lt property, in other words, a letter that is not |
5121 | classified as a modifier or "other". | classified as a modifier or "other". |
5122 | ||
5123 | The Cs (Surrogate) property applies only to characters in the range | The Cs (Surrogate) property applies only to characters in the range |
5124 | U+D800 to U+DFFF. Such characters are not valid in Unicode strings and | U+D800 to U+DFFF. Such characters are not valid in Unicode strings and |
5125 | so cannot be tested by PCRE, unless UTF validity checking has been | so cannot be tested by PCRE, unless UTF validity checking has been |
5126 | turned off (see the discussion of PCRE_NO_UTF8_CHECK, | turned off (see the discussion of PCRE_NO_UTF8_CHECK, |
5127 | PCRE_NO_UTF16_CHECK and PCRE_NO_UTF32_CHECK in the pcreapi page). Perl | PCRE_NO_UTF16_CHECK and PCRE_NO_UTF32_CHECK in the pcreapi page). Perl |
5128 | does not support the Cs property. | does not support the Cs property. |
5129 | ||
5130 | The long synonyms for property names that Perl supports (such as | The long synonyms for property names that Perl supports (such as |
5131 | \p{Letter}) are not supported by PCRE, nor is it permitted to prefix | \p{Letter}) are not supported by PCRE, nor is it permitted to prefix |
5132 | any of these properties with "Is". | any of these properties with "Is". |
5133 | ||
5134 | No character that is in the Unicode table has the Cn (unassigned) prop- | No character that is in the Unicode table has the Cn (unassigned) prop- |
5135 | erty. Instead, this property is assumed for any code point that is not | erty. Instead, this property is assumed for any code point that is not |
5136 | in the Unicode table. | in the Unicode table. |
5137 | ||
5138 | Specifying caseless matching does not affect these escape sequences. | Specifying caseless matching does not affect these escape sequences. |
5139 | For example, \p{Lu} always matches only upper case letters. | For example, \p{Lu} always matches only upper case letters. |
5140 | ||
5141 | Matching characters by Unicode property is not fast, because PCRE has | Matching characters by Unicode property is not fast, because PCRE has |
5142 | to do a multistage table lookup in order to find a character's prop- | to do a multistage table lookup in order to find a character's prop- |
5143 | erty. That is why the traditional escape sequences such as \d and \w do | erty. That is why the traditional escape sequences such as \d and \w do |
5144 | not use Unicode properties in PCRE by default, though you can make them | not use Unicode properties in PCRE by default, though you can make them |
5145 | do so by setting the PCRE_UCP option or by starting the pattern with | do so by setting the PCRE_UCP option or by starting the pattern with |
5146 | (*UCP). | (*UCP). |
5147 | ||
5148 | Extended grapheme clusters | Extended grapheme clusters |
5149 | ||
5150 | The \X escape matches any number of Unicode characters that form an | The \X escape matches any number of Unicode characters that form an |
5151 | "extended grapheme cluster", and treats the sequence as an atomic group | "extended grapheme cluster", and treats the sequence as an atomic group |
5152 | (see below). Up to and including release 8.31, PCRE matched an ear- | (see below). Up to and including release 8.31, PCRE matched an ear- |
5153 | lier, simpler definition that was equivalent to | lier, simpler definition that was equivalent to |
5154 | ||
5155 | (?>\PM\pM*) | (?>\PM\pM*) |
5156 | ||
5157 | That is, it matched a character without the "mark" property, followed | That is, it matched a character without the "mark" property, followed |
5158 | by zero or more characters with the "mark" property. Characters with | by zero or more characters with the "mark" property. Characters with |
5159 | the "mark" property are typically non-spacing accents that affect the | the "mark" property are typically non-spacing accents that affect the |
5160 | preceding character. | preceding character. |
5161 | ||
5162 | This simple definition was extended in Unicode to include more compli- | This simple definition was extended in Unicode to include more compli- |
5163 | cated kinds of composite character by giving each character a grapheme | cated kinds of composite character by giving each character a grapheme |
5164 | breaking property, and creating rules that use these properties to | breaking property, and creating rules that use these properties to |
5165 | define the boundaries of extended grapheme clusters. In releases of | define the boundaries of extended grapheme clusters. In releases of |
5166 | PCRE later than 8.31, \X matches one of these clusters. | PCRE later than 8.31, \X matches one of these clusters. |
5167 | ||
5168 | \X always matches at least one character. Then it decides whether to | \X always matches at least one character. Then it decides whether to |
5169 | add additional characters according to the following rules for ending a | add additional characters according to the following rules for ending a |
5170 | cluster: | cluster: |
5171 | ||
5172 | 1. End at the end of the subject string. | 1. End at the end of the subject string. |
5173 | ||
5174 | 2. Do not end between CR and LF; otherwise end after any control char- | 2. Do not end between CR and LF; otherwise end after any control char- |
5175 | acter. | acter. |
5176 | ||
5177 | 3. Do not break Hangul (a Korean script) syllable sequences. Hangul | 3. Do not break Hangul (a Korean script) syllable sequences. Hangul |
5178 | characters are of five types: L, V, T, LV, and LVT. An L character may | characters are of five types: L, V, T, LV, and LVT. An L character may |
5179 | be followed by an L, V, LV, or LVT character; an LV or V character may | be followed by an L, V, LV, or LVT character; an LV or V character may |
5180 | be followed by a V or T character; an LVT or T character may be follwed | be followed by a V or T character; an LVT or T character may be follwed |
5181 | only by a T character. | only by a T character. |
5182 | ||
5183 | 4. Do not end before extending characters or spacing marks. Characters | 4. Do not end before extending characters or spacing marks. Characters |
5184 | with the "mark" property always have the "extend" grapheme breaking | with the "mark" property always have the "extend" grapheme breaking |
5185 | property. | property. |
5186 | ||
5187 | 5. Do not end after prepend characters. | 5. Do not end after prepend characters. |
# | Line 5186 BACKSLASH | Line 5190 BACKSLASH |
5190 | ||
5191 | PCRE's additional properties | PCRE's additional properties |
5192 | ||
5193 | As well as the standard Unicode properties described above, PCRE sup- | As well as the standard Unicode properties described above, PCRE sup- |
5194 | ports four more that make it possible to convert traditional escape | ports four more that make it possible to convert traditional escape |
5195 | sequences such as \w and \s and POSIX character classes to use Unicode | sequences such as \w and \s and POSIX character classes to use Unicode |
5196 | properties. PCRE uses these non-standard, non-Perl properties inter- | properties. PCRE uses these non-standard, non-Perl properties inter- |
5197 | nally when PCRE_UCP is set. They are: | nally when PCRE_UCP is set. They are: |
5198 | ||
5199 | Xan Any alphanumeric character | Xan Any alphanumeric character |
# | Line 5197 BACKSLASH | Line 5201 BACKSLASH |
5201 | Xsp Any Perl space character | Xsp Any Perl space character |
5202 | Xwd Any Perl "word" character | Xwd Any Perl "word" character |
5203 | ||
5204 | Xan matches characters that have either the L (letter) or the N (num- | Xan matches characters that have either the L (letter) or the N (num- |
5205 | ber) property. Xps matches the characters tab, linefeed, vertical tab, | ber) property. Xps matches the characters tab, linefeed, vertical tab, |
5206 | form feed, or carriage return, and any other character that has the Z | form feed, or carriage return, and any other character that has the Z |
5207 | (separator) property. Xsp is the same as Xps, except that vertical tab | (separator) property. Xsp is the same as Xps, except that vertical tab |
5208 | is excluded. Xwd matches the same characters as Xan, plus underscore. | is excluded. Xwd matches the same characters as Xan, plus underscore. |
5209 | ||
5210 | Resetting the match start | Resetting the match start |
5211 | ||
5212 | The escape sequence \K causes any previously matched characters not to | The escape sequence \K causes any previously matched characters not to |
5213 | be included in the final matched sequence. For example, the pattern: | be included in the final matched sequence. For example, the pattern: |
5214 | ||
5215 | foo\Kbar | foo\Kbar |
5216 | ||
5217 | matches "foobar", but reports that it has matched "bar". This feature | matches "foobar", but reports that it has matched "bar". This feature |
5218 | is similar to a lookbehind assertion (described below). However, in | is similar to a lookbehind assertion (described below). However, in |
5219 | this case, the part of the subject before the real match does not have | this case, the part of the subject before the real match does not have |
5220 | to be of fixed length, as lookbehind assertions do. The use of \K does | to be of fixed length, as lookbehind assertions do. The use of \K does |
5221 | not interfere with the setting of captured substrings. For example, | not interfere with the setting of captured substrings. For example, |
5222 | when the pattern | when the pattern |
5223 | ||
5224 | (foo)\Kbar | (foo)\Kbar |
5225 | ||
5226 | matches "foobar", the first substring is still set to "foo". | matches "foobar", the first substring is still set to "foo". |
5227 | ||
5228 | Perl documents that the use of \K within assertions is "not well | Perl documents that the use of \K within assertions is "not well |
5229 | defined". In PCRE, \K is acted upon when it occurs inside positive | defined". In PCRE, \K is acted upon when it occurs inside positive |
5230 | assertions, but is ignored in negative assertions. | assertions, but is ignored in negative assertions. |
5231 | ||
5232 | Simple assertions | Simple assertions |
5233 | ||
5234 | The final use of backslash is for certain simple assertions. An asser- | The final use of backslash is for certain simple assertions. An asser- |
5235 | tion specifies a condition that has to be met at a particular point in | tion specifies a condition that has to be met at a particular point in |
5236 | a match, without consuming any characters from the subject string. The | a match, without consuming any characters from the subject string. The |
5237 | use of subpatterns for more complicated assertions is described below. | use of subpatterns for more complicated assertions is described below. |
5238 | The backslashed assertions are: | The backslashed assertions are: |
5239 | ||
5240 | \b matches at a word boundary | \b matches at a word boundary |
# | Line 5241 BACKSLASH | Line 5245 BACKSLASH |
5245 | \z matches only at the end of the subject | \z matches only at the end of the subject |
5246 | \G matches at the first matching position in the subject | \G matches at the first matching position in the subject |
5247 | ||
5248 | Inside a character class, \b has a different meaning; it matches the | Inside a character class, \b has a different meaning; it matches the |
5249 | backspace character. If any other of these assertions appears in a | backspace character. If any other of these assertions appears in a |
5250 | character class, by default it matches the corresponding literal char- | character class, by default it matches the corresponding literal char- |
5251 | acter (for example, \B matches the letter B). However, if the | acter (for example, \B matches the letter B). However, if the |
5252 | PCRE_EXTRA option is set, an "invalid escape sequence" error is gener- | PCRE_EXTRA option is set, an "invalid escape sequence" error is gener- |
5253 | ated instead. | ated instead. |
5254 | ||
5255 | A word boundary is a position in the subject string where the current | A word boundary is a position in the subject string where the current |
5256 | character and the previous character do not both match \w or \W (i.e. | character and the previous character do not both match \w or \W (i.e. |
5257 | one matches \w and the other matches \W), or the start or end of the | one matches \w and the other matches \W), or the start or end of the |
5258 | string if the first or last character matches \w, respectively. In a | string if the first or last character matches \w, respectively. In a |
5259 | UTF mode, the meanings of \w and \W can be changed by setting the | UTF mode, the meanings of \w and \W can be changed by setting the |
5260 | PCRE_UCP option. When this is done, it also affects \b and \B. Neither | PCRE_UCP option. When this is done, it also affects \b and \B. Neither |
5261 | PCRE nor Perl has a separate "start of word" or "end of word" metase- | PCRE nor Perl has a separate "start of word" or "end of word" metase- |
5262 | quence. However, whatever follows \b normally determines which it is. | quence. However, whatever follows \b normally determines which it is. |
5263 | For example, the fragment \ba matches "a" at the start of a word. | For example, the fragment \ba matches "a" at the start of a word. |
5264 | ||
5265 | The \A, \Z, and \z assertions differ from the traditional circumflex | The \A, \Z, and \z assertions differ from the traditional circumflex |
5266 | and dollar (described in the next section) in that they only ever match | and dollar (described in the next section) in that they only ever match |
5267 | at the very start and end of the subject string, whatever options are | at the very start and end of the subject string, whatever options are |
5268 | set. Thus, they are independent of multiline mode. These three asser- | set. Thus, they are independent of multiline mode. These three asser- |
5269 | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which | tions are not affected by the PCRE_NOTBOL or PCRE_NOTEOL options, which |
5270 | affect only the behaviour of the circumflex and dollar metacharacters. | affect only the behaviour of the circumflex and dollar metacharacters. |
5271 | However, if the startoffset argument of pcre_exec() is non-zero, indi- | However, if the startoffset argument of pcre_exec() is non-zero, indi- |
5272 | cating that matching is to start at a point other than the beginning of | cating that matching is to start at a point other than the beginning of |
5273 | the subject, \A can never match. The difference between \Z and \z is | the subject, \A can never match. The difference between \Z and \z is |
5274 | that \Z matches before a newline at the end of the string as well as at | that \Z matches before a newline at the end of the string as well as at |
5275 | the very end, whereas \z matches only at the end. | the very end, whereas \z matches only at the end. |
5276 | ||
5277 | The \G assertion is true only when the current matching position is at | The \G assertion is true only when the current matching position is at |
5278 | the start point of the match, as specified by the startoffset argument | the start point of the match, as specified by the startoffset argument |
5279 | of pcre_exec(). It differs from \A when the value of startoffset is | of pcre_exec(). It differs from \A when the value of startoffset is |
5280 | non-zero. By calling pcre_exec() multiple times with appropriate argu- | non-zero. By calling pcre_exec() multiple times with appropriate argu- |
5281 | ments, you can mimic Perl's /g option, and it is in this kind of imple- | ments, you can mimic Perl's /g option, and it is in this kind of imple- |
5282 | mentation where \G can be useful. | mentation where \G can be useful. |
5283 | ||
5284 | Note, however, that PCRE's interpretation of \G, as the start of the | Note, however, that PCRE's interpretation of \G, as the start of the |
5285 | current match, is subtly different from Perl's, which defines it as the | current match, is subtly different from Perl's, which defines it as the |
5286 | end of the previous match. In Perl, these can be different when the | end of the previous match. In Perl, these can be different when the |
5287 | previously matched string was empty. Because PCRE does just one match | previously matched string was empty. Because PCRE does just one match |
5288 | at a time, it cannot reproduce this behaviour. | at a time, it cannot reproduce this behaviour. |
5289 | ||
5290 | If all the alternatives of a pattern begin with \G, the expression is | If all the alternatives of a pattern begin with \G, the expression is |
5291 | anchored to the starting match position, and the "anchored" flag is set | anchored to the starting match position, and the "anchored" flag is set |
5292 | in the compiled regular expression. | in the compiled regular expression. |
5293 | ||
5294 | ||
5295 | CIRCUMFLEX AND DOLLAR | CIRCUMFLEX AND DOLLAR |
5296 | ||
5297 | The circumflex and dollar metacharacters are zero-width assertions. | |
5298 | That is, they test for a particular condition being true without con- | |
5299 | suming any characters from the subject string. | |
5300 | ||
5301 | Outside a character class, in the default matching mode, the circumflex | Outside a character class, in the default matching mode, the circumflex |
5302 | character is an assertion that is true only if the current matching | character is an assertion that is true only if the current matching |
5303 | point is at the start of the subject string. If the startoffset argu- | point is at the start of the subject string. If the startoffset argu- |
5304 | ment of pcre_exec() is non-zero, circumflex can never match if the | ment of pcre_exec() is non-zero, circumflex can never match if the |
5305 | PCRE_MULTILINE option is unset. Inside a character class, circumflex | PCRE_MULTILINE option is unset. Inside a character class, circumflex |
5306 | has an entirely different meaning (see below). | has an entirely different meaning (see below). |
5307 | ||
5308 | Circumflex need not be the first character of the pattern if a number | Circumflex need not be the first character of the pattern if a number |
5309 | of alternatives are involved, but it should be the first thing in each | of alternatives are involved, but it should be the first thing in each |
5310 | alternative in which it appears if the pattern is ever to match that | alternative in which it appears if the pattern is ever to match that |
5311 | branch. If all possible alternatives start with a circumflex, that is, | branch. If all possible alternatives start with a circumflex, that is, |
5312 | if the pattern is constrained to match only at the start of the sub- | if the pattern is constrained to match only at the start of the sub- |
5313 | ject, it is said to be an "anchored" pattern. (There are also other | ject, it is said to be an "anchored" pattern. (There are also other |
5314 | constructs that can cause a pattern to be anchored.) | constructs that can cause a pattern to be anchored.) |
5315 | ||
5316 | A dollar character is an assertion that is true only if the current | The dollar character is an assertion that is true only if the current |
5317 | matching point is at the end of the subject string, or immediately | matching point is at the end of the subject string, or immediately |
5318 | before a newline at the end of the string (by default). Dollar need not | before a newline at the end of the string (by default). Note, however, |
5319 | be the last character of the pattern if a number of alternatives are | that it does not actually match the newline. Dollar need not be the |
5320 | involved, but it should be the last item in any branch in which it | last character of the pattern if a number of alternatives are involved, |
5321 | appears. Dollar has no special meaning in a character class. | but it should be the last item in any branch in which it appears. Dol- |
5322 | lar has no special meaning in a character class. | |
5323 | ||
5324 | The meaning of dollar can be changed so that it matches only at the | The meaning of dollar can be changed so that it matches only at the |
5325 | very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at | very end of the string, by setting the PCRE_DOLLAR_ENDONLY option at |
# | Line 5640 INTERNAL OPTION SETTING | Line 5649 INTERNAL OPTION SETTING |
5649 | some cases the pattern can contain special leading sequences such as | some cases the pattern can contain special leading sequences such as |
5650 | (*CRLF) to override what the application has set or what has been | (*CRLF) to override what the application has set or what has been |
5651 | defaulted. Details are given in the section entitled "Newline | defaulted. Details are given in the section entitled "Newline |
5652 | sequences" above. There are also the (*UTF8), (*UTF16),(*UTF32) and | sequences" above. There are also the (*UTF8), (*UTF16),(*UTF32), and |
5653 | (*UCP) leading sequences that can be used to set UTF and Unicode prop- | (*UCP) leading sequences that can be used to set UTF and Unicode prop- |
5654 | erty modes; they are equivalent to setting the PCRE_UTF8, PCRE_UTF16, | erty modes; they are equivalent to setting the PCRE_UTF8, PCRE_UTF16, |
5655 | PCRE_UTF32 and the PCRE_UCP options, respectively. | PCRE_UTF32 and the PCRE_UCP options, respectively. The (*UTF) sequence |
5656 | is a generic version that can be used with any of the libraries. | |
5657 | ||
5658 | ||
5659 | SUBPATTERNS | SUBPATTERNS |
# | Line 5655 SUBPATTERNS | Line 5665 SUBPATTERNS |
5665 | ||
5666 | cat(aract|erpillar|) | cat(aract|erpillar|) |
5667 | ||
5668 | matches "cataract", "caterpillar", or "cat". Without the parentheses, | matches "cataract", "caterpillar", or "cat". Without the parentheses, |
5669 | it would match "cataract", "erpillar" or an empty string. | it would match "cataract", "erpillar" or an empty string. |
5670 | ||
5671 | 2. It sets up the subpattern as a capturing subpattern. This means | 2. It sets up the subpattern as a capturing subpattern. This means |
5672 | that, when the whole pattern matches, that portion of the subject | that, when the whole pattern matches, that portion of the subject |
5673 | string that matched the subpattern is passed back to the caller via the | string that matched the subpattern is passed back to the caller via the |
5674 | ovector argument of the matching function. (This applies only to the | ovector argument of the matching function. (This applies only to the |
5675 | traditional matching functions; the DFA matching functions do not sup- | traditional matching functions; the DFA matching functions do not sup- |
5676 | port capturing.) | port capturing.) |
5677 | ||
5678 | Opening parentheses are counted from left to right (starting from 1) to | Opening parentheses are counted from left to right (starting from 1) to |
5679 | obtain numbers for the capturing subpatterns. For example, if the | obtain numbers for the capturing subpatterns. For example, if the |
5680 | string "the red king" is matched against the pattern | string "the red king" is matched against the pattern |
5681 | ||
5682 | the ((red|white) (king|queen)) | the ((red|white) (king|queen)) |
# | Line 5674 SUBPATTERNS | Line 5684 SUBPATTERNS |
5684 | the captured substrings are "red king", "red", and "king", and are num- | the captured substrings are "red king", "red", and "king", and are num- |
5685 | bered 1, 2, and 3, respectively. | bered 1, 2, and 3, respectively. |
5686 | ||
5687 | The fact that plain parentheses fulfil two functions is not always | The fact that plain parentheses fulfil two functions is not always |
5688 | helpful. There are often times when a grouping subpattern is required | helpful. There are often times when a grouping subpattern is required |
5689 | without a capturing requirement. If an opening parenthesis is followed | without a capturing requirement. If an opening parenthesis is followed |
5690 | by a question mark and a colon, the subpattern does not do any captur- | by a question mark and a colon, the subpattern does not do any captur- |
5691 | ing, and is not counted when computing the number of any subsequent | ing, and is not counted when computing the number of any subsequent |
5692 | capturing subpatterns. For example, if the string "the white queen" is | capturing subpatterns. For example, if the string "the white queen" is |
5693 | matched against the pattern | matched against the pattern |
5694 | ||
5695 | the ((?:red|white) (king|queen)) | the ((?:red|white) (king|queen)) |
# | Line 5687 SUBPATTERNS | Line 5697 SUBPATTERNS |
5697 | the captured substrings are "white queen" and "queen", and are numbered | the captured substrings are "white queen" and "queen", and are numbered |
5698 | 1 and 2. The maximum number of capturing subpatterns is 65535. | 1 and 2. The maximum number of capturing subpatterns is 65535. |
5699 | ||
5700 | As a convenient shorthand, if any option settings are required at the | As a convenient shorthand, if any option settings are required at the |
5701 | start of a non-capturing subpattern, the option letters may appear | start of a non-capturing subpattern, the option letters may appear |
5702 | between the "?" and the ":". Thus the two patterns | between the "?" and the ":". Thus the two patterns |
5703 | ||
5704 | (?i:saturday|sunday) | (?i:saturday|sunday) |
5705 | (?:(?i)saturday|sunday) | (?:(?i)saturday|sunday) |
5706 | ||
5707 | match exactly the same set of strings. Because alternative branches are | match exactly the same set of strings. Because alternative branches are |
5708 | tried from left to right, and options are not reset until the end of | tried from left to right, and options are not reset until the end of |
5709 | the subpattern is reached, an option setting in one branch does affect | the subpattern is reached, an option setting in one branch does affect |
5710 | subsequent branches, so the above patterns match "SUNDAY" as well as | subsequent branches, so the above patterns match "SUNDAY" as well as |
5711 | "Saturday". | "Saturday". |
5712 | ||
5713 | ||
5714 | DUPLICATE SUBPATTERN NUMBERS | DUPLICATE SUBPATTERN NUMBERS |
5715 | ||
5716 | Perl 5.10 introduced a feature whereby each alternative in a subpattern | Perl 5.10 introduced a feature whereby each alternative in a subpattern |
5717 | uses the same numbers for its capturing parentheses. Such a subpattern | uses the same numbers for its capturing parentheses. Such a subpattern |
5718 | starts with (?| and is itself a non-capturing subpattern. For example, | starts with (?| and is itself a non-capturing subpattern. For example, |
5719 | consider this pattern: | consider this pattern: |
5720 | ||
5721 | (?|(Sat)ur|(Sun))day | (?|(Sat)ur|(Sun))day |
5722 | ||
5723 | Because the two alternatives are inside a (?| group, both sets of cap- | Because the two alternatives are inside a (?| group, both sets of cap- |
5724 | turing parentheses are numbered one. Thus, when the pattern matches, | turing parentheses are numbered one. Thus, when the pattern matches, |
5725 | you can look at captured substring number one, whichever alternative | you can look at captured substring number one, whichever alternative |
5726 | matched. This construct is useful when you want to capture part, but | matched. This construct is useful when you want to capture part, but |
5727 | not all, of one of a number of alternatives. Inside a (?| group, paren- | not all, of one of a number of alternatives. Inside a (?| group, paren- |
5728 | theses are numbered as usual, but the number is reset at the start of | theses are numbered as usual, but the number is reset at the start of |
5729 | each branch. The numbers of any capturing parentheses that follow the | each branch. The numbers of any capturing parentheses that follow the |
5730 | subpattern start after the highest number used in any branch. The fol- | subpattern start after the highest number used in any branch. The fol- |
5731 | lowing example is taken from the Perl documentation. The numbers under- | lowing example is taken from the Perl documentation. The numbers under- |
5732 | neath show in which buffer the captured content will be stored. | neath show in which buffer the captured content will be stored. |
5733 | ||
# | Line 5725 DUPLICATE SUBPATTERN NUMBERS | Line 5735 DUPLICATE SUBPATTERN NUMBERS |
5735 | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x | / ( a ) (?| x ( y ) z | (p (q) r) | (t) u (v) ) ( z ) /x |
5736 | # 1 2 2 3 2 3 4 | # 1 2 2 3 2 3 4 |
5737 | ||
5738 | A back reference to a numbered subpattern uses the most recent value | A back reference to a numbered subpattern uses the most recent value |
5739 | that is set for that number by any subpattern. The following pattern | that is set for that number by any subpattern. The following pattern |
5740 | matches "abcabc" or "defdef": | matches "abcabc" or "defdef": |
5741 | ||
5742 | /(?|(abc)|(def))\1/ | /(?|(abc)|(def))\1/ |
5743 | ||
5744 | In contrast, a subroutine call to a numbered subpattern always refers | In contrast, a subroutine call to a numbered subpattern always refers |
5745 | to the first one in the pattern with the given number. The following | to the first one in the pattern with the given number. The following |
5746 | pattern matches "abcabc" or "defabc": | pattern matches "abcabc" or "defabc": |
5747 | ||
5748 | /(?|(abc)|(def))(?1)/ | /(?|(abc)|(def))(?1)/ |
5749 | ||
5750 | If a condition test for a subpattern's having matched refers to a non- | If a condition test for a subpattern's having matched refers to a non- |
5751 | unique number, the test is true if any of the subpatterns of that num- | unique number, the test is true if any of the subpatterns of that num- |
5752 | ber have matched. | ber have matched. |
5753 | ||
5754 | An alternative approach to using this "branch reset" feature is to use | An alternative approach to using this "branch reset" feature is to use |
5755 | duplicate named subpatterns, as described in the next section. | duplicate named subpatterns, as described in the next section. |
5756 | ||
5757 | ||
5758 | NAMED SUBPATTERNS | NAMED SUBPATTERNS |
5759 | ||
5760 | Identifying capturing parentheses by number is simple, but it can be | Identifying capturing parentheses by number is simple, but it can be |
5761 | very hard to keep track of the numbers in complicated regular expres- | very hard to keep track of the numbers in complicated regular expres- |
5762 | sions. Furthermore, if an expression is modified, the numbers may | sions. Furthermore, if an expression is modified, the numbers may |
5763 | change. To help with this difficulty, PCRE supports the naming of sub- | change. To help with this difficulty, PCRE supports the naming of sub- |
5764 | patterns. This feature was not added to Perl until release 5.10. Python | patterns. This feature was not added to Perl until release 5.10. Python |
5765 | had the feature earlier, and PCRE introduced it at release 4.0, using | had the feature earlier, and PCRE introduced it at release 4.0, using |
5766 | the Python syntax. PCRE now supports both the Perl and the Python syn- | the Python syntax. PCRE now supports both the Perl and the Python syn- |
5767 | tax. Perl allows identically numbered subpatterns to have different | tax. Perl allows identically numbered subpatterns to have different |
5768 | names, but PCRE does not. | names, but PCRE does not. |
5769 | ||
5770 | In PCRE, a subpattern can be named in one of three ways: (?<name>...) | In PCRE, a subpattern can be named in one of three ways: (?<name>...) |
5771 | or (?'name'...) as in Perl, or (?P<name>...) as in Python. References | or (?'name'...) as in Perl, or (?P<name>...) as in Python. References |
5772 | to capturing parentheses from other parts of the pattern, such as back | to capturing parentheses from other parts of the pattern, such as back |
5773 | references, recursion, and conditions, can be made by name as well as | references, recursion, and conditions, can be made by name as well as |
5774 | by number. | by number. |
5775 | ||
5776 | Names consist of up to 32 alphanumeric characters and underscores. | Names consist of up to 32 alphanumeric characters and underscores. |
5777 | Named capturing parentheses are still allocated numbers as well as | Named capturing parentheses are still allocated numbers as well as |
5778 | names, exactly as if the names were not present. The PCRE API provides | names, exactly as if the names were not present. The PCRE API provides |
5779 | function calls for extracting the name-to-number translation table from | function calls for extracting the name-to-number translation table from |
5780 | a compiled pattern. There is also a convenience function for extracting | a compiled pattern. There is also a convenience function for extracting |
5781 | a captured substring by name. | a captured substring by name. |
5782 | ||
5783 | By default, a name must be unique within a pattern, but it is possible | By default, a name must be unique within a pattern, but it is possible |
5784 | to relax this constraint by setting the PCRE_DUPNAMES option at compile | to relax this constraint by setting the PCRE_DUPNAMES option at compile |
5785 | time. (Duplicate names are also always permitted for subpatterns with | time. (Duplicate names are also always permitted for subpatterns with |
5786 | the same number, set up as described in the previous section.) Dupli- | the same number, set up as described in the previous section.) Dupli- |
5787 | cate names can be useful for patterns where only one instance of the | cate names can be useful for patterns where only one instance of the |
5788 | named parentheses can match. Suppose you want to match the name of a | named parentheses can match. Suppose you want to match the name of a |
5789 | weekday, either as a 3-letter abbreviation or as the full name, and in | weekday, either as a 3-letter abbreviation or as the full name, and in |
5790 | both cases you want to extract the abbreviation. This pattern (ignoring | both cases you want to extract the abbreviation. This pattern (ignoring |
5791 | the line breaks) does the job: | the line breaks) does the job: |
5792 | ||
# | Line 5786 NAMED SUBPATTERNS | Line 5796 NAMED SUBPATTERNS |
5796 | (?<DN>Thu)(?:rsday)?| | (?<DN>Thu)(?:rsday)?| |
5797 | (?<DN>Sat)(?:urday)? | (?<DN>Sat)(?:urday)? |
5798 | ||
5799 | There are five capturing substrings, but only one is ever set after a | There are five capturing substrings, but only one is ever set after a |
5800 | match. (An alternative way of solving this problem is to use a "branch | match. (An alternative way of solving this problem is to use a "branch |
5801 | reset" subpattern, as described in the previous section.) | reset" subpattern, as described in the previous section.) |
5802 | ||
5803 | The convenience function for extracting the data by name returns the | The convenience function for extracting the data by name returns the |
5804 | substring for the first (and in this example, the only) subpattern of | substring for the first (and in this example, the only) subpattern of |
5805 | that name that matched. This saves searching to find which numbered | that name that matched. This saves searching to find which numbered |
5806 | subpattern it was. | subpattern it was. |
5807 | ||
5808 | If you make a back reference to a non-unique named subpattern from | If you make a back reference to a non-unique named subpattern from |
5809 | elsewhere in the pattern, the one that corresponds to the first occur- | elsewhere in the pattern, the one that corresponds to the first occur- |
5810 | rence of the name is used. In the absence of duplicate numbers (see the | rence of the name is used. In the absence of duplicate numbers (see the |
5811 | previous section) this is the one with the lowest number. If you use a | previous section) this is the one with the lowest number. If you use a |
5812 | named reference in a condition test (see the section about conditions | named reference in a condition test (see the section about conditions |
5813 | below), either to check whether a subpattern has matched, or to check | below), either to check whether a subpattern has matched, or to check |
5814 | for recursion, all subpatterns with the same name are tested. If the | for recursion, all subpatterns with the same name are tested. If the |
5815 | condition is true for any one of them, the overall condition is true. | condition is true for any one of them, the overall condition is true. |
5816 | This is the same behaviour as testing by number. For further details of | This is the same behaviour as testing by number. For further details of |
5817 | the interfaces for handling named subpatterns, see the pcreapi documen- | the interfaces for handling named subpatterns, see the pcreapi documen- |
5818 | tation. | tation. |
5819 | ||
5820 | Warning: You cannot use different names to distinguish between two sub- | Warning: You cannot use different names to distinguish between two sub- |
5821 | patterns with the same number because PCRE uses only the numbers when | patterns with the same number because PCRE uses only the numbers when |
5822 | matching. For this reason, an error is given at compile time if differ- | matching. For this reason, an error is given at compile time if differ- |
5823 | ent names are given to subpatterns with the same number. However, you | ent names are given to subpatterns with the same number. However, you |
5824 | can give the same name to subpatterns with the same number, even when | can give the same name to subpatterns with the same number, even when |
5825 | PCRE_DUPNAMES is not set. | PCRE_DUPNAMES is not set. |
5826 | ||
5827 | ||
5828 | REPETITION | REPETITION |
5829 | ||
5830 | Repetition is specified by quantifiers, which can follow any of the | Repetition is specified by quantifiers, which can follow &nbs |