cvs: phpdoc-zh /reference/pcre pattern.modifiers.xml pattern.syntax.xml /reference/pcre/functions pcre.pattern.modifiers.xml pcre.pattern.syntax.xml

From: Date: Thu, 07 Jul 2005 10:09:55 +0000
Subject: cvs: phpdoc-zh /reference/pcre pattern.modifiers.xml pattern.syntax.xml /reference/pcre/functions pcre.pattern.modifiers.xml pcre.pattern.syntax.xml
Groups: php.doc.zh 
Request: Send a blank email to doc-zh+get-2013@lists.php.net to get a copy of this message
yincheng Thu Jul 7 06:09:55 2005 EDT Added files: /phpdoc-zh/reference/pcre pattern.modifiers.xml pattern.syntax.xml Removed files: /phpdoc-zh/reference/pcre/functions pcre.pattern.modifiers.xml pcre.pattern.syntax.xml Log: λÖÃÒÆ¶¯

http://cvs.php.net/co.php/phpdoc-zh/reference/pcre/pattern.modifiers.xml?r=1.1&p=1 Index: phpdoc-zh/reference/pcre/pattern.modifiers.xml +++ phpdoc-zh/reference/pcre/pattern.modifiers.xml <?xml version="1.0" encoding="gb2312"?> <!-- $Revision: 1.1 $ --> <!-- $Author: yincheng $ --> <!-- EN-Revision: 1.5 Maintainer: dallas Status: ready --> <refentry id="reference.pcre.pattern.modifiers"> <refnamediv> <refname>ģʽÐÞÕý·û</refname> <refpurpose>½â˵ÕýÔò±í´ïʽģʽÖÐʹÓõÄÐÞÕý·û</refpurpose> </refnamediv> <refsect1> <title>˵Ã÷</title> <para> ÏÂÃæÁгöÁ˵±Ç°ÔÚ PCRE ÖпÉÄÜʹÓõÄÐÞÕý·û¡£À¨ºÅÖÐÊÇÕâЩÐÞÕý·ûµÄÄÚ²¿ PCRE Ãû¡£ </para> <para> <blockquote> <variablelist> <varlistentry> <term><emphasis>i</emphasis> (PCRE_CASELESS)</term> <listitem> <simpara> Èç¹ûÉ趨´ËÐÞÕý·û£¬Ä£Ê½ÖеÄ×Ö·û½«Í¬Ê±Æ¥Åä´óСд×Öĸ¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>m</emphasis> (PCRE_MULTILINE)</term> <listitem> <simpara> ĬÈÏÇé¿öÏ£¬PCRE ½«Ä¿±ê×Ö·û´®×÷Ϊµ¥Ò»µÄÒ»¡°ÐС±×Ö·ûËù×é³ÉµÄ£¨ÉõÖÁÆäÖаüº¬Óл»ÐзûÒ²ÊÇÈç´Ë£©¡£¡°ÐÐÆðʼ¡±Ôª×Ö·û£¨^£©½ö½öÆ¥Åä×Ö·û´®µÄÆðʼ£¬¡°ÐнáÊø¡±Ôª×Ö·û£¨$£©½ö½öÆ¥Åä×Ö·û´®µÄ½áÊø£¬»òÕß×îºóÒ»¸ö×Ö·ûÊÇ»»ÐзûʱÆäÇ°Ãæ£¨³ý·ÇÉ趨ÁË <emphasis>D</emphasis> ÐÞÕý·û£©¡£ÕâºÍ Perl ÊÇÒ»ÑùµÄ¡£ </simpara> <simpara> µ±É趨ÁË´ËÐÞÕý·û£¬¡°ÐÐÆðʼ¡±ºÍ¡°ÐнáÊø¡±³ýÁËÆ¥ÅäÕû¸ö×Ö·û´®¿ªÍ·ºÍ½áÊøÍ⣬»¹·Ö±ðÆ¥ÅäÆäÖеĻ»ÐзûµÄÖ®ºóºÍ֮ǰ¡£ÕâºÍ Perl µÄ /m ÐÞÕý·ûÊǵÈЧµÄ¡£Èç¹ûÄ¿±ê×Ö·û´®ÖÐûÓС°\n¡±×Ö·û»òÕßģʽÖÐûÓÐ ^ »ò $£¬ÔòÉ趨´ËÐÞÕý·ûûÓÐÈκÎЧ¹û¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>s</emphasis> (PCRE_DOTALL)</term> <listitem> <simpara> Èç¹ûÉ趨ÁË´ËÐÞÕý·û£¬Ä£Ê½ÖеÄÔ²µãÔª×Ö·û£¨.£©Æ¥ÅäËùÓеÄ×Ö·û£¬°üÀ¨»»Ðзû¡£Ã»ÓдËÉ趨µÄ»°£¬Ôò²»°üÀ¨»»Ðзû¡£ÕâºÍ Perl µÄ /s ÐÞÕý·ûÊǵÈЧµÄ¡£Åųý×Ö·ûÀàÀýÈç [^a] ×ÜÊÇÆ¥Åä»»ÐзûµÄ£¬ÎÞÂÛÊÇ·ñÉ趨ÁË´ËÐÞÕý·û¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>x</emphasis> (PCRE_EXTENDED)</term> <listitem> <simpara> Èç¹ûÉ趨ÁË´ËÐÞÕý·û£¬Ä£Ê½ÖеĿհ××Ö·û³ýÁ˱»×ªÒåµÄ»òÔÚ×Ö·ûÀàÖеÄÒÔÍâÍêÈ«±»ºöÂÔ£¬ÔÚδתÒåµÄ×Ö·ûÀàÖ®ÍâµÄ # ÒÔ¼°ÏÂÒ»¸ö»»ÐзûÖ®¼äµÄËùÓÐ×Ö·û£¬°üÀ¨Á½Í·£¬Ò²¶¼±»ºöÂÔ¡£ÕâºÍ Perl µÄ /x ÐÞÕý·ûÊǵÈЧµÄ£¬Ê¹µÃ¿ÉÒÔÔÚ¸´ÔÓµÄģʽÖмÓÈë×¢ÊÍ¡£È»¶ø×¢Ò⣬Õâ½öÊÊÓÃÓÚÊý¾Ý×Ö·û¡£¿Õ°××Ö·û¿ÉÄÜÓÀÔ¶²»»á³öÏÖÓÚģʽÖеÄÌØÊâ×Ö·ûÐòÁУ¬ÀýÈçÒýÈëÌõ¼þ×ÓģʽµÄÐòÁÐ (?( Öм䡣 </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>e</emphasis></term> <listitem> <simpara> Èç¹ûÉ趨ÁË´ËÐÞÕý·û£¬<function>preg_replace</function> ÔÚÌæ»»×Ö·û´®ÖжÔÄæÏòÒýÓÃ×÷Õý³£µÄÌæ»»£¬½«Æä×÷Ϊ PHP ´úÂëÇóÖµ£¬²¢ÓÃÆä½á¹ûÀ´Ìæ»»ËùËÑË÷µÄ×Ö·û´®¡£ </simpara> <para> Ö»ÓÐ <function>preg_replace</function> ʹÓôËÐÞÕý·û£¬ÆäËü PCRE º¯Êý½«ºöÂÔÖ®¡£ <note> <simpara> ±¾ÐÞÕý·ûÔÚ PHP3 Öв»¿ÉÓᣠ</simpara> </note> </para> </listitem> </varlistentry> <varlistentry> <term><emphasis>A</emphasis> (PCRE_ANCHORED)</term> <listitem> <simpara> Èç¹ûÉ趨ÁË´ËÐÞÕý·û£¬Ä£Ê½±»Ç¿ÖÆÎª¡°anchored¡±£¬¼´Ç¿Öƽö´ÓÄ¿±ê×Ö·û´®µÄ¿ªÍ·¿ªÊ¼Æ¥Åä¡£´ËЧ¹ûÒ²¿ÉÒÔͨ¹ýÊʵ±µÄģʽ±¾ÉíÀ´ÊµÏÖ£¨ÔÚ Perl ÖÐʵÏÖµÄΨһ·½·¨£©¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>D</emphasis> (PCRE_DOLLAR_ENDONLY)</term> <listitem> <simpara> Èç¹ûÉ趨ÁË´ËÐÞÕý·û£¬Ä£Ê½ÖеÄÃÀÔªÔª×Ö·û½öÆ¥ÅäÄ¿±ê×Ö·û´®µÄ½áβ¡£Ã»ÓдËÑ¡Ïîʱ£¬Èç¹û×îºóÒ»¸ö×Ö·ûÊÇ»»ÐзûµÄ»°£¬ÃÀÔª·ûºÅÒ²»áÆ¥Åä´Ë×Ö·û֮ǰ£¨µ«²»»áÆ¥ÅäÈÎºÎÆäËü»»Ðзû֮ǰ£©¡£Èç¹ûÉ趨ÁË <emphasis>m</emphasis> ÐÞÕý·ûÔòºöÂÔ´ËÑ¡Ïî¡£Perl ÖÐûÓÐÓëÆäµÈ¼ÛµÄÐÞÕý·û¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>S</emphasis></term> <listitem> <simpara> µ±Ò»¸öģʽ½«±»Ê¹ÓÃÈô¸É´Îʱ£¬Îª¼ÓËÙÆ¥ÅäÆð¼ûÖµµÃÏÈ¶ÔÆä½øÐзÖÎö¡£Èç¹ûÉ趨ÁË´ËÐÞÕý·ûÔò»á½øÐжîÍâµÄ·ÖÎö¡£Ä¿Ç°£¬·ÖÎöÒ»¸öģʽ½ö¶ÔûÓе¥Ò»¹Ì¶¨Æðʼ×Ö·ûµÄ non-anchored ģʽÓÐÓᣠ</simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>U</emphasis> (PCRE_UNGREEDY)</term> <listitem> <simpara> ±¾ÐÞÕý·û·´×ªÁËÆ¥ÅäÊýÁ¿µÄֵʹÆä²»ÊÇĬÈϵÄÖØ¸´£¬¶ø±ä³ÉÔÚºóÃæ¸úÉÏ¡°?¡±²Å±äµÃÖØ¸´¡£ÕâºÍ Perl ²»¼æÈÝ¡£Ò²¿ÉÒÔͨ¹ýÔÚģʽ֮ÖÐÉ趨 (?U) ÐÞÕý·ûÀ´ÆôÓôËÑ¡Ïî¡£ <!-- This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern. --> </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>X</emphasis> (PCRE_EXTRA)</term> <listitem> <simpara> ´ËÐÞÕý·ûÆôÓÃÁËÒ»¸ö PCRE ÖÐÓë Perl ²»¼æÈݵĶîÍ⹦ÄÜ¡£Ä£Ê½ÖеÄÈκη´Ð±ÏߺóÃæ¸úÉÏÒ»¸öûÓÐÌØÊâÒâÒåµÄ×Öĸµ¼ÖÂÒ»¸ö´íÎ󣬴Ӷø±£Áô´Ë×éºÏÒÔ±¸½«À´À©³ä¡£Ä¬ÈÏÇé¿öÏ£¬ºÍ Perl Ò»Ñù£¬Ò»¸ö·´Ð±ÏߺóÃæ¸úÒ»¸öûÓÐÌØÊâÒâÒåµÄ×Öĸ±»µ±³É¸Ã×Öĸ±¾Éí¡£µ±Ç°Ã»ÓÐÆäËüÌØÐÔÊÜ´ËÐÞÕý·û¿ØÖÆ¡£ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>u</emphasis> (PCRE_UTF8)</term> <listitem> <simpara> ´ËÐÞÕý·ûÆôÓÃÁËÒ»¸ö PCRE ÖÐÓë Perl ²»¼æÈݵĶîÍ⹦ÄÜ¡£Ä£Ê½×Ö·û´®±»µ±³É UTF-8¡£±¾ÐÞÕý·ûÔÚ Unix ÏÂ×Ô PHP 4.1.0 Æð¿ÉÓã¬ÔÚ win32 ÏÂ×Ô PHP 4.2.3 Æð¿ÉÓᣠ</simpara> </listitem> </varlistentry> </variablelist> </blockquote> </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 --> http://cvs.php.net/co.php/phpdoc-zh/reference/pcre/pattern.syntax.xml?r=1.1&p=1 Index: phpdoc-zh/reference/pcre/pattern.syntax.xml +++ phpdoc-zh/reference/pcre/pattern.syntax.xml <?xml version="1.0" encoding="gb2312"?> <!-- $Revision: 1.1 $ --> <!-- $Author: yincheng $ --> <!-- EN-Revision: 1.5 Maintainer: dallas Status: partial --> <refentry id="reference.pcre.pattern.syntax"> <refnamediv> <refname>ģʽÓï·¨</refname> <refpurpose>½â˵ Perl ¼æÈÝÕýÔò±í´ïʽµÄÓï·¨</refpurpose> </refnamediv> <refsect1> <title>˵Ã÷</title> <simpara> PCRE ¿âÊÇÒ»×éÓÃºÍ Perl 5 ÏàͬµÄÓï·¨ºÍÓïÒåʵÏÖÁËÕýÔò±í´ïʽģʽƥÅäµÄº¯Êý£¬²»¹ýÓÐÉÙÐíÇø±ð£¨¼ûÏÂÃæ£©¡£µ±Ç° PCRE µÄʵÏÖÊÇÓë Perl 5.005 Ïà·ûµÄ¡£ </simpara> </refsect1> <refsect1> <title>Óë Perl µÄÇø±ð</title> <para> ÕâÀï̸µ½µÄÇø±ðÊÇ¾Í Perl 5.005 À´ËµµÄ¡£ <orderedlist> <listitem> <simpara> ĬÈÏÇé¿öÏ£¬¿Õ°××Ö·ûÊÇ C ÓïÑԿ⺯Êý isspace() ËùÄÜʶ±ðµÄÈκÎ×Ö·û£¬¾¡¹ÜÓпÉÄÜÓë±ðµÄ×Ö·ûÀàÐͱí±àÒëÔÚÒ»Æð¡£Í¨³£ isspace() Æ¥Åä¿Õ¸ñ£¬»»Ò³·û£¬»»Ðзû£¬»Ø³µ·û£¬Ë®Æ½ÖƱí·ûºÍ´¹Ö±ÖƱí·û¡£Perl 5 ²»ÔÙ½«´¹Ö±ÖƱí·û°üÀ¨ÔÚ¿Õ°××Ö·ûÖÐÁË¡£ÊÂʵÉϳ¤¾ÃÒÔÀ´´æÔÚÓÚ Perl ÎĵµÖеÄתÒåÐòÁÐ \v ´Óδ±»Ê¶±ð¹ý£¬²»¹ý¸Ã×Ö·ûÖÁÉÙµ½ 5.002 Ϊֹ¶¼±»µ±³É¿Õ°××Ö·ûµÄ¡£ÔÚ 5.004 ºÍ 5.005 ÖÐ \s ²»Æ¥Åä´Ë×Ö·û¡£ </simpara> </listitem> <listitem> <simpara> PCRE ²»ÔÊÐíÔÚÏòǰ¶ÏÑÔÖÐʹÓÃÖØ¸´µÄÊýÁ¿·û¡£Perl ÔÊÐíÕâÑù£¬µ«¿ÉÄܲ»ÊÇÄãÏëÏóÖеĺ¬Òå¡£ÀýÈ磬(?!a){3} ²¢²»ÊǶÏÑÔÏÂÃæÈý¸ö×Ö·û²»ÊÇ¡°a¡±£¬¶øÊǶÏÑÔÏÂÒ»¸ö×Ö·û²»ÊÇ¡°a¡±Èý´Î¡£ </simpara> </listitem> <listitem> <simpara> ²¶»ñ³öÏÖÔÚÅųýģʽ¶ÏÑÔÖеÄ×ÓģʽËäÈ»±»¼ÆÊý£¬µ«²¢Î´ÔÚÆ«ÒÆÏòÁ¿ÖÐÉ趨ÆäÌõÄ¿¡£Perl ÔÚÆ¥Åäʧ°Üǰ´Ó´ËÖÖģʽÖÐÉ趨ÆäÊý×Ö±äÁ¿£¬µ«Ö»ÔÚÅÅ´¥Ãþʽ¶ÏÑÔÖ»°üº¬Ò»¸ö·Ö֧ʱ¡£ <!-- ·Ñ½â£¡ Capturing subpatterns that occur inside negative looka- head assertions are counted, but their entries in the offsets vector are never set. Perl sets its numerical variables from any such patterns that are matched before the assertion fails to match something (thereby succeeding), but only if the negative look ahead assertion contains just one branch. --> </simpara> </listitem> <listitem> <simpara> ¾¡¹ÜÄ¿±ê×Ö·û´®ÖÐÖ§³Ö¶þ½øÖƵÄÁã×Ö·û£¬µ«²»ÄܳöÏÖÔÚģʽ×Ö·û´®ÖУ¬ÒòΪËü±»µ±×÷ÆÕͨµÄ C ×Ö·û´®´«µÝ£¬ÒÔ¶þ½øÖÆÁãÖÕÖ¹¡£×ªÒåÐòÁС°\\x00¡±¿ÉÒÔÔÚģʽÖÐÓÃÀ´±íʾ¶þ½øÖÆÁã¡£ </simpara> </listitem> <listitem> <simpara> ²»Ö§³ÖÏÂÁÐ Perl תÒåÐòÁУº\l£¬\u£¬\L£¬\U£¬\E£¬\Q¡£ÊÂʵÉÏÕâЩÊÇÓÉ Perl µÄ×Ö·û´®´¦ÀíÀ´ÊµÏֵ쬲¢²»ÊÇģʽƥÅäÒýÇæµÄÒ»²¿·Ö¡£ </simpara> </listitem> <listitem> <simpara> ²»Ö§³Ö Perl µÄ \G ¶ÏÑÔ£¬ÒòΪÕâºÍµ¥¸öµÄģʽƥÅäÎ޹ء£ </simpara> </listitem> <listitem> <simpara> ºÜÃ÷ÏÔ£¬PCRE ²»Ö§³Ö (?{code}) ½á¹¹¡£ </simpara> </listitem> <listitem> <simpara> µ±²¿·ÖÄ£Ê½ÖØ¸´µÄʱºò£¬ÓÐ¹Ø Perl 5.005_02 ²¶»ñ×Ö·û´®µÄÉ趨ÓÐЩ¹Å¹ÖµÄµØ·½¡£¾ÙÀý˵£¬ÓÃģʽ /^(a(b)?)+$/ ȥƥÅä "aba" »á½« $2 ÉèΪ "b"£¬µ«ÊÇÓÃģʽ /^(aa(bb)?)+$/ ȥƥÅä "aabbaa" »áʹ $2 ÎÞÖµ¡£È»¶ø£¬Èç¹û°Ñģʽ¸Ä³É /^(aa(b(b))?)+$/£¬Ôò $2£¨ºÍ $3£©¾ÍÓÐÖµÁË¡£ÔÚ Perl 5.004 ÖÐÒÔÉÏÁ½ÖÖÇé¿öÏ $2 ¶¼»á±»¸³Öµ£¬ÔÚ PCRE ÖÐÒ²ÊÇ &true;¡£Èç¹ûÒÔºó Perl ¸ÄÁË£¬PCRE ¿ÉÄÜÒ²»á¸úןġ£ </simpara> </listitem> <listitem> <simpara> ÁíÒ»¸öδ½â¾öµÄì¶ÜÊÇ Perl 5.005_02 ÖÐģʽ /^(a)?(?(1)a|b)+$/ ÄÜÆ¥ÅäÉÏ×Ö·û´® "a"£¬µ«ÊÇ PCRE ²»»á¡£È»¶ø£¬ÔÚ Perl ºÍ PCRE ÖÐÓà /^(a)?a/ ȥƥÅä "a" »áʹ $1 ûÓÐÖµ¡£ </simpara> </listitem> <listitem> <para> PCRE ÌṩÁËһЩ¶Ô Perl ÕýÔò±í´ïʽ»úÖÆµÄÀ©Õ¹£º <orderedlist> <listitem> <simpara> ¾¡¹ÜÏòºó¶ÏÑÔ±ØÐëÆ¥Åä¹Ì¶¨³¤¶È×Ö·û´®£¬µ«Ã¿¸öÏòºó¶ÏÑԵķÖÖ§¿ÉÒÔÆ¥Å䲻ͬ³¤¶ÈµÄ×Ö·û´®¡£Perl 5.005 ÒªÇóËùÓзÖÖ§µÄ³¤¶ÈÏàͬ¡£ </simpara> </listitem> <listitem> <simpara> Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_DOLLAR_ENDONLY</link> ¶øÃ»ÓÐÉ趨 <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link>£¬Ôò $ Ôª×Ö·ûֻƥÅä×Ö·û´®µÄ×îĩβ¡£ </simpara> </listitem> <listitem> <simpara> Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_EXTRA</link>£¬·´Ð±ÏߺóÃæ¸úÒ»¸öûÓÐÌØÊ⺬ÒåµÄ×Öĸ»á³ö´í¡£ </simpara> </listitem> <listitem> <simpara> Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_UNGREEDY</link>£¬ÔòÖØ¸´µÄÊýÁ¿·ûµÄ greed ±»·´×ª£¬¼´£¬Ä¬ÈÏʱ²»ÊÇ greedy£¬µ«Èç¹ûºóÃæ¸úÉÏÒ»¸öÎʺžͱä³É greedy ÁË¡£ </simpara> </listitem> </orderedlist> </para> </listitem> </orderedlist> </para> </refsect1> <refsect1 id="regexp.reference"> <title>ÕýÔò±í´ïʽÏê½â</title> <refsect2 id="regexp.introduction"> <title>½éÉÜ</title> <para> ÏÂÃæËµÃ÷ PCRE ËùÖ§³ÖµÄÕýÔò±í´ïʽµÄÓï·¨ºÍÓïÒå¡£Perl ÎĵµºÍºÜ¶àÆäËüÊéÖÐÒ²½â˵ÁËÕýÔò±í´ïʽ£¬ÓеÄÊéÖÐÓкܶàÀý×Ó¡£Jeffrey Friedl дµÄ¡°Mastering Regular Expressions¡±£¬ÓÉ O'Reilly ³ö°æÉç·¢ÐУ¨ISBN 1-56592-257-3£©£¬°üº¬ÁË´óÁ¿Ï¸½Ú¡£ÕâÀïµÄ˵Ã÷Ö»ÊǸö²Î¿¼Îĵµ¡£ </para> <para> ÕýÔò±í´ïʽÊÇ´Ó×óÏòÓÒȥƥÅäÄ¿±ê×Ö·û´®µÄÒ»×éģʽ¡£´ó¶àÊý×Ö·ûÔÚģʽÖбíʾËüÃÇ×ÔÉí²¢Æ¥ÅäÄ¿±êÖÐÏàÓ¦µÄ×Ö·û¡£×÷Ϊһ¸öСÀý×Ó£¬Ä£Ê½ <literal>The quick brown fox</literal> Æ¥ÅäÁËÄ¿±ê×Ö·û´®ÖÐÓëÆäÍêÈ«ÏàͬµÄÒ»²¿·Ö¡£ </para> </refsect2> <refsect2 id="regexp.reference.meta"> <title>Ôª×Ö·û</title> <para> ÕýÔò±í´ïʽµÄÍþÁ¦ÔÚÓÚÆäÄܹ»ÔÚģʽÖаüº¬Ñ¡ÔñºÍÑ­»·¡£ËüÃÇͨ¹ýʹÓÃ<emphasis>Ôª×Ö·û</emphasis>À´±àÂëÔÚģʽÖУ¬Ôª×Ö·û²»´ú±íÆä×ÔÉí£¬ËüÃÇÓÃÒ»Ð©ÌØÊâµÄ·½Ê½À´½âÎö¡£ </para> <para> ÓÐÁ½×鲻ͬµÄÔª×Ö·û£ºÒ»ÖÖÊÇģʽÖгýÁË·½À¨ºÅÄÚ¶¼Äܱ»Ê¶±ðµÄ£¬»¹ÓÐÒ»ÖÖÊÇÔÚ·½À¨ºÅÄÚ±»Ê¶±ðµÄ¡£·½À¨ºÅÖ®ÍâµÄÔª×Ö·ûÓÐÕâЩ£º <variablelist> <varlistentry> <term><emphasis>\</emphasis></term> <listitem> <simpara> ÓÐÊýÖÖÓÃ;µÄͨÓÃתÒå·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>^</emphasis></term> <listitem> <simpara> ¶ÏÑÔÄ¿±êµÄ¿ªÍ·£¨»òÔÚ¶àÐÐģʽÏÂÐеĿªÍ·£¬¼´½ôËæÒ»»»ÐзûÖ®ºó£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>$</emphasis></term> <listitem> <simpara> ¶ÏÑÔÄ¿±êµÄ½á⣨»òÔÚ¶àÐÐģʽÏÂÐеĽá⣬¼´½ôËæÒ»»»Ðзû֮ǰ£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>.</emphasis></term> <listitem> <simpara> Æ¥Åä³ýÁË»»ÐзûÍâµÄÈÎÒâÒ»¸ö×Ö·û£¨Ä¬ÈÏÇé¿öÏ£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>[</emphasis></term> <listitem> <simpara> ×Ö·ûÀඨÒ忪ʼ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>]</emphasis></term> <listitem> <simpara> ×Ö·ûÀඨÒå½áÊø </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>|</emphasis></term> <listitem> <simpara> ¿ªÊ¼Ò»¸ö¶àѡһµÄ·ÖÖ§ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>(</emphasis></term> <listitem> <simpara> ×Óģʽ¿ªÊ¼ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>)</emphasis></term> <listitem> <simpara> ×Óģʽ½áÊø </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>?</emphasis></term> <listitem> <simpara> À©Õ¹ ( µÄº¬Ò壬ҲÊÇ 0 »ò 1 ÊýÁ¿ÏÞ¶¨·û£¬ÒÔ¼°ÊýÁ¿ÏÞ¶¨·û×îСֵ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>*</emphasis></term> <listitem> <simpara> Æ¥Åä 0 ¸ö»ò¶à¸öµÄÊýÁ¿ÏÞ¶¨·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>+</emphasis></term> <listitem> <simpara> Æ¥Åä 1 ¸ö»ò¶à¸öµÄÊýÁ¿ÏÞ¶¨·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>{</emphasis></term> <listitem> <simpara> ×îÉÙ£¯×î¶àÊýÁ¿ÏÞ¶¨¿ªÊ¼ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>}</emphasis></term> <listitem> <simpara> ×îÉÙ£¯×î¶àÊýÁ¿ÏÞ¶¨½áÊø </simpara> </listitem> </varlistentry> </variablelist> ģʽÖз½À¨ºÅÄڵIJ¿·Ö³ÆÎª¡°×Ö·ûÀࡱ¡£×Ö·ûÀàÖпÉÓõÄÔª×Ö·ûΪ£º <variablelist> <varlistentry> <term><emphasis>\</emphasis></term> <listitem> <simpara> ͨÓÃתÒå×Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>^</emphasis></term> <listitem> <simpara> Åųý×Ö·ûÀ࣬µ«½öµ±ÆäΪµÚÒ»¸ö×Ö·ûʱÓÐЧ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>-</emphasis></term> <listitem> <simpara> Ö¸³ö×Ö·û·¶Î§ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>]</emphasis></term> <listitem> <simpara> ½áÊø×Ö·ûÀà </simpara> </listitem> </varlistentry> </variablelist> ÒÔÏÂ˵Ã÷ÁËÿһ¸öÔª×Ö·ûµÄÓ÷¨¡£ </para> </refsect2> <refsect2 id="regexp.reference.backslash"> <title>·´Ð±Ïߣ¨\£©</title> <para> ·´Ð±Ïß×Ö·ûÓм¸ÖÖÓÃ;¡£Ê×ÏÈ£¬Èç¹ûÆäºó¸ú×ÅÒ»¸ö·Ç×ÖĸÊý×Ö×Ö·û£¬ÔòÈ¡Ïû¸Ã×Ö·û¿ÉÄܾßÓеÄÈκÎÌØÊ⺬Òå¡£´ËÖÖ½«·´Ð±ÏßÓÃ×÷תÒå×Ö·ûµÄÓ÷¨ÊÊÓÃÓÚÎÞÂÛÊÇ×Ö·ûÀàÖ®Öл¹ÊÇÖ®Íâ¡£ </para> <para> ÀýÈ磬Èç¹ûÏëÆ¥ÅäÒ»¸ö¡°*¡±×Ö·û£¬ÔòÔÚģʽÖÐÓá°\*¡±¡£ÕâÊÊÓÃÓÚÎÞÂÛÏÂÒ»¸ö×Ö·ûÊÇ·ñ»á±»µ±×÷Ôª×Ö·ûÀ´½âÊÍ£¬Òò´ËÔÚ·Ç×ÖĸÊý×Ö×Ö·û֮ǰ¼ÓÉÏÒ»¸ö¡°\¡±À´Ö¸Ã÷¸Ã×Ö·û¾Í´ú±íÆä±¾Éí×ÜÊǰ²È«µÄ¡£ÓÈÆäÊÇ£¬Èç¹ûҪƥÅäÒ»¸ö·´Ð±Ïߣ¬Óá°\\¡±¡£ </para> <para> Èç¹ûģʽ±àÒëʱ¼ÓÉÏÁË <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> Ñ¡ÏģʽÖеĿհ××Ö·û£¨×Ö·ûÀàÖÐÒÔÍâµÄ£©ÒÔ¼°×Ö·ûÀàÖ®ÍâµÄ¡°#¡±µ½»»ÐзûÖ®¼äµÄ×Ö·û¶¼±»ºöÂÔ¡£¿ÉÒÔÓÃתÒåµÄ·´Ð±Ïß½«¿Õ°××Ö·û»òÕß¡°#¡±×Ö·û°üÀ¨µ½Ä£Ê½ÖÐÈ¥¡£ </para> <para> ·´Ð±ÏߵĵڶþÖÖÓÃ;ÌṩÁËÒ»ÖÖÔÚģʽÖÐÒԿɼû·½Ê½È¥±àÂë²»¿É´òÓ¡×Ö·ûµÄ·½·¨¡£²¢Ã»Óв»¿É´òÓ¡×Ö·û³öÏÖµÄÏÞÖÆ£¬³ýÁË´ú±íģʽ½áÊøµÄ¶þ½øÖÆÁãÒÔÍâ¡£µ«ÓÃÎı¾±à¼­Æ÷À´×¼±¸Ä£Ê½µÄʱºò£¬Í¨³£ÓÃÒÔϵÄתÒåÐòÁÐÀ´±íʾÄÇЩ¶þ½øÖÆ×Ö·û¸üÈÝÒ×һЩ£º </para> <para> <variablelist> <varlistentry> <term><emphasis>\a</emphasis></term> <listitem> <simpara> alarm£¬¼´ BEL ×Ö·û£¨0x07£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\cx</emphasis></term> <listitem> <simpara> "control-x"£¬ÆäÖÐ x ÊÇÈÎÒâ×Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\e</emphasis></term> <listitem> <simpara> escape£¨0x1B£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\f</emphasis></term> <listitem> <simpara> »»Ò³·û formfeed£¨0x0C£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\n</emphasis></term> <listitem> <simpara> »»Ðзû newline£¨0x0A£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\r</emphasis></term> <listitem> <simpara> »Ø³µ·û carriage return£¨0x0D£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\t</emphasis></term> <listitem> <simpara> ÖÆ±í·û tab£¨0x09£© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\xhh</emphasis></term> <listitem> <simpara> Ê®Áù½øÖÆ´úÂëΪ hh µÄ×Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\ddd</emphasis></term> <listitem> <simpara> °Ë½øÖÆ´úÂëΪ ddd µÄ×Ö·û£¬»ò backreference </simpara> </listitem> </varlistentry> </variablelist> </para> <para> ¡°<literal>\cx</literal>¡±µÄ¾«È·Ð§¹ûÈçÏ£ºÈç¹û¡°<literal>x</literal>¡±ÊÇСд×Öĸ£¬Ôò±»×ª»»Îª´óд×Öĸ¡£½Ó×Å×Ö·ûÖÐµÄµÚ 6 루0x40£©±»·´×ª¡£´Ó¶ø¡°<literal>\cz</literal>¡±³ÉΪ 0x1A£¬µ«¡°<literal>\c{</literal>¡±³ÉΪ 0x3B£¬¶ø¡°<literal>\c;</literal>¡±³ÉΪ 0x7B¡£ </para> <para> ÔÚ¡°<literal>\x</literal>¡±Ö®ºó×î¶àÔÙ¶ÁÈ¡Á½¸öÊ®Áù½øÖÆÊý×Ö£¨ÆäÖеÄ×Öĸ¿ÉÒÔÊÇ´óд»òСд£©¡£ </para> <para> ÔÚ¡°<literal>\0</literal>¡±Ö®ºó×î¶àÔÙ¶ÁÈ¡Á½¸ö°Ë½øÖÆÊý×Ö¡£ÒÔÉÏÁ½ÖÖÇé¿öÏ£¬Èç¹ûÉÙÓÚÁ½¸öÊý×Ö£¬ÔòֻʹÓÃÒѳöÏֵġ£Òò´ËÐòÁС°<literal>\0\x\07</literal>¡±´ú±íÁ½¸ö¶þ½øÖƵÄÁã¼ÓÒ»¸ö BEL ×Ö·û¡£Èç¹ûÊǰ˽øÖÆÊý×ÖÔòÈ·±£ÔÚ¿ªÊ¼µÄÁãºóÃæÔÙÌṩÁ½¸öÊý×Ö¡£ </para> <para> ´¦Àí·´Ð±ÏߺóÃæ¸ú×ÅÒ»¸ö²»ÊÇ 0 µÄÊý×ֱȽϸ´ÔÓ¡£ÔÚ×Ö·ûÀàÖ®Í⣬PCRE ÒÔÊ®½øÖÆÊý×Ö¶ÁÈ¡¸ÃÊý×Ö¼°ÆäºóÃæµÄÊý×Ö¡£Èç¹ûÊý×ÖСÓÚ 10£¬»òÕß֮ǰ±í´ïʽÖв¶»ñµ½ÖÁÉÙ¸ÃÊý×ÖµÄ×óÔ²À¨ºÅ£¬ÔòÕâ¸öÐòÁн«±»×÷Ϊ<emphasis>ÄæÏòÒýÓÃ</emphasis>¡£ÓйشËÈçºÎÔË×÷µÄ˵Ã÷ÔÚºóÃæ£¬ÒÔ¼°À¨ºÅÄÚµÄ×Óģʽ¡£ </para> <para> ÔÚ×Ö·ûÀàÖ®ÖУ¬»òÕßÈç¹ûÊ®½øÖÆÊý×Ö´óÓÚ 9 ²¢ÇÒ֮ǰûÓÐÄÇô¶à²¶»ñµÄ×Óģʽ£¬PCRE ÖØÐ´ӷ´Ð±Ïß¿ªÊ¼¶ÁÈ¡ÆäºóµÄ×î¶àÈý¸ö°Ë½øÖÆÊý×Ö£¬²¢ÒÔ×îµÍλµÄ 8 ¸ö±ÈÌØ²úÉú³öÒ»¸öµ¥Ò»×Ö½Ú¡£ÈÎºÎÆäºóµÄÊý×Ö¶¼´ú±í×ÔÉí¡£ÀýÈ磺 </para> <para> <variablelist> <varlistentry> <term><emphasis>\040</emphasis></term> <listitem> <simpara> ÁíÒ»ÖÖ±íʾ¿Õ¸ñµÄ·½·¨ </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\40</emphasis></term> <listitem> <simpara> ͬÉÏ£¬Èç¹û֮ǰ²¶»ñµÄ×ÓģʽÉÙÓÚ 40 ¸öµÄ»° </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\7</emphasis></term> <listitem> <simpara> ×ÜÊÇÒ»¸öÄæÏòÒýÓà </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\11</emphasis></term> <listitem> <simpara> ¿ÉÄÜÊǸöÄæÏòÒýÓ㬻òÕßÊÇÖÆ±í·û tab </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\011</emphasis></term> <listitem> <simpara> ×ÜÊDZíÊ¾ÖÆ±í·û tab </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\0113</emphasis></term> <listitem> <simpara> ±íÊ¾ÖÆ±í·û tab ºóÃæ¸ú×ÅÒ»¸ö×Ö·û¡°3¡± </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\113</emphasis></term> <listitem> <simpara> ±íʾ°Ë½øÖÆ´úÂëΪ 113 µÄ×Ö·û£¨ÒòΪ²»Äܳ¬¹ý 99 ¸öÄæÏòÒýÓã© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\377</emphasis></term> <listitem> <simpara> ±íʾһ¸öËùÓеıÈÌØ¶¼ÊÇ 1 µÄ×Ö½Ú </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\81</emphasis></term> <listitem> <simpara> ҪôÊÇÒ»¸öÄæÏòÒýÓã¬ÒªÃ´ÊÇÒ»¸ö¶þ½øÖƵÄÁãºóÃæ¸ú×ÅÁ½¸ö×Ö·û¡°8¡±ºÍ¡°1¡± </simpara> </listitem> </varlistentry> </variablelist> </para> <para> ×¢Òâ°Ë½øÖÆÖµ 100 »ò¸ü´óµÄֵ֮ǰ²»ÄÜÒÔÁã´òÍ·£¬ÒòΪ²»»á¶ÁÈ¡£¨·´Ð±Ïߺ󣩳¬¹ýÈý¸ö°Ë½øÖÆÊý×Ö¡£ </para> <para> ËùÓе͍ÒåÁËÒ»¸öµ¥Ò»×Ö½ÚµÄÐòÁпÉÒÔÓÃÓÚ×Ö·ûÀàÖ®ÖлòÖ®Íâ¡£´ËÍ⣬ÔÚ×Ö·ûÀàÖ®ÖУ¬ÐòÁС°<literal>\b</literal>¡±±»½âÊÍΪ·´Ð±Ïß×Ö·û£¨0x08£©£¬¶øÔÚ×Ö·ûÀàÖ®ÍâÓв»Í¬º¬Ò壨¼ûÏÂÃæ£©¡£ </para> <para> ·´Ð±ÏߵĵÚÈý¸öÓ÷¨ÊÇÖ¸¶¨Í¨ÓÃ×Ö·ûÀàÐÍ£º </para> <para> <variablelist> <varlistentry> <term><emphasis>\d</emphasis></term> <listitem> <simpara> ÈÎһʮ½øÖÆÊý×Ö </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\D</emphasis></term> <listitem> <simpara> ÈÎÒ»·ÇÊ®½øÖÆÊýµÄ×Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\s</emphasis></term> <listitem> <simpara> ÈÎÒ»¿Õ°××Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\S</emphasis></term> <listitem> <simpara> ÈÎÒ»·Ç¿Õ°××Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\w</emphasis></term> <listitem> <simpara> ÈÎÒ»¡°×Ö¡±µÄ×Ö·û </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\W</emphasis></term> <listitem> <simpara> ÈÎÒ»¡°·Ç×Ö¡±µÄ×Ö·û </simpara> </listitem> </varlistentry> </variablelist> </para> <para> ÈκÎÒ»¸öתÒåÐòÁн«ÍêÕûµÄ×Ö·û×éºÏ·Ö¸î³ÉÁ½¸ö·ÖÀëµÄ²¿·Ö¡£ÈÎÒ»¸ø¶¨µÄ×Ö·ûÆ¥ÅäÒ»¸öÇÒ½öÒ»¸öתÒåÐòÁС£ </para> <para> ¡°×Ö¡±µÄ×Ö·ûÊÇÖ¸ÈκÎÒ»¸ö×Öĸ»òÊý×Ö»òÏ»®Ïߣ¬Ò²¾ÍÊÇ˵£¬ÈκοÉÒÔÊÇ Perl "<literal>word</literal>" µÄ×Ö·û¡£×ÖĸºÍÊý×ֵ͍ÒåÓÉ PCRE ×Ö·û±í¿ØÖÆ£¬¿ÉÄÜ»á¸ù¾ÝÖ¸¶¨ÇøÓòµÄÆ¥Åä¶ø¸Ä±ä£¨¼ûÉÏÃæµÄ¡°ÇøÓòÖ§³Ö¡±£©¡£¾ÙÀý˵£¬ÔÚ "fr" (French) ÇøÓò£¬Ä³Ð©±àÂë´óÓÚ 128 µÄ×Ö·ûÓÃÀ´±íÊ¾ÖØÒô×Öĸ£¬ÕâЩ×Ö·ûÄܹ»±» <literal>\w</literal> ËùÆ¥Åä¡£ </para> <para> ÕâЩ×Ö·ûÀàÐÍÐòÁпÉÒÔ³öÏÖÔÚ×Ö·ûÀàÖ®ÖкÍÖ®Í⡣ÿһ¸öÆ¥ÅäÏàÓ¦ÀàÐÍÖеÄÒ»¸ö×Ö·û¡£Èç¹ûµ±Ç°Æ¥ÅäµãÔÚÄ¿±ê×Ö·û´®µÄ½á⣬ÒÔÉÏËùÓÐÆ¥Å䶼ʧ°Ü£¬ÒòΪûÓÐ×Ö·û¿É¹©Æ¥Åä¡£ </para> <para> ·´Ð±ÏߵĵÚËĸöÓ÷¨ÊÇijЩ¼òµ¥µÄ¶ÏÑÔ¡£¶ÏÑÔÊÇÖ¸ÔÚÒ»¸öÆ¥ÅäÖеÄÌØ¶¨Î»ÖñØÐë´ïµ½µÄÌõ¼þ£¬²¢²»»áÏûºÄÄ¿±ê×Ö·û´®ÖеÄÈκÎ×Ö·û¡£×ÓģʽÖиü¸´ÔӵĶÏÑÔµÄÓ÷¨ÔÚÏÂÃæÃèÊö¡£·´Ð±ÏߵĶÏÑÔÓУº </para> <para> <variablelist> <varlistentry> <term><emphasis>\b</emphasis></term> <listitem> <simpara> ×Ö·Ö½çÏß </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\B</emphasis></term> <listitem> <simpara> ·Ç×Ö·Ö½çÏß </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\A</emphasis></term> <listitem> <simpara> Ä¿±êµÄ¿ªÍ·£¨Óë¶àÐÐģʽÎ޹أ© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\Z</emphasis></term> <listitem> <simpara> Ä¿±êµÄ½áβ»òλÓÚ½áβµÄ»»Ðзûǰ£¨Óë¶àÐÐģʽÎ޹أ© </simpara> </listitem> </varlistentry> <varlistentry> <term><emphasis>\z</emphasis></term> <listitem> <simpara> Ä¿±êµÄ½á⣨Óë¶àÐÐģʽÎ޹أ© </simpara> </listitem> </varlistentry> </variablelist> </para> <para> ÕâЩ¶ÏÑÔ¿ÉÄܲ»ÄܳöÏÖÔÚ×Ö·ûÀàÖУ¨µ«ÊÇ×¢Òâ "<literal>\b</literal>" Óв»Í¬µÄº¬Ò壬ÔÚ×Ö·ûÀàÖ®ÖÐÒ²¾ÍÊÇ·´Ð±Ïß×Ö·û£©¡£ </para> <para> ×ֱ߽çÊÇÄ¿±ê×Ö·û´®ÖеÄÒ»¸öλÖã¬Æäµ±Ç°×Ö·ûºÍǰһ¸ö×Ö·û²»ÄÜͬʱƥÅä <literal>\w</literal> »òÕß <literal>\W</literal>£¨Ò²¾ÍÊÇÆäÖÐÒ»¸öÆ¥Åä <literal>\w</literal> ¶øÁíÒ»¸öÆ¥Åä <literal>\W</literal>£©£¬»òÕßÊÇ×Ö·û´®µÄ¿ªÍ·»ò½á⣬¼ÙÈçµÚÒ»¸ö»ò×îºóÒ»¸ö×Ö·ûÆ¥Åä \w µÄ»°¡£ </para> <para> <literal>\A</literal>£¬<literal>\Z</literal> ºÍ <literal>\z</literal> ¶ÏÑÔÓ봫ͳµÄÒôµ÷·ûºÍÃÀÔª·û£¨ÏÂÃæËµÃ÷£©µÄ²»Í¬Ö®´¦ÔÚÓÚËüÃǽöÆ¥ÅäÄ¿±ê×Ö·û´®µÄ¾ø¶Ô¿ªÍ·ºÍ½áβ¶ø²»¹ÜÉ趨ÁËÈκÎÑ¡Ïî¡£ËüÃDz»ÊÜ <link linkend="pcre.pattern.modifiers">PCRE_NOTBOL</link> »ò <link linkend="pcre.pattern.modifiers">PCRE_NOTEOL</link> Ñ¡ÏîµÄÓ°Ïì¡£<literal>\Z</literal> ºÍ <literal>\z</literal> µÄ²»Í¬Ö®´¦ÔÚÓÚ <literal>\Z</literal> Æ¥ÅäÁË×÷Ϊ×Ö·û´®×îºóÒ»¸ö×Ö·ûµÄ»»Ðзû֮ǰÒÔ¼°×Ö·û´®µÄ½á⣬¶ø <literal>\z</literal> ½öÆ¥Åä×Ö·û´®µÄ½áβ¡£ </para> </refsect2> <refsect2 id="regexp.reference.circudollar"> <title>Òôµ÷·û£¨^£©ºÍÃÀÔª·û£¨$£©</title> <para> ÔÚ×Ö·ûÀàÖ®Í⣬ĬÈÏÆ¥ÅäģʽÏ£¬Òôµ÷·ûÊÇÒ»¸ö½öÔÚµ±Ç°Æ¥ÅäµãÊÇÄ¿±ê×Ö·û´®µÄ¿ªÍ·Ê±²ÅÎªÕæµÄ¶ÏÑÔ¡£ÔÚ×Ö·ûÀàÖ®ÖУ¬Òôµ÷·ûµÄº¬ÒåÍêÈ«²»Í¬£¨¼ûÏÂÃæ£©¡£ </para> <para> Èç¹ûÉæ¼°µ½¼¸Ñ¡Ò»Ê±Òôµ÷·û²»ÐèÒªÊÇģʽµÄµÚÒ»¸ö×Ö·û£¬µ«Èç¹û³öÏÖÔÚij¸ö·ÖÖ§ÖÐÔòÓ¦¸ÃÊǸÃÑ¡Ôñ·ÖÖ§µÄµÚÒ»¸ö×Ö·û¡£Èç¹ûËùÓеÄÑ¡Ôñ·ÖÖ§¶¼ÒÔÒôµ÷·û¿ªÍ·£¬Õâ¾ÍÊÇ˵£¬Èç¹ûģʽÏÞÖÆÎªÖ»Æ¥ÅäÄ¿±êµÄ¿ªÍ·£¬ÄÇôÕâÊÇÒ»¸ö½ô¹Ìģʽ¡££¨Ò²ÓÐÆäËü½á¹¹¿ÉÒÔʹģʽ³ÉΪ½ô¹ÌµÄ¡££© </para> <para> ÃÀÔª·ûÊÇÒ»¸ö½öÔÚµ±Ç°Æ¥ÅäµãÊÇÄ¿±ê×Ö·û´®µÄ½áβ»òÕßµ±×îºóÒ»¸ö×Ö·ûÊÇ»»ÐзûʱÆäÇ°ÃæµÄλÖÃʱΪ &true; µÄ¶ÏÑÔ£¨Ä¬ÈÏÇé¿öÏ£©¡£Èç¹ûÉæ¼°µ½¼¸Ñ¡Ò»Ê±ÃÀÔª·û²»ÐèÒªÊÇģʽµÄ×îºóÒ»¸ö×Ö·û£¬µ«Ó¦¸ÃÊÇÆä³öÏֵķÖÖ§ÖеÄ×îºóÒ»¸ö×Ö·û¡£ÃÀÔª·ûÔÚ×Ö·ûÀàÖ®ÖÐûÓÐÌØÊ⺬Òå¡£ </para> <para> ÃÀÔª·ûµÄº¬Òå¿É±»¸Ä±äʹÆä½öÆ¥Åä×Ö·û´®µÄ½áβ£¬Ö»ÒªÔÚ±àÒë»òÆ¥ÅäʱÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_DOLLAR_ENDONLY</link> Ñ¡Ïî¼´¿É¡£Õâ²¢²»Ó°Ïì \Z ¶ÏÑÔ¡£ </para> <para> Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link> Ñ¡ÏîÔòÒôµ÷·ûºÍÃÀÔª·ûµÄº¬Òå±»¸Ä±äÁË¡£´ËÖÖÇé¿öÏ£¬ËüÃÇ·Ö±ðÆ¥Åä½ô½Ó×ÅÄÚ²¿ "\n" ×Ö·ûµÄÖ®ºóºÍ֮ǰ£¬ÔÙ¼ÓÉÏÄ¿±ê×Ö·û´®µÄ¿ªÍ·ºÍ½áβ¡£ÀýÈçģʽ /^abc$/ ÔÚ¶àÐÐģʽÏÂÆ¥ÅäÁËÄ¿±ê×Ö·û´® "def\nabc"£¬µ«Õý³£Ê±²»Æ¥Åä¡£Òò´Ë£¬ÓÉÓÚËùÓзÖÖ§¶¼ÒÔ "^" ¿ªÍ·¶øÔÚµ¥ÐÐģʽϳÉΪ½ô¹ÌµÄģʽÔÚ¶àÐÐģʽÏÂΪ·Ç½ô¹ÌµÄ¡£Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link>£¬Ôò <link linkend="pcre.pattern.modifiers">PCRE_DOLLAR_ENDONLY</link> Ñ¡Ïî»á±»ºöÂÔ¡£ </para> <para> ×¢Òâ \A£¬\Z ºÍ \z ÐòÁÐÔÚÁ½ÖÖÇé¿ö϶¼¿ÉÒÔÓÃÀ´Æ¥ÅäÄ¿±êµÄ¿ªÍ·ºÍ½á⣬Èç¹ûģʽËùÓеķÖÖ§¶¼ÒÔ \A ¿ªÊ¼ÔòÆä×ÜÊǽô¹ÌµÄ£¬²»ÂÛÊÇ·ñÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link>¡£ </para> </refsect2> <refsect2 id="regexp.reference.dot"> <title>¾äºÅ£¨.£©</title> <para> ÔÚ×Ö·ûÀàÖ®Í⣬ģʽÖеÄÔ²µã¿ÉÒÔÆ¥ÅäÄ¿±êÖеÄÈκÎÒ»¸ö×Ö·û£¬°üÀ¨²»¿É´òÓ¡×Ö·û£¬µ«²»Æ¥Åä»»Ðзû£¨Ä¬ÈÏÇé¿öÏ£©¡£Èç¹ûÉ趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> ÔòÔ²µãÒ²»áÆ¥Åä»»Ðзû¡£´¦ÀíÔ²µãÓë´¦ÀíÒôµ÷·ûºÍÃÀÔª·ûÊÇÍêÈ«¶ÀÁ¢µÄ£¬Î¨Ò»µÄÁªÏµ¾ÍÊÇËüÃǶ¼Éæ¼°µ½»»Ðзû¡£Ô²µãÔÚ×Ö·ûÀàÖ®ÖÐûÓÐÌØÊ⺬Òå¡£ </para> </refsect2> <refsect2 id="regexp.reference.squarebrackets"> <title>·½À¨ºÅ£¨[]£©</title> <para> ×ó·½À¨ºÅ¿ªÊ¼ÁËÒ»¸ö×Ö·ûÀ࣬ÓÒ·½À¨ºÅ½áÊøÖ®¡£µ¥¶ÀÒ»¸öÓÒ·½À¨ºÅ²»ÊÇÌØÊâ×Ö·û¡£Èç¹ûÔÚ×Ö·ûÀàÖ®ÖÐÐèÒªÒ»¸öÓÒ·½À¨ºÅ£¬ÔòÆäÓ¦¸ÃÊÇ×Ö·ûÀàÖеĵÚÒ»¸ö×Ö·û£¨Èç¹ûÓÐÒôµ÷·ûµÄ»°£¬Ôò½ô½ÓÒôµ÷·ûÖ®ºó£©£¬»òÕßÓ÷´Ð±ÏßתÒå¡£ </para> <para> ×Ö·ûÀàÆ¥ÅäÄ¿±êÖеÄÒ»¸ö×Ö·û£¬¸Ã×Ö·û±ØÐëÊÇ×Ö·ûÀඨÒåµÄ×Ö·û¼¯ÖеÄÒ»¸ö£»³ý·Ç×Ö·ûÀàÖеĵÚÒ»¸ö×Ö·ûÊÇÒôµ÷·û£¬´ËÇé¿öÏÂÄ¿±ê×Ö·û±ØÐë²»ÔÚ×Ö·ûÀඨÒåµÄ×Ö·û¼¯ÖС£Èç¹ûÔÚ×Ö·ûÀàÖÐÐèÒªÒôµ÷·û±¾Éí£¬ÔòÆä±ØÐë²»ÊǵÚÒ»¸ö×Ö·û£¬»òÓ÷´Ð±ÏßתÒå¡£ </para> <para> ¾ÙÀý˵£¬×Ö·ûÀà [aeiou] Æ¥ÅäÁËÈκÎÒ»¸öСдԪÒô×Öĸ£¬¶ø [^aeiou] Æ¥ÅäÁËÈκÎÒ»¸ö²»ÊÇСдԪÒô×ÖĸµÄ×Ö·û¡£×¢ÒâÒôµ÷·ûÖ»ÊÇÒ»¸öͨ¹ýö¾ÙÖ¸¶¨ÄÇЩ²»ÔÚ×Ö·ûÀàÖ®ÖеÄ×Ö·ûµÄ·ûºÅ¡£²»ÊǶÏÑÔ£ºÈԾɻáÏûºÄµôÄ¿±ê×Ö·û´®ÖеÄÒ»¸ö×Ö·û£¬Èç¹ûµ±Ç°Î»ÖÃÔÚ×Ö·û´®½áβµÄ»°Ôòʧ°Ü¡£ </para> <para> µ±É趨Á˲»Çø·Ö´óСдµÄÆ¥Åäʱ£¬×Ö·ûÀàÖеÄÈκÎ×Öĸͬʱ´ú±íÁËÆä´óСдÐÎʽ£¬Òò´Ë¾ÙÀý˵£¬Ð¡Ð´µÄ [aeiou] ͬʱƥÅäÁË "A" ºÍ "a"£¬Ð¡Ð´µÄ [^aeiou] ²»Æ¥Åä "A"£¬µ«Çø·Ö´óСдʱÔò»áÆ¥Åä¡£ </para> <para> »»ÐзûÔÚ×Ö·ûÀàÖв»»áÌØÊâ¶Ô´ý£¬²»ÂÛ <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> »òÕß <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link> Ñ¡ÏîÉ趨ÁËʲôֵ¡£ÐÎÈç [^a] µÄ×Ö·ûÀà×ÜÊÇÄܹ»ºÍ»»ÐзûÏàÆ¥ÅäµÄ¡£ </para> <para> ¼õºÅ£¨-£©×Ö·û¿ÉÒÔÔÚ×Ö·ûÀàÖÐÖ¸¶¨Ò»¸ö×Ö·û·¶Î§¡£ÀýÈ磬[d-m] Æ¥ÅäÁË d ºÍ m Ö®¼äµÄÈκÎ×Ö·û£¬°üÀ¨Á½Õß¡£Èç¹û×Ö·ûÀàÖÐÐèÒª¼õºÅ±¾Éí£¬Ôò±ØÐëÓ÷´Ð±ÏßתÒå»òÕ߷ŵ½Ò»¸ö²»Äܱ»½âÊÍΪָ¶¨·¶Î§µÄλÖ㬵äÐ͵ÄλÖÃÊÇ×Ö·ûÀàÖеĵÚÒ»¸ö»ò×îºóÒ»¸ö×Ö·û¡£ </para> <para> ×ÖÃæÉ쵀 "]" ²»¿ÉÄܱ»µ±³É×Ö·û·¶Î§µÄ½áÊø¡£ÐÎÈç [W-]46] µÄģʽ»á±»½âÊÍΪ°üÀ¨Á½¸ö×Ö·ûµÄ×Ö·ûÀࣨ"W" and "-"£©ºóÃæ¸ú×Å×Ö·û´® "46]"£¬Òò´ËÆä»áÆ¥Åä "W46]" »òÕß "-46]"¡£È»¶ø£¬Èç¹û½« "]" Ó÷´Ð±ÏßתÒ壬Ôò»á±»µ±³É·¶Î§µÄ½áÊøÀ´½âÊÍ¡£Òò´Ë [W-\]46] »á±»½âÊÍΪһ¸ö×Ö·ûÀ࣬°üº¬ÓÐÒ»¸ö·¶Î§ÒÔ¼°Á½¸öµ¥¶ÀµÄ×Ö·û¡£°Ë½øÖÆ»òÊ®Áù½øÖƱíʾµÄ "]" Ò²¿ÉÒÔÓÃÀ´±íʾ·¶Î§µÄ½áÊø¡£ </para> <para> ·¶Î§ÊÇÒÔ ASCII ±È½Ï˳ÐòÀ´²Ù×÷µÄ¡£Ò²¿ÉÒÔÓÃÓÚÓÃÊý×Ö±íʾµÄ×Ö·û£¬ÀýÈç [\000-\037]¡£ÔÚ²»Çø·Ö´óСдƥÅäÖÐÈç¹û·¶Î§Àï°üÀ¨ÁË×Öĸ£¬ÔòͬʱƥÅä´óСд×Öĸ¡£ÀýÈç [W-c] µÈ¼ÛÓÚ [][\^_`wxyzabc] ²»Çø·Ö´óСдµØÆ¥Åä¡£Èç¹ûʹÓÃÁË "fr" ÇøÓòµÄ×Ö·û±í£¬[\xc8-\xcb] Æ¥ÅäÁË´óСдµÄÖØÒô E ×Ö·û¡£ </para> <para> ×Ö·ûÀàÐÍ \d£¬\D£¬\s£¬\S£¬\w ºÍ \W Ò²¿ÉÒÔ³öÏÖÓÚ×Ö·ûÀàÖУ¬²¢½«ÆäËùÄÜÆ¥ÅäµÄ×Ö·ûÌí¼Ó½ø×Ö·ûÀàÖС£ÀýÈ磬[\dABCDEF] Æ¥ÅäÁËÈκÎÊ®Áù½øÖÆÊý×Ö¡£ÓÃÒôµ÷·û¿ÉÒԺܷ½±ãµØÖƶ¨ÑϸñµÄ×Ö·û¼¯£¬ÀýÈç [^\W_] Æ¥ÅäÁËÈκÎ×Öĸ»òÊý×Ö£¬µ«²»Æ¥ÅäÏ»®Ïß¡£ </para> <para> ÈκγýÁË \£¬-£¬^£¨Î»ÓÚ¿ªÍ·£©ÒÔ¼°½áÊøµÄ ] Ö®ÍâµÄ·Ç×ÖĸÊý×Ö×Ö·ûÔÚ×Ö·ûÀàÖж¼Ã»ÓÐÌØÊ⺬Ò壬µ«Êǽ«ËüÃÇתÒåҲûÓлµ´¦¡£ </para> </refsect2> <refsect2 id="regexp.reference.verticalbar"> <title>ÊúÏߣ¨|£©</title> <para> ÊúÏß×Ö·ûÓÃÀ´·Ö¸ô¶àѡһģʽ¡£ÀýÈ磬ģʽ£º <screen> <![CDATA[ gilbert|sullivan ]]> </screen> Æ¥ÅäÁË "gilbert" »òÕß "sullivan" ÖеÄÒ»¸ö¡£¿ÉÒÔÓÐÈÎÒâ¶à¸ö·ÖÖ§£¬Ò²¿ÉÒÔÓпյķÖÖ§£¨Æ¥Åä¿Õ×Ö·û´®£©¡£Æ¥Åä½ø³Ì´Ó×óµ½ÓÒÂÖÁ÷³¢ÊÔÿ¸ö·ÖÖ§£¬²¢Ê¹ÓõÚÒ»¸ö³É¹¦Æ¥ÅäµÄ·ÖÖ§¡£Èç¹û·ÖÖ§ÔÚ×Óģʽ£¨ÔÚÏÂÃæ¶¨Ò壩ÖУ¬Ôò¡°³É¹¦Æ¥Å䡱±íʾͬʱƥÅäÁË×ÓģʽÖеķÖÖ§ÒÔ¼°Ö÷ģʽµÄÆäËü²¿·Ö¡£ </para> </refsect2> <refsect2 id="regexp.reference.internal-options"> <title>ÄÚ²¿Ñ¡ÏîÉ趨</title> <para> <link linkend="pcre.pattern.modifiers">PCRE_CASELESS</link>£¬<link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link>£¬<link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> ºÍ <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> µÄÉ趨¿ÉÒÔÔÚģʽÄÚ²¿Í¨¹ý°üº¬ÔÚ "(?" ºÍ ")" Ö®¼äµÄ Perl Ñ¡Ïî×ÖĸÐòÁÐÀ´¸Ä±ä¡£Ñ¡Ïî×ÖĸΪ£º <screen> <![CDATA[ i ±íʾ <link linkend="pcre.pattern.modifiers">PCRE_CASELESS</link> m ±íʾ <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link> s ±íʾ <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> x ±íʾ <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> ]]> </screen> ÀýÈ磬(?im) É趨Á˲»Çø·Ö´óСд£¬¶àÐÐÆ¥Åä¡£Ò²¿ÉÒÔͨ¹ýÔÚ×Öĸǰ¼ÓÉϼõºÅÀ´È¡ÏûÕâЩѡÏî¡£ÀýÈç×éºÏµÄÑ¡Ïî (?im-sx)£¬É趨ÁË <link linkend="pcre.pattern.modifiers">PCRE_CASELESS</link> ºÍ <link linkend="pcre.pattern.modifiers">PCRE_MULTILINE</link>£¬²¢È¡ÏûÁË <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> ºÍ <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link>¡£Èç¹ûÒ»¸ö×ÖĸÔÚ¼õºÅ֮ǰÓëÖ®ºó¶¼³öÏÖÁË£¬Ôò¸ÃÑ¡ÏȡÏûÉ趨¡£ </para> <para> ÕâЩѡÏî¸Ä±äµÄ·¶Î§ÒÀÀµÓÚÉ趨ÔÚģʽ³öÏÖµÄλÖá£×Óģʽ£¨ÔÚºóÃæ¶¨Ò壩֮ÍâµÄÉ趨£¬ÆäЧ¹ûµÈͬÓÚÑ¡ÏîÔÚ¿ªÊ¼Æ¥ÅäʱÉ趨»òÕßÈ¡ÏûÉ趨¡£ÏÂÃæµÄģʽÐÐΪÍêÈ«Ïàͬ£º <screen> <![CDATA[ (?i)abc a(?i)bc ab(?i)c abc(?i) ]]> </screen> ËüÃǵÈͬÓÚ½«Ä£Ê½ "abc" ¼ÓÉÏÑ¡Ïî <link linkend="pcre.pattern.modifiers">PCRE_CASELESS</link>¡£»»¾ä»°Ëµ£¬ÕâÖÖ¡°¶¥²ã¡±É趨ÊÊÓÃÓÚÕû¸öģʽ£¨³ý·ÇÔÚ×ÓģʽÖÐÓÖÓÐÆäËüÐ޸ģ©¡£Èç¹û¶Ôij¸öÑ¡ÏîÓг¬¹ýÒ»¸öµÄ¶¥²ãÉ趨£¬ÔòʹÓÃ×îÓұߵÄÒ»¸ö¡£ </para> <para> Èç¹ûÑ¡Ïî¸Ä±ä³öÏÖÓÚ×ÓģʽÖУ¬ÔòЧ¹û²»Í¬¡£ÕâÊÇ Perl 5.005 µÄÐÐΪµÄÒ»¸ö±ä»¯¡£×ÓģʽÖеÄÑ¡Ïî¸Ä±äÖ»Ó°Ïìµ½×ÓģʽÄÚ²¿ÆäºóµÄ²¿·Ö£¬Òò´Ë <screen> <![CDATA[ (a(?i)b)c ]]> </screen> ½«Ö»Æ¥Åä "abc" ºÍ "aBc"£¨¼Ù¶¨Ã»ÓÐʹÓà <link linkend="pcre.pattern.modifiers">PCRE_CASELESS</link>£©¡£ÕâÒâζ×ÅÑ¡ÏîÔÚģʽµÄ²»Í¬²¿Î»¿ÉÒÔÔì³É²»Í¬µÄÉ趨¡£ÔÚÒ»¸ö·ÖÖ§Öеĸıä¿ÉÒÔ´«µÝµ½Í¬Ò»¸ö×ÓģʽÖкóÃæµÄ·ÖÖ§ÖУ¬ÀýÈ磺 <screen> <![CDATA[ (a(?i)b|c) ]]> </screen> ½«Æ¥Åä "ab"£¬"aB"£¬"c" ºÍ "C"£¬¾¡¹ÜÔÚÆ¥Åä "C" µÄʱºòµÚÒ»¸ö·ÖÖ§»áÔÚÑ¡ÏîÉ趨֮ǰ¾Í±»¶ªÆú¡£ÕâÊÇÒòΪѡÏîÉ趨µÄЧ¹ûÊÇÔÚ±àÒëʱȷ¶¨µÄ£¬·ñÔò»áÔì³É·Ç³£¹ÖÒìµÄÐÐΪ¡£ </para> <para> PCRE רÓÃÑ¡Ïî <link linkend="pcre.pattern.modifiers">PCRE_UNGREEDY</link> ºÍ <link linkend="pcre.pattern.modifiers">PCRE_EXTRA</link> ¿ÉÒÔºÍ Perl ¼æÈÝÑ¡ÏîÒÔͬÑùµÄ·½Ê½À´¸Ä±ä£¬·Ö±ðʹÓÃ×Öĸ U ºÍ X¡£(?X) ±ê¼ÇÉ趨ÓÐÐ©ÌØÊ⣬Ëü±ØÐë³öÏÖÓÚÈÎºÎÆäËüÌØÐÔ֮ǰ¡£×îºÃ·ÅÔÚ×ͷµÄλÖᣠ</para> </refsect2> <refsect2 id="regexp.reference.subpatterns"> <title>×Óģʽ</title> <para> ×ÓģʽÓÉÔ²À¨ºÅ¶¨½ç£¬¿ÉÒÔǶÌס£½«Ä£Ê½ÖеÄÒ»²¿·Ö±ê¼ÇΪ×Óģʽ¿ÉÒÔ£º </para> <para> 1. ½«¶àѡһµÄ·ÖÖ§¾Ö²¿»¯¡£ÀýÈ磬ģʽ£º <screen> <![CDATA[ cat(aract|erpillar|) ]]> </screen> Æ¥ÅäÁË "cat"£¬"cataract" »ò "caterpillar" Ö®Ò»£¬Ã»ÓÐÔ²À¨ºÅµÄ»°½«Æ¥Åä "cataract"£¬"erpillar" »ò¿Õ×Ö·û´®¡£ </para> <para> 2. ½«×ÓģʽÉ趨Ϊ²¶»ñ×Óģʽ£¨ÈçͬÒÔǰ¶¨ÒåµÄ£©¡£µ±Õû¸öģʽƥÅäʱ£¬Ä¿±ê×Ö·û´®ÖÐÆ¥ÅäÁË×ÓģʽµÄ²¿·Ö»áͨ¹ý <function>pcre_exec</function> µÄ <emphasis>ovector</emphasis> ²ÎÊý´«µÝ»Øµ÷ÓÃÕß¡£×óÔ²À¨ºÅ´Ó×óµ½ÓÒ¼ÆÊý£¨´Ó 1 ¿ªÊ¼£©ÒÔÈ¡µÃ²¶»ñ×ÓģʽµÄÊýÄ¿¡£ </para> <para> ÀýÈ磬Èç¹û½«×Ö·û´® "the red king" À´ºÍģʽ <screen> <![CDATA[ the ((red|white) (king|queen)) ]]> </screen> ½øÐÐÆ¥Å䣬²¶»ñµÄ×Ó´®Îª "red king"£¬"red" ÒÔ¼° "king"£¬²¢±»¼ÆÎª 1£¬2 ºÍ 3¡£ </para> <literallayout> The fact that plain parentheses fulfil two functions is not always helpful. There are often times when a grouping subpattern is required without a capturing requirement. If an opening parenthesis is followed by "?:", the subpattern does not do any capturing, and is not counted when computing the number of any subsequent capturing subpatterns. For example, if the string "the white queen" is matched against the pattern the ((?:red|white) (king|queen)) the captured substrings are "white queen" and "queen", and are numbered 1 and 2. The maximum number of captured substrings is 99, and the maximum number of all subpatterns, both capturing and non-capturing, is 200. As a convenient shorthand, if any option settings are required at the start of a non-capturing subpattern, the option letters may appear between the "?" and the ":". Thus the two patterns (?i:saturday|sunday) (?:(?i)saturday|sunday) match exactly the same set of strings. Because alternative branches are tried from left to right, and options are not reset until the end of the subpattern is reached, an option setting in one branch does affect subsequent branches, so the above patterns match "SUNDAY" as well as "Saturday". </literallayout> </refsect2> <refsect2 id="regexp.reference.repetition"> <title>Repetition</title> <literallayout> Repetition is specified by quantifiers, which can follow any of the following items: a single character, possibly escaped the . metacharacter a character class a back reference (see next section) a parenthesized subpattern (unless it is an assertion - see below) The general repetition quantifier specifies a minimum and maximum number of permitted matches, by giving the two numbers in curly brackets (braces), separated by a comma. The numbers must be less than 65536, and the first must be less than or equal to the second. For example: z{2,4} matches "zz", "zzz", or "zzzz". A closing brace on its own is not a special character. If the second number is omitted, but the comma is present, there is no upper limit; if the second number and the comma are both omitted, the quantifier specifies an exact number of required matches. Thus [aeiou]{3,} matches at least 3 successive vowels, but may match many more, while \d{8} matches exactly 8 digits. An opening curly bracket that appears in a position where a quantifier is not allowed, or one that does not match the syntax of a quantifier, is taken as a literal character. For example, {,6} is not a quantifier, but a literal string of four characters. The quantifier {0} is permitted, causing the expression to behave as if the previous item and the quantifier were not present. For convenience (and historical compatibility) the three most common quantifiers have single-character abbreviations: * is equivalent to {0,} + is equivalent to {1,} ? is equivalent to {0,1} It is possible to construct infinite loops by following a subpattern that can match no characters with a quantifier that has no upper limit, for example: (a?)* Earlier versions of Perl and PCRE used to give an error at compile time for such patterns. However, because there are cases where this can be useful, such patterns are now accepted, but if any repetition of the subpattern does in fact match no characters, the loop is forcibly broken. By default, the quantifiers are "greedy", that is, they match as much as possible (up to the maximum number of permitted times), without causing the rest of the pattern to fail. The classic example of where this gives problems is in trying to match comments in C programs. These appear between the sequences /* and */ and within the sequence, individual * and / characters may appear. An attempt to match C comments by applying the pattern /\*.*\*/ to the string /* first command */ not comment /* second comment */ fails, because it matches the entire string due to the greediness of the .* item. However, if a quantifier is followed by a question mark, then it ceases to be greedy, and instead matches the minimum number of times possible, so the pattern /\*.*?\*/ does the right thing with the C comments. The meaning of the various quantifiers is not otherwise changed, just the preferred number of matches. Do not confuse this use of ques- tion mark with its use as a quantifier in its own right. Because it has two uses, it can sometimes appear doubled, as in \d??\d which matches one digit by preference, but can match two if that is the only way the rest of the pattern matches. If the <link linkend="pcre.pattern.modifiers">PCRE_UNGREEDY</link> option is set (an option which is not available in Perl) then the quantifiers are not greedy by default, but individual ones can be made greedy by following them with a question mark. In other words, it inverts the default behaviour. When a parenthesized subpattern is quantified with a minimum repeat count that is greater than 1 or with a limited maximum, more store is required for the compiled pattern, in proportion to the size of the minimum or maximum. If a pattern starts with .* or .{0,} and the <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> option (equivalent to Perl's /s) is set, thus allowing the . to match newlines, then the pattern is implicitly anchored, because whatever follows will be tried against every character position in the subject string, so there is no point in retrying the overall match at any position after the first. PCRE treats such a pattern as though it were preceded by \A. In cases where it is known that the subject string contains no newlines, it is worth setting <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> when the pattern begins with .* in order to obtain this optimization, or alternatively using ^ to indicate anchoring explicitly. When a capturing subpattern is repeated, the value captured is the substring that matched the final iteration. For example, after (tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value of the captured substring is "tweedledee". However, if there are nested capturing subpatterns, the corresponding captured values may have been set in previous iterations. For example, after /(a|(b))+/ matches "aba" the value of the second captured substring is "b". </literallayout> </refsect2> <refsect2 id="regexp.reference.back-references"> <title>BACK REFERENCES</title> <literallayout> Outside a character class, a backslash followed by a digit greater than 0 (and possibly further digits) is a back reference to a capturing subpattern earlier (i.e. to its left) in the pattern, provided there have been that many previous capturing left parentheses. However, if the decimal number following the backslash is less than 10, it is always taken as a back reference, and causes an error only if there are not that many capturing left parentheses in the entire pattern. In other words, the parentheses that are referenced need not be to the left of the reference for numbers less than 10. See the section entitled "Backslash" above for further details of the handling of digits following a backslash. A back reference matches whatever actually matched the capturing subpattern in the current subject string, rather than anything matching the subpattern itself. So the pattern (sens|respons)e and \1ibility matches "sense and sensibility" and "response and responsibility", but not "sense and responsibility". If caseful matching is in force at the time of the back reference, then the case of letters is relevant. For example, ((?i)rah)\s+\1 matches "rah rah" and "RAH RAH", but not "RAH rah", even though the original capturing subpattern is matched caselessly. There may be more than one back reference to the same subpattern. If a subpattern has not actually been used in a particular match, then any back references to it always fail. For example, the pattern (a|(bc))\2 always fails if it starts to match "a" rather than "bc". Because there may be up to 99 back references, all digits following the backslash are taken as part of a potential back reference number. If the pattern continues with a digit character, then some delimiter must be used to terminate the back reference. If the <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> option is set, this can be whitespace. Otherwise an empty comment can be used. A back reference that occurs inside the parentheses to which it refers fails when the subpattern is first used, so, for example, (a\1) never matches. However, such references can be useful inside repeated subpatterns. For example, the pattern (a|b\1)+ matches any number of "a"s and also "aba", "ababaa" etc. At each iteration of the subpattern, the back reference matches the character string corresponding to the previous iteration. In order for this to work, the pattern must be such that the first iteration does not need to match the back reference. This can be done using alternation, as in the example above, or by a quantifier with a minimum of zero. </literallayout> </refsect2> <refsect2 id="regexp.reference.assertions"> <title>Assertions</title> <literallayout> An assertion is a test on the characters following or preceding the current matching point that does not actually consume any characters. The simple assertions coded as \b, \B, \A, \Z, \z, ^ and $ are described above. More complicated assertions are coded as subpatterns. There are two kinds: those that look ahead of the current position in the subject string, and those that look behind it. An assertion subpattern is matched in the normal way, except that it does not cause the current matching position to be changed. Lookahead assertions start with (?= for positive assertions and (?! for negative assertions. For example, \w+(?=;) matches a word followed by a semicolon, but does not include the semicolon in the match, and foo(?!bar) matches any occurrence of "foo" that is not followed by "bar". Note that the apparently similar pattern (?!foo)bar does not find an occurrence of "bar" that is preceded by something other than "foo"; it finds any occurrence of "bar" whatsoever, because the assertion (?!foo) is always &true; when the next three characters are "bar". A lookbehind assertion is needed to achieve this effect. Lookbehind assertions start with (?&lt;= for positive assertions and (?&lt;! for negative assertions. For example, (?&lt;!foo)bar does find an occurrence of "bar" that is not preceded by "foo". The contents of a lookbehind assertion are restricted such that all the strings it matches must have a fixed length. However, if there are several alternatives, they do not all have to have the same fixed length. Thus (?&lt;=bullock|donkey) is permitted, but (?&lt;!dogs?|cats?) causes an error at compile time. Branches that match different length strings are permitted only at the top level of a lookbehind assertion. This is an extension compared with Perl 5.005, which requires all branches to match the same length of string. An assertion such as (?&lt;=ab(c|de)) is not permitted, because its single top-level branch can match two different lengths, but it is acceptable if rewritten to use two top-level branches: (?&lt;=abc|abde) The implementation of lookbehind assertions is, for each alternative, to temporarily move the current position back by the fixed width and then try to match. If there are insufficient characters before the current position, the match is deemed to fail. Lookbehinds in conjunction with once-only subpatterns can be particularly useful for matching at the ends of strings; an example is given at the end of the section on once-only subpatterns. Several assertions (of any sort) may occur in succession. For example, (?&lt;=\d{3})(?&lt;!999)foo matches "foo" preceded by three digits that are not "999". Notice that each of the assertions is applied independently at the same point in the subject string. First there is a check that the previous three characters are all digits, then there is a check that the same three characters are not "999". This pattern does not match "foo" preceded by six characters, the first of which are digits and the last three of which are not "999". For example, it doesn't match "123abcfoo". A pattern to do that is (?&lt;=\d{3}...)(?&lt;!999)foo This time the first assertion looks at the preceding six characters, checking that the first three are digits, and then the second assertion checks that the preceding three characters are not "999". Assertions can be nested in any combination. For example, (?&lt;=(?&lt;!foo)bar)baz matches an occurrence of "baz" that is preceded by "bar" which in turn is not preceded by "foo", while (?&lt;=\d{3}(?!999)...)foo is another pattern which matches "foo" preceded by three digits and any three characters that are not "999". Assertion subpatterns are not capturing subpatterns, and may not be repeated, because it makes no sense to assert the same thing several times. If any kind of assertion contains capturing subpatterns within it, these are counted for the purposes of numbering the capturing subpatterns in the whole pattern. However, substring capturing is carried out only for positive assertions, because it does not make sense for negative assertions. Assertions count towards the maximum of 200 parenthesized subpatterns. </literallayout> </refsect2> <refsect2 id="regexp.reference.onlyonce"> <title>Once-only subpatterns</title> <literallayout> With both maximizing and minimizing repetition, failure of what follows normally causes the repeated item to be re- evaluated to see if a different number of repeats allows the rest of the pattern to match. Sometimes it is useful to prevent this, either to change the nature of the match, or to cause it fail earlier than it otherwise might, when the author of the pattern knows there is no point in carrying on. Consider, for example, the pattern \d+foo when applied to the subject line 123456bar After matching all 6 digits and then failing to match "foo", the normal action of the matcher is to try again with only 5 digits matching the \d+ item, and then with 4, and so on, before ultimately failing. Once-only subpatterns provide the means for specifying that once a portion of the pattern has matched, it is not to be re-evaluated in this way, so the matcher would give up immediately on failing to match "foo" the first time. The notation is another kind of special parenthesis, starting with (?&gt; as in this example: (?&gt;\d+)bar This kind of parenthesis "locks up" the part of the pattern it contains once it has matched, and a failure further into the pattern is prevented from backtracking into it. Back- tracking past it to previous items, however, works as normal. An alternative description is that a subpattern of this type matches the string of characters that an identical standalone pattern would match, if anchored at the current point in the subject string. Once-only subpatterns are not capturing subpatterns. Simple cases such as the above example can be thought of as a maximizing repeat that must swallow everything it can. So, while both \d+ and \d+? are prepared to adjust the number of digits they match in order to make the rest of the pattern match, (?&gt;\d+) can only match an entire sequence of digits. This construction can of course contain arbitrarily complicated subpatterns, and it can be nested. Once-only subpatterns can be used in conjunction with look- behind assertions to specify efficient matching at the end of the subject string. Consider a simple pattern such as abcd$ when applied to a long string which does not match. Because matching proceeds from left to right, PCRE will look for each "a" in the subject and then see if what follows matches the rest of the pattern. If the pattern is specified as ^.*abcd$ then the initial .* matches the entire string at first, but when this fails (because there is no following "a"), it backtracks to match all but the last character, then all but the last two characters, and so on. Once again the search for "a" covers the entire string, from right to left, so we are no better off. However, if the pattern is written as ^(?>.*)(?&lt;=abcd) then there can be no backtracking for the .* item; it can match only the entire string. The subsequent lookbehind assertion does a single test on the last four characters. If it fails, the match fails immediately. For long strings, this approach makes a significant difference to the processing time. When a pattern contains an unlimited repeat inside a subpattern that can itself be repeated an unlimited number of times, the use of a once-only subpattern is the only way to avoid some failing matches taking a very long time indeed. The pattern (\D+|&lt;\d+>)*[!?] matches an unlimited number of substrings that either consist of non-digits, or digits enclosed in &lt;>, followed by either ! or ?. When it matches, it runs quickly. However, if it is applied to aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa it takes a long time before reporting failure. This is because the string can be divided between the two repeats in a large number of ways, and all have to be tried. (The example used [!?] rather than a single character at the end, because both PCRE and Perl have an optimization that allows for fast failure when a single character is used. They remember the last single character that is required for a match, and fail early if it is not present in the string.) If the pattern is changed to ((?>\D+)|&lt;\d+>)*[!?] sequences of non-digits cannot be broken, and failure happens quickly. </literallayout> </refsect2> <refsect2 id="regexp.reference.conditional"> <title>Conditional subpatterns</title> <literallayout> It is possible to cause the matching process to obey a subpattern conditionally or to choose between two alternative subpatterns, depending on the result of an assertion, or whether a previous capturing subpattern matched or not. The two possible forms of conditional subpattern are (?(condition)yes-pattern) (?(condition)yes-pattern|no-pattern) If the condition is satisfied, the yes-pattern is used; otherwise the no-pattern (if present) is used. If there are more than two alternatives in the subpattern, a compile-time error occurs. There are two kinds of condition. If the text between the parentheses consists of a sequence of digits, then the condition is satisfied if the capturing subpattern of that number has previously matched. Consider the following pattern, which contains non-significant white space to make it more readable (assume the <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> option) and to divide it into three parts for ease of discussion: ( \( )? [^()]+ (?(1) \) ) The first part matches an optional opening parenthesis, and if that character is present, sets it as the first captured substring. The second part matches one or more characters that are not parentheses. The third part is a conditional subpattern that tests whether the first set of parentheses matched or not. If they did, that is, if subject started with an opening parenthesis, the condition is &true;, and so the yes-pattern is executed and a closing parenthesis is required. Otherwise, since no-pattern is not present, the subpattern matches nothing. In other words, this pattern matches a sequence of non-parentheses, optionally enclosed in parentheses. If the condition is not a sequence of digits, it must be an assertion. This may be a positive or negative lookahead or lookbehind assertion. Consider this pattern, again containing non-significant white space, and with the two alternatives on the second line: (?(?=[^a-z]*[a-z]) \d{2}-[a-z]{3}-\d{2} | \d{2}-\d{2}-\d{2} ) The condition is a positive lookahead assertion that matches an optional sequence of non-letters followed by a letter. In other words, it tests for the presence of at least one letter in the subject. If a letter is found, the subject is matched against the first alternative; otherwise it is matched against the second. This pattern matches strings in one of the two forms dd-aaa-dd or dd-dd-dd, where aaa are letters and dd are digits. </literallayout> </refsect2> <refsect2 id="regexp.reference.comments"> <title>Comments</title> <literallayout> The sequence (?# marks the start of a comment which continues up to the next closing parenthesis. Nested parentheses are not permitted. The characters that make up a comment play no part in the pattern matching at all. If the <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> option is set, an unescaped # character outside a character class introduces a comment that contin- ues up to the next newline character in the pattern. </literallayout> </refsect2> <refsect2 id="regexp.reference.recursive"> <title>Recursive patterns</title> <literallayout> Consider the problem of matching a string in parentheses, allowing for unlimited nested parentheses. Without the use of recursion, the best that can be done is to use a pattern that matches up to some fixed depth of nesting. It is not possible to handle an arbitrary nesting depth. Perl 5.6 has provided an experimental facility that allows regular expressions to recurse (amongst other things). The special item (?R) is provided for the specific case of recursion. This PCRE pattern solves the parentheses problem (assume the <link linkend="pcre.pattern.modifiers">PCRE_EXTENDED</link> option is set so that white space is ignored): \( ( (?>[^()]+) | (?R) )* \) First it matches an opening parenthesis. Then it matches any number of substrings which can either be a sequence of non- parentheses, or a recursive match of the pattern itself (i.e. a correctly parenthesized substring). Finally there is a closing parenthesis. This particular example pattern contains nested unlimited repeats, and so the use of a once-only subpattern for matching strings of non-parentheses is important when applying the pattern to strings that do not match. For example, when it is applied to (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() it yields "no match" quickly. However, if a once-only subpattern is not used, the match runs for a very long time indeed because there are so many different ways the + and * repeats can carve up the subject, and all have to be tested before failure can be reported. The values set for any capturing subpatterns are those from the outermost level of the recursion at which the subpattern value is set. If the pattern above is matched against (ab(cd)ef) the value for the capturing parentheses is "ef", which is the last value taken on at the top level. If additional parentheses are added, giving \( ( ( (?>[^()]+) | (?R) )* ) \) ^ ^ ^ ^ then the string they capture is "ab(cd)ef", the contents of the top level parentheses. If there are more than 15 capturing parentheses in a pattern, PCRE has to obtain extra memory to store data during a recursion, which it does by using pcre_malloc, freeing it via pcre_free afterwards. If no memory can be obtained, it saves data for the first 15 capturing parentheses only, as there is no way to give an out-of-memory error from within a recursion. </literallayout> </refsect2> <refsect2 id="regexp.reference.performances"> <title>Performances</title> <literallayout> Certain items that may appear in patterns are more efficient than others. It is more efficient to use a character class like [aeiou] than a set of alternatives such as (a|e|i|o|u). In general, the simplest construction that provides the required behaviour is usually the most efficient. Jeffrey Friedl's book contains a lot of discussion about optimizing regular expressions for efficient performance. When a pattern begins with .* and the <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> option is set, the pattern is implicitly anchored by PCRE, since it can match only at the start of a subject string. However, if <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> is not set, PCRE cannot make this optimization, because the . metacharacter does not then match a newline, and if the subject string contains newlines, the pattern may match from the character immediately following one of them instead of from the very start. For example, the pattern (.*) second matches the subject "first\nand second" (where \n stands for a newline character) with the first captured substring being "and". In order to do this, PCRE has to retry the match starting after every newline in the subject. If you are using such a pattern with subject strings that do not contain newlines, the best performance is obtained by setting <link linkend="pcre.pattern.modifiers">PCRE_DOTALL</link> , or starting the pattern with ^.* to indicate explicit anchoring. That saves PCRE from having to scan along the subject looking for a newline to restart at. Beware of patterns that contain nested indefinite repeats. These can take a long time to run when applied to a string that does not match. Consider the pattern fragment (a+)* This can match "aaaa" in 33 different ways, and this number increases very rapidly as the string gets longer. (The * repeat can match 0, 1, 2, 3, or 4 times, and for each of those cases other than 0, the + repeats can match different numbers of times.) When the remainder of the pattern is such that the entire match is going to fail, PCRE has in principle to try every possible variation, and this can take an extremely long time. An optimization catches some of the more simple cases such as (a+)*b where a literal character follows. Before embarking on the standard matching procedure, PCRE checks that there is a "b" later in the subject string, and if there is not, it fails the match immediately. However, when there is no following literal this optimization cannot be used. You can see the difference by comparing the behaviour of (a+)*\d with the pattern above. The former gives a failure almost instantly when applied to a whole line of "a" characters, whereas the latter takes an appreciable time with strings longer than about 20 characters. </literallayout> </refsect2> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->
« previous php.doc.zh (#2013) next »