±¾ÎÄÄÚÈÝ, ÕûÀí×ÔÍøÎÄFiner points of PHP regular expressions. Æä·ÖÎö¹ý³Ì°þ¼ë³éË¿, Ë¿Ë¿Èë¿Û, ÖµµÃÒ»¶Á. ¸ÃÎÄϵͳµØÁгöÁËPHPÖÐÕýÔò±í´ïʽ³£¼ûÌØÐÔ, ÎÒֻժȡÆäÖеݹ鲿·Ö·ÒëÕûÀí³öÀ´.
֮ǰһƪÎÄÕ·ÒëÁËPerlÓïÑÔÖеĵݹéÕýÔò±í´ïʽ. Æäʵ²»ÉÙÓïÑÔÖеÄÕýÔò¶¼ÊÇÖ§³ÖµÝ¹éµÄ, ÀýÈç±¾ÎÄÒª½éÉܵÄPHPÕýÔòµÝ¹é. ËäÈ», ¹¤×÷ÖÐ×î³£ÓõÄÕýÔò±í´ïʽ¶¼ºÜ¡±ÕýÔò¡±, Ö»ÓÃ×î»ù±¾µÄÓï·¨¾ÍÄܽâ¾ö85%ÒÔÉϵÄÎÊÌâ, ¶øÇÒºÏÀíÓÐЧµØʹÓÃÆÕͨÕýÔòÀ´½â¾ö¸´ÔÓÎÊÌâÒ²ÊÇÒ»Ãż¼ÇÉÓëѧÎÊ; µ«ÊǸ߼¶Ò»µãµÄÓï·¨µÄÈ·ÓÐËü´æµÄ¼ÛÖµ, ÓÐʱ²»ÓÃËü»¹Õæ°ì²»ÁËʶù; ¿öÇÒѧϰÕýÔòµÄÀÖȤҲÔÚÓÚ³¢ÊÔ¸÷ÖÖ¸÷ÑùµÄ¿ÉÄÜÐÔ, Âú×ã×Ô¼ºÎÞÇîÎÞ¾¡µÄºÃÆæÐÄ.
±¾ÎÄÄÚÈÝ, ÕûÀí×ÔÍøÎÄFiner points of PHP regular expressions. Æä·ÖÎö¹ý³Ì°þ¼ë³éË¿, Ë¿Ë¿Èë¿Û, ÖµµÃÒ»¶Á. ¸ÃÎÄϵͳµØÁгöÁËPHPÖÐÕýÔò±í´ïʽ³£¼ûÌØÐÔ, ÎÒֻժȡÆäÖеݹ鲿·Ö·ÒëÕûÀí³öÀ´.
ÕýÎÄ
Àý×Ó
ʲôʱºò»áÓõ½µÝ¹éÕýÔò±í´ïʽÄØ? µ±È»ÊÇ´ýÆ¥ÅäµÄ×Ö´®ÖеݹéµØ³öÏÖijÖÖģʽʱ(òËÆ·Ï»°). ×î¾µäµÄÀý×Ó, ¾ÍÊǵݹéÕýÔò´¦ÀíǶÌ×À¨ºÅµÄÎÊÌâÁË. Àý×ÓÈçÏÂ.
¼ÙÉèÄãµÄÎı¾Öаüº¬ÁËÕýÈ·Åä¶ÔµÄǶÌ×À¨ºÅ. À¨ºÅµÄÉî¶È¿ÉÒÔÊÇÎÞÏÞ²ã. ÄãÏ벶»ñÕâÑùµÄÀ¨ºÅ×é.
Ë¡ÎÒ¾ç͸, ±ê×¼´ð°¸ÊÇÕâÑùµÄ:
$string = "some text (a(b(c)d)e) more text"; if(preg_match("/\(([^()]+|(?R))*\)/",$string,$matches)) { echo "<pre>"; print_r($matches); echo "</pre>"; }ÆäÊä³ö½á¹ûÊÇ:
Array ( [0] => (a(b(c)d)e) [1] => e )
¿É¼û, ÎÒÃÇËùÐèÒªµÄÎı¾, ÒѾ²¶»ñµ½$matches[0]ÖÐÁË.
ÔÀí
ÏÖÔÚ˼¿¼ÔÀí.
ÉÏÃæµÄÕýÔò±í´ïʽÖеĹؼüµãÊÇ(?R). (?R)µÄ×÷ÓþÍÊǵݹéµØÌæ»»ËüËùÔÚµÄÕûÌõÕýÔò±í´ïʽ. ÔÚÿ´Îµü´úʱ, PHP Óï·¨·ÖÎöÆ÷¶¼»á½«(?R)Ì滻Ϊ¡±\(([^()]+|(?R))*\)¡°.
Òò´Ë, ¾ßÌåµ½ÉÏÊöµÄÀý×Ó, ÆäÕýÔò±í´ïʽµÈ¼ÛÓÚ:
"/\(([^()]+|\(([^()]+|\(([^()]+)*\))*\))*\)/"
µ«ÊÇÉÏÃæµÄ´úÂëÖ»ÊʺÏÉî¶ÈΪ3²ãµÄÀ¨ºÅ. ¶ÔÓÚδ֪Éî¶ÈµÄÀ¨ºÅǶÌ×, ¾ÍÖ»ºÃʹÓÃÕâÖÖÕýÔòÁË:
"/\(([^()]+|(?R))*\)/"
Ëü²»µ«Äܹ»Æ¥ÅäÎÞÏÞÉî¶È, »¹¼ò»¯ÁËÕýÔò±í´ïʽµÄÓï·¨. ¹¦ÄÜÇ¿´ó, Óï·¨¼ò½à.
ÏÖÔÚÀ´Ï¸¿´Ò»ÏÂ"/\(([^()]+|(?R))*\)/"ÊÇÔõÑùÆ¥Åä"(a(b(c)d)e)"µÄ:
"(c)"Õⲿ·Ö±»ÕýÔòʽ "\(([^()]+)*\)" Æ¥Åä. Çë×¢Òâ, (c) Æäʵ¾ÍÏ൱ÓÚÕû¸öµÝ¹éµÄÒ»¸öËõÓ°, ÂéȸËäСÎåÔà¾ãÈ«, Òò´ËËüÓõ½ÁËÕû¸öÕýÔò±í´ïʽ.
»»ÑÔÖ®, ÏÂÒ»²½ÖеÄ(c), ¿ÉÒÔʹÓÃ(?R) À´Æ¥Åä.
(b(c)d)µÄÆ¥Åä¹ý³ÌΪ:
"\("Æ¥Åä"(";
"[^()]+"Æ¥Åä"b";
(?R)Æ¥Åä"(c)";
"[^()]+"Æ¥Åä"d";
"\)"Æ¥Åä")".
¸ù¾ÝÉÏÃæµÄÆ¥ÅäÔÀí, ²»ÄÑÀí½âΪʲôÊý×éµÄµÚ2¸öÔªËØ$matches[1]Óë'e'µÈ¼Û. ×Ó´®'e'ÊÇÔÚ×îºóÒ»´ÎÆ¥Åäµü´úÖб»²¶»ñ. Æ¥Åä¹ý³ÌÖÐ, Ö»ÓÐ×îºóÒ»´ÎµÄ²¶»ñ½á¹û²Å»á±£´æµ½Êý×éÖÐ.
rex×¢: ¹ØÓÚÕâ¸öÌØÐÔ, ¿ÉÒÔ×ÔÐг¢ÊÔÒ»ÏÂ, ¿´¿´Ê¹ÓÃÕýÔòʽ([a-z]+[0-9]+)+À´Æ¥Åä×Ö´®abc123xyz890, Æ䲶»ñ½á¹û$1ÊÇʲô. ×¢Òâ, Æä½á¹ûÓë Left Longest ÔÀí²¢²»³åÍ».
Èç¹ûÎÒÃÇÖ»ÐèÒª²¶»ñ $matches[0], ¿ÉÒÔÕâÑù×ö:
$string = "some text (a(b(c)d)e) more text"; if(preg_match("/\((?:[^()]+|(?R))*\)/",$string,$matches)) { echo "<pre>"; print_r($matches); echo "</pre>"; }²úÉúµÄ½á¹ûÏàͬ:
Array ( [0] => (a(b(c)d)e) )
Ëù×öµÄ¸Ä¶¯ÊDz¶»ñÀ¨ºÅ()¸ÄΪ·Ç²¶»ñ²¶»ñÀ¨ºÅ(?:)ÁË.
»¹¿ÉÒÔ½øÒ»²½ÍêÉÆΪ:
$string = "some text (a(b(c)d)e) more text"; if(preg_match("/\((?>[^()]+|(?R))*\)/",$string,$matches)) { echo "<pre>"; print_r($matches); echo "</pre>"; }
ÕâÀïÎÒÃÇÓõ½ÁËËùνµÄÒ»´ÎÐÔģʽ(rex×¢: ÓàêÉÏÈÉúÒëµÄ¡¶¾«Í¨ÕýÔò±í´ïʽv3.0¡·ÖÐ, ν֮¡±¹Ì»¯·Ö×顱. ¿É²Î¿¼¸ÃÊé.) PHPÊÖ²áÒ²ÍƼöÖ»ÒªÌõ¼þÔÊÐí, ¾Í¾¡¿ÉÄÜʹÓÃÕâÖÖģʽ, ÒÔ±ãÌáÉýÕýÔò±í´ïʽµÄËÙ¶È.
Ò»´ÎÐÔģʽºÜ¼òµ¥, ÕâÀï²»ÔÙÏêÊö. Èç¹û¸ÐÐËȤ, ¿ÉÒԲο¼PHP ¹Ù·½ÊÖ²á. Èç¹ûÄúÏëÉîÈëѧϰPERL¼æÈÝʽÕýÔò±í´ïʽ, Çë²Î¿¼ÎÄÄ©Á´½Ó.
ÔÎÄ: Finer points of PHP regular expressions
Perl¼æÈÝÕýÔò±í´ïʽ ¹ÙÍø Îĵµ
PHP¹ÙÍøµÄPCREÕýÔòÎĵµ
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ