תÒåÐòÁÐ(·´Ð±Ïß)

·´Ð±ÏßÓжàÖÖÓ÷¨¡£Ê×ÏÈ£¬Èç¹û½ô½Ó×ÅÊÇÒ»¸ö·Ç×ÖĸÊý×Ö×Ö·û£¬±íÃ÷È¡Ïû ¸Ã×Ö·ûËù´ú±íµÄÌØÊ⺭Òå¡£ÕâÖÖ½«·´Ð±Ïß×÷ΪתÒå×Ö·ûµÄÓ÷¨ÔÚ×Ö·ûÀà ÄÚ²¿ºÍÍⲿ¶¼¿ÉÓá£

±ÈÈ磬Èç¹ûÄãÏ£ÍûÆ¥ÅäÒ»¸ö "*" ×Ö·û£¬¾ÍÐèÒªÔÚģʽÖÐдΪ "\*"¡£ ÕâÊÊÓÃÓÚÒ»¸ö×Ö·ûÔÚ²»½øÐÐתÒå»áÓÐÌØÊ⺬ÒåµÄÇé¿öÏ¡£ µ«ÊÇ£¬ ¶ÔÓÚ·ÇÊý×Ö×ÖĸµÄ×Ö·û£¬×ÜÊÇÔÚÐèÒªÆä½øÐÐÔ­ÎÄÆ¥ÅäµÄʱºòÔÚËüÇ°ÃæÔö¼ÓÒ»¸ö·´Ð±Ïߣ¬ À´ÉùÃ÷Ëü´ú±í×Ô¼º£¬ÕâÊÇ°²È«µÄ¡£Èç¹ûҪƥÅäÒ»¸ö·´Ð±Ïߣ¬ÄÇôÔÚģʽÖÐʹÓà "\\"¡£

Note:

·´Ð±ÏßÔÚµ¥ÒýºÅ×Ö·û´®ºÍË«ÒýºÅ×Ö·û´® Öж¼ÓÐÌØÊ⺬Ò壬Òò´ËҪƥÅäÒ»¸ö·´Ð±Ïߣ¬ ģʽÖбØÐëдΪ "\\\\"¡£ Òë×¢£º "/\\/"£¬ Ê×ÏÈËü×÷Ϊ×Ö·û´®£¬·´Ð±Ïß»á½øÐÐתÒ壬 ÄÇôתÒåºóµÄ½á¹ûÊÇ/\/£¬Õâ¸ö²ÅÊÇÕýÔò±í´ïʽÒýÇæÄõ½µÄģʽ£¬ ¶øÕýÔò±í´ïʽÒýÇæÒ²ÈÏΪ \ ÊÇתÒå±ê¼Ç£¬Ëü»á½«·Ö¸ô·û / ½øÐÐתÒ壬 ´Ó¶øµÃµ½µÄÊÇÒ»¸ö´íÎó£¬Òò´Ë£¬ÐèÒª 4 ¸ö·´Ð±Ï߲ſÉÒÔÆ¥ÅäÒ»¸ö·´Ð±Ïß¡£

Èç¹ûÒ»¸öģʽ±»Ê¹Óà PCRE_EXTENDED Ñ¡Ïî±àÒ룬 ģʽÖеĿհ××Ö·û(³ýÁË×Ö·ûÀàÖеÄ)ºÍδתÒåµÄ#µ½ÐÐÄ©µÄËùÓÐ×Ö·û¶¼»á±»ºöÂÔ¡£ ÒªÔÚÕâÖÖÇé¿öÏÂʹÓÿհ××Ö·û»òÕß#£¬¾ÍÐèÒª¶ÔÆä½øÐÐתÒå¡£

·´Ð±ÏߵĵڶþÖÖÓÃ;ÌṩÁËÒ»ÖÖ¶Ô·Ç´òÓ¡×Ö·û½øÐпɼû±àÂëµÄ¿ØÖÆÊֶΡ£ ³ýÁ˶þ½øÖÆµÄ 0 »áÖÕ½áÒ»¸öģʽÍ⣬²¢²»»áÑϸñµÄÏÞÖÆ·Ç´òÓ¡×Ö·û(×ÔÉí)µÄ³öÏÖ£¬ µ«Êǵ±Ò»¸öģʽÒÔÎı¾±à¼­Æ÷µÄ·½Ê½±à¼­×¼±¸µÄʱºò£¬ ʹÓÃÏÂÃæµÄתÒåÐòÁÐÏà±ÈʹÓöþ½øÖÆ×Ö·û»á¸ü¼ÓÈÝÒס£

\a
ÏìÁå×Ö·û(Ê®Áù½øÖÆ 07)
\cx
"control-x"£¬x ÊÇÈÎÒâ×Ö·û
\e
תÒå (Ê®Áù½øÖÆ 1B)
\f
»»Ò³ (Ê®Áù½øÖÆ 0C)
\n
»»ÐÐ (Ê®Áù½øÖÆ 0A)
\p{xx}
Ò»¸ö·ûºÏ xx ÊôÐÔµÄ×Ö·û£¬Ïêϸ²é¿´unicode properties ÊôÐÔ
\P{xx}
Ò»¸ö²»·ûºÏxxÊôÐÔµÄ×Ö·û£¬Ïêϸ²é¿´unicode properties ÊôÐÔ
\r
»Ø³µ (Ê®Áù½øÖÆ 0D)
\t
ˮƽÖƱí·û (Ê®Áù½øÖÆ 09)
\xhh
hhÊ®Áù½øÖƱàÂëµÄ×Ö·û£¬Ïêϸ²é¿´unicode properties ÊôÐÔ
\ddd
ddd°Ë½øÖƱàÂëµÄ×Ö·û£¬»òÕߺóÏòÒýÓÃ

\cxµÄÈ·ÇÐЧ¹ûÈçÏ£º Èç¹ûxÊÇÒ»¸öСд×Öĸ£¬Ëü±»×ª»»Îª´óд¡£½Ó×Å£¬ ½«×Ö·ûµÄµÚ6λ(Ê®Áù½øÖÆ 40£¬ÓÒÊýµÚÒ»¸öλΪµÚ0λ)È¡·´¡£ ±ÈÈç\cz³ÉΪʮÁù½øÖƵÄ1A£¬\c{³ÉΪʮÁù½øÖÆ3B£¬ \c;³ÉΪʮÁù½øÖÆ7B¡£

ÔÚ"\x"ºóÃ棬¶ÁÈ¡Á½¸öÊ®Áù½øÖÆÊý(×Öĸ¿ÉÒÔÊÇ´óд»òСд)¡£ ÔÚUTF-8ģʽ£¬ "\x{...}"ÔÊÐíʹÓ㬠»¨À¨ºÅÄÚµÄÄÚÈÝÊÇÊ®Áù½øÖÆÓÐЧÊý×Ö¡£ Ëü½«¸ø³öµÄÊ®Áù½øÖÆÊý×Ö½âÊÍΪ UTF-8 ×Ö·û´úÂë¡£Ô­À´µÄÊ®Áù½øÖÆתÒåÐòÁУ¬ \xhh£¬ Æ¥ÅäÒ»¸öË«×Ö½ÚµÄUTF-8×Ö·û£¬Èç¹ûËüµÄÖµ´óÓÚ127

ÔÚ"\0"Ö®ºó£¬ ¶ÁÈ¡Á½¸ö°Ë½øÖÆÊý¡£ËùÓÐÇé¿öÏ£¬Èç¹ûÊýÉÙÓÚ2¸ö£¬ÔòÖ±½ÓʹÓᣠÐòÁÐ "\0\x\07" Ö¸¶¨ÁËÁ½¸ö¶þ½øÖÆ 0 ½ô¸ú×ÅÒ»¸ö BEL ×Ö·û¡£ ÇëÈ·±£³õʼµÄ 0 Ö®ºóµÄÁ½¸öÊý×ÖÊǺϷ¨µÄ°Ë½øÖÆÊý¡£

´¦ÀíÒ»¸ö·´Ð±Ïß½ô¸ú×ŵIJ»ÊÇ0µÄÊý×ÖµÄÇé¿ö±È½Ï¸´ÔÓ¡£ÔÚ×Ö·ûÀàÍⲿ£¬ PCRE ¶ÁÈ¡Ëü²¢ÒÔÊ®½øÖƶÁÈ¡½ôËæÆäºóµÄÊý×Ö¡£ Èç¹ûÊýֵСÓÚ 10£¬ »òÕß֮ǰ²¶»ñµ½Á˸ÃÊý×ÖÄܹ»´ú±íµÄ×óÀ¨ºÅ(×Ó×é)£¬ Õû¸öÊý×ÖÐòÁб»ÈÏΪÊǺóÏòÒýÓ᣺óÏòÒýÓÃÈçºÎ¹¤×÷ÔÚºóÃæÃèÊö£¬ ½ÓÏÂÀ´¾Í»áÌÖÂÛÀ¨ºÅ×Ó×é¡£

ÔÚÒ»¸ö×Ö·ûÀàÀïÃ棬»òÕßÊ®½øÖÆÊý´óÓÚ 9 ²¢ÇÒûÓÐÄÇô¶àµÄ×Ó×é±»²¶»ñ£¬ PCRE ÖØжÁÈ¡·´Ð±ÏߺóµÄµÚÈý¸ö 8 ½øÖÆÊý×Ö£¬²¢ÇÒ´Ó×îµÍµÄ 8 λÉú³Éµ¥×Ö½ÚÖµ¡£ ÈκεĺóÐøÊý×Ö¶¼´ú±íËüÃÇ×ÔÉí¡£ÀýÈ磺

\040
¿Õ¸ñµÄÁíÍâÒ»ÖÖÓ÷¨
\40
µ±ÌṩÁËÉÙÓÚ40¸ö×Ó×éʱҲÈÏΪÊÇ¿Õ¸ñ¡£
\7
ʼÖÕÊǺóÏòÒýÓÃ
\11
¿ÉÄÜÊǺóÏòÒýÓã¬Ò²¿ÉÄÜÊÇÖƱí·û
\011
×ÜÊÇÒ»¸öÖƱí·û
\0113
Ò»¸öÖƱí·û½ô¸ú×ÅÒ»¸ö3(ÒòΪÿ´Î×î¶àÖ»¶ÁÈ¡3¸ö8½øÖÆλ
\113
°Ë½øÖÆ113´ú±íµÄ×Ö·û
\377
8½øÖÆ377ÊÇ10½øÖÆ255£¬ Òò´Ë´ú±íÒ»¸öÈ«1µÄ×Ö·û
\81
Ò»¸öºóÏòÒýÓûòÕßÒ»¸ö¶þ½øÖÆ 0 ½ô¸ú×ÅÁ½¸öÊý×Ö 8 ºÍ 1(ÒòΪ8²»ÊÇ8½øÖÆÓÐЧÊý×Ö)

×¢Ò⣬°Ë½øÖÆÖµµÄ 100 »òÕ߸ü´óµÄÖµ±ØÐëûÓÐÇ°ÖõÄ0Òýµ¼£¬ ÒòΪÿ´Î×î¶à¶ÁÈ¡3¸ö8½øÖÆλ.

ËùÓÐÐòÁж¨ÒåµÄµ¥×Ö½ÚÖµ¶¼¿ÉÒÔÔÚ×Ö·ûÀàÄÚ²¿»òÍⲿʹÓá£ÁíÍ⣬ÔÚ×Ö·ûÀàÖÐ, ÐòÁÐ "\b" ½âÊÍΪÍ˸ñ×Ö·û¡£×Ö·ûÀàÍâËüÓÖÓв»Í¬µÄÒâÒå(ÏÂÃæÓÐÃèÊö)

·´Ð±ÏߵĵÚÈýÖÖÓ÷¨ÊÇÓÃÀ´ÃèÊöÌض¨µÄ×Ö·ûÀࣺ

\d
ÈÎÒâÊ®½øÖÆÊý×Ö
\D
ÈÎÒâ·ÇÊ®½øÖÆÊý×Ö
\h
ÈÎÒâˮƽ¿Õ°××Ö·û(since PHP 5.2.4)
\H
ÈÎÒâ·Çˮƽ¿Õ°××Ö·û(since PHP 5.2.4)
\s
ÈÎÒâ¿Õ°××Ö·û
\S
ÈÎÒâ·Ç¿Õ°××Ö·û
\v
ÈÎÒâ´¹Ö±¿Õ°××Ö·û(since PHP 5.2.4)
\V
ÈÎÒâ·Ç´¹Ö±¿Õ°××Ö·û(since PHP 5.2.4)
\w
ÈÎÒâµ¥´Ê×Ö·û
\W
ÈÎÒâ·Çµ¥´Ê×Ö·û

ÉÏÃæÿһ¶ÔתÒåÐòÁж¼´ú±íÁËÍêÕû×Ö·û¼¯ÖÐÁ½¸ö²»ÏཻµÄ²¿·Ö£¬ ÈÎÒâ×Ö·ûÒ»¶¨»áÆ¥ÅäÆäÖÐÒ»¸ö£¬Í¬Ê±Ò»¶¨²»»áÆ¥ÅäÁíÍâÒ»¸ö¡£

µ¥´Ê×Ö·ûÖ¸µÄÊÇÈÎÒâ×Öĸ¡¢Êý×Ö¡¢Ï»®Ïß¡£ Ò²¾ÍÊÇ˵ÈÎÒâ¿ÉÒÔ×é³Éperlµ¥´ÊµÄ×Ö·û¡£ ×ÖĸºÍÊý×ֵĶ¨Òåͨ¹ýPCRE×Ö·û±í¿ØÖÆ£¬¿ÉÒÔͨ¹ýÖ¸¶¨µØÓòÉèÖÃʹÆäÆ¥Åä¸Ä±ä¡£±ÈÈ磬 ÔÚ·¨¹ú (fr) µØÓòÉèÖÃÖУ¬Ò»Ð©³¬¹ý 128 µÄ×Ö·û´úÂë±»ÓÃÓÚÖØÒô×Öĸ£¬ ËüÃÇ¿ÉÒÔʵÓà \w Æ¥Åä¡£

ÕâЩ×Ö·ûÀàÐòÁÐÔÚ×Ö·ûÀàÄÚ²¿»òÍⲿ¶¼¿ÉÒÔ³öÏÖ¡£ ËûÃÇÿ´ÎÆ¥ÅäËù´ú±íµÄ×Ö·ûÀàÐÍÖеÄÒ»¸ö×Ö·û¡£ Èç¹ûµ±Ç°Æ¥ÅäµãλÓÚÄ¿±ê×Ö·û´®Ä©Î²£¬ ËüÃÇÖеÄËùÓÐ×Ö·û¶¼Æ¥Åäʧ°Ü£¬ ÒòΪûÓÐ×Ö·ûÈÃËüÃÇÆ¥ÅäÁË¡£

·´Ð±ÏߵĵÚËÄÖÖÓ÷¨ÊÇһЩ¼òµ¥µÄ¶ÏÑÔ¡£ Ò»¸ö¶ÏÑÔÖ¸¶¨Ò»¸ö±ØÐëÔÚÌض¨Î»ÖÃÆ¥ÅäµÄÌõ¼þ£¬ ËüÃDz»»á´ÓÄ¿±ê×Ö·û´®ÖÐÏûºÄÈκÎ×Ö·û¡£ ½ÓÏÂÀ´ÎÒÃÇ»áÌÖÂÛʹÓÃ×Ó×éµÄ¸ü¼Ó¸´ÔӵĶÏÑÔ¡£ ·´Ð±Ï߶ÏÑÔ°üÀ¨£º

\b
µ¥´Ê±ß½ç
\B
·Çµ¥´Ê±ß½ç
\A
Ä¿±êµÄ¿ªÊ¼Î»ÖÃ(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\Z
Ä¿±êµÄ½áÊøλÖûò½áÊø´¦µÄ»»Ðзû(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\z
Ä¿±êµÄ½áÊøλÖÃ(¶ÀÁ¢ÓÚ¶àÐÐģʽ)
\G
ÔÚÄ¿±êÖÐÊ×´ÎÆ¥ÅäλÖÃ

ÕâЩ¶ÏÑÔ²»ÄܳöÏÖÔÚ×Ö·ûÀàÖÐ(µ«ÊÇ×¢Ò⣬ "\b"ÔÚ×Ö·ûÀàÖÐÓв»Í¬µÄÒâÒ壬 ±íʾµÄÊÇÍ˸ñ(backspace)×Ö·û)

Ò»¸öµ¥´Ê±ß½ç±íʾµÄÊÇÔÚÄ¿±ê×Ö·û´®ÖУ¬ µ±Ç°×Ö·ûºÍÇ°Ò»¸ö×Ö·û²»Í¬Ê±Æ¥Åä\w»ò\W(Ò»¸ö±ÈÅä\w, Ò»¸öÆ¥Åä\W)£¬ »òÕß×÷Ϊ×Ö·û´®¿ªÊ¼»ò½áβ×Ö·ûµÄʱºòµ±Ç°×Ö·ûÆ¥Åä\w¡£

\A£¬ \Z£¬ \z¶ÏÑÔ²»Í¬ÓÚ´«Í³µÄ^ºÍ$(Ïê¼ûÏÂÎÄ)£¬ ÒòΪËûÃÇÓÀԶƥÅäÄ¿±ê×Ö·û´®µÄ¿ªÊ¼ºÍ½á⣬¶ø²»»áÊÜģʽÐÞÊηûµÄÏÞÖÆ¡£ ËüÃDz»ÊÜPCRE_MULTILINE£¬PCRE_DOLLAR_ENDONLYÑ¡ÏîµÄÓ°Ïì¡£ \Z ºÍ \z Ö®¼äµÄ²»Í¬ÔÚÓÚµ±×Ö·û´®½áÊø×Ö·ûʱ»»Ðзûʱ \Z »á½«Æä¿´×ö×Ö·û´®½áβƥÅä, ¶ø \z ֻƥÅä×Ö·û´®½áβ¡£

\G ¶ÏÑÔÔÚÖ¸¶¨ÁË$offset ²ÎÊýµÄpreg_match() µ÷ÓÃÖУ¬ ½öÔÚµ±Ç°Æ¥ÅäλÖÃÔÚÆ¥Å俪ʼµãµÄʱºò²ÅÊdzɹ¦µÄ¡£ µ± $offset µÄÖµ²»Îª 0 µÄʱºò£¬ ËüÓë \A ÊDz»Í¬µÄ¡£ Òë×¢£ºÁíÍâÒ»µãÓë \A µÄ²»Í¬Ö®´¦ÔÚÓÚʹÓà preg_match_all() ʱ£¬ ÿ´ÎÆ¥Åä \G Ö»ÊǶÏÑÔÊÇ·ñÊÇÆ¥Åä½á¹ûµÄ¿ªÊ¼Î»Ö㬠¶ø \A ¶ÏÑÔµÄÔòÊÇÆ¥Åä½á¹ûµÄ¿ªÊ¼Î»ÖÃÊÇ·ñÔÚÄ¿±ê×Ö·û´®¿ªÊ¼Î»Öá£

×Ô PHP 4.3.3¿ªÊ¼£¬ \Q ºÍ \E ¿ÉÒÔÓÃÓÚÔÚģʽÖкöÂÔÕýÔò±í´ïʽԪ×Ö·û¡£±ÈÈ磺 \w+\Q.$.\E$ »áÆ¥ÅäÒ»¸ö»ò¶à¸öµ¥´Ê×Ö·û£¬½ô½Ó×ÅÒ»¸öµãºÅ£¬Ò»¸ö$£¬Ò»¸öµãºÅ£¬ ×îºóêÏò×Ö·û´®Ä©Î²¡£

×Ô PHP 5.2.4 ¿ªÊ¼¡£ \K ¿ÉÒÔÓÃÓÚÖØÖÃÆ¥Åä¡£ ±ÈÈ磬 foot\Kbar Æ¥Åä"footbar"¡£ µ«Êǵõ½µÄÆ¥Åä½á¹ûÊÇ "bar"¡£µ«ÊÇ£¬ \K µÄʹÓò»»á¸ÉÔ¤µ½×Ó×éÄÚµÄÄÚÈÝ£¬ ±ÈÈç (foot)\Kbar Æ¥Åä "footbar"£¬µÚÒ»¸ö×Ó×éÄڵĽá¹ûÈÔÈ»»áÊÇ "foo"¡£Òë×¢£º \K ·ÅÔÚ×Ó×éºÍ×Ó×éÍâÃæµÄЧ¹ûÊÇÒ»ÑùµÄ¡£

User Contributed Notes

vea dot git at gmail dot com 12-Jul-2017 12:36
\b BS

$str=""

$str =str_replace("\b", "", $str);
//echo 

$str =str_replace(chr(8), "", $str);
//echo

$str =str_replace("\\b", "", $str);
//echo
error17191 at gmail dot com 21-Sep-2015 09:55
Some escape sequence like the tab character \t won't work inside single quotes '\t', But they work inside double quotes.
Other escape sequences like the backspace character won't work unless you use its ascii codepoint and chr() function i.e. chr(8)
info at maisuma dot jp 17-Jun-2014 01:47
You can use Unicode character escape sequences (tested on PHP 5.3.3 & PCRE 7.8).

<?php
//This source is supposed to be written in UTF-8.
$a='€';
var_dump(preg_match('/\\x{20ac}/u',$a)); //Match!
bluemoehre at gmx dot de 14-Jan-2014 09:06
Using \R in character classes is NOT possible:

var_dump( preg_match('#\R+#',"\n") ); -> int(1)
var_dump( preg_match('#[\R]+#',"\n") ); -> int(0)
collons at ya dot com 05-Oct-2013 01:06
The pattern "/\\A/" may be replaced by "/\\\A/" in order to match a "\A" string. Any other escaped "\" looks to work fine so you can use "/\\S/", for instance, to match a "\S" string.
mike at eastghost dot com 28-Mar-2012 04:25
"line break" is ill-defined:

 -- Windows uses CR+LF (\r\n)
 -- Linux LF (\n)
 -- OSX CR (\r)

Little-known special character:
\R in preg_* matches all three.

preg_match( '/^\R$/', "match\nany\\n\rline\r\nending\r" ); // match any line endings
grigor at the domain gatchev.info 22-Jun-2011 10:59
As \v matches both single char line ends (CR, LF) and double char (CR+LF, LF+CR), it is not a fixed length atom (eg. is not allowed in lookbehind assertions).