站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > MySQL 5.1参考手册中文版

Appendix聽G.聽MySQL Regular Expressions - MySQL 5.1参考手册中文版

闄勫綍G锛歁ySQL姝e垯琛ㄨ揪寮

姝e垯琛ㄨ揪寮忔槸涓哄鏉傛悳绱㈡寚瀹氭ā寮忕殑寮哄ぇ鏂瑰紡銆

MySQL閲囩敤Henry Spencer鐨勬鍒欒〃杈惧紡瀹炴柦锛屽叾鐩爣鏄鍚POSIX 1003.2銆傝鍙傝闄勫綍C锛氭劅璋MySQL閲囩敤浜嗘墿灞曠殑鐗堟湰锛屼互鏀寔鍦SQL璇彞涓笌REGEXP鎿嶄綔绗︿竴璧蜂娇鐢ㄧ殑妯″紡鍖归厤鎿嶄綔銆傝鍙傝3.3.4.7鑺傦紝鈥滄ā寮忓尮閰嶁

鍦ㄦ湰闄勫綍涓紝褰掔撼浜嗗湪MySQL涓彲鐢ㄤ簬REGEXP鎿嶄綔鐨勭壒娈婂瓧绗﹀拰缁撴瀯锛屽苟缁欏嚭浜嗕竴浜涚ず渚嬨鏈檮褰曟湭鍖呭惈鍙湪Henry Spencerregex(7)鎵嬪唽椤甸潰涓彂鐜扮殑鎵鏈夌粏鑺傘傝鎵嬪唽椤甸潰鍖呭惈鍦MySQL婧愮爜鍒嗗彂鐗堜腑锛屼綅浜regex鐩綍涓嬬殑regex.7鏂囦欢涓

姝e垯琛ㄨ揪寮忔弿杩颁簡涓缁勫瓧绗︿覆銆傛渶绠鍗曠殑姝e垯琛ㄨ揪寮忔槸涓嶅惈浠讳綍鐗规畩瀛楃鐨勬鍒欒〃杈惧紡銆備緥濡傦紝姝e垯琛ㄨ揪寮hello鍖归厤hello

闈炲钩鍑$殑姝e垯琛ㄨ揪寮忛噰鐢ㄤ簡鐗规畩鐨勭壒瀹氱粨鏋勶紝浠庤屼娇寰楀畠浠兘澶熶笌1涓互涓婄殑瀛楃涓插尮閰嶃備緥濡傦紝姝e垯琛ㄨ揪寮hello|word鍖归厤瀛楃涓hello鎴栧瓧绗︿覆word

浣滀负涓涓洿涓哄鏉傜殑绀轰緥锛屾鍒欒〃杈惧紡B[an]*s鍖归厤涓嬭堪瀛楃涓蹭腑鐨勪换浣曚竴涓細BananasBaaaaasBs浠ュ強浠B寮濮嬨佷互s缁撴潫銆佸苟鍦ㄥ叾涓寘鍚换鎰忔暟鐩an瀛楃鐨勪换浣曞叾浠栧瓧绗︿覆銆

瀵逛簬REGEXP鎿嶄綔绗︼紝姝e垯琛ㄨ揪寮忓彲浠ヤ娇鐢ㄤ换浣曚笅杩扮壒娈婂瓧绗﹀拰缁撴瀯锛

         ^

鍖归厤瀛楃涓茬殑寮濮嬮儴鍒嗐

mysql> SELECT 'fo\nfo' REGEXP '^fo$';                   -> 0
mysql> SELECT 'fofo' REGEXP '^fo';                      -> 1

         $

鍖归厤瀛楃涓茬殑缁撴潫閮ㄥ垎銆

mysql> SELECT 'fo\no' REGEXP '^fo\no$';                 -> 1
mysql> SELECT 'fo\no' REGEXP '^fo$';                    -> 0

         .

鍖归厤浠讳綍瀛楃锛堝寘鎷洖杞﹀拰鏂拌锛夈

mysql> SELECT 'fofo' REGEXP '^f.*$';                    -> 1
mysql> SELECT 'fo\r\nfo' REGEXP '^f.*$';                -> 1

         a*

鍖归厤0鎴栧涓a瀛楃鐨勪换浣曞簭鍒椼

mysql> SELECT 'Ban' REGEXP '^Ba*n';                     -> 1
mysql> SELECT 'Baaan' REGEXP '^Ba*n';                   -> 1
mysql> SELECT 'Bn' REGEXP '^Ba*n';                      -> 1

         a+

鍖归厤1涓垨澶氫釜a瀛楃鐨勪换浣曞簭鍒椼

mysql> SELECT 'Ban' REGEXP '^Ba+n';                     -> 1
mysql> SELECT 'Bn' REGEXP '^Ba+n';                      -> 0

         a?

鍖归厤0涓垨1a瀛楃銆

mysql> SELECT 'Bn' REGEXP '^Ba?n';                      -> 1
mysql> SELECT 'Ban' REGEXP '^Ba?n';                     -> 1
mysql> SELECT 'Baan' REGEXP '^Ba?n';                    -> 0

         de|abc

鍖归厤搴忓垪deabc

mysql> SELECT 'pi' REGEXP 'pi|apa';                     -> 1
mysql> SELECT 'axe' REGEXP 'pi|apa';                    -> 0
mysql> SELECT 'apa' REGEXP 'pi|apa';                    -> 1
mysql> SELECT 'apa' REGEXP '^(pi|apa)$';                -> 1
mysql> SELECT 'pi' REGEXP '^(pi|apa)$';                 -> 1
mysql> SELECT 'pix' REGEXP '^(pi|apa)$';                -> 0

         (abc)*

鍖归厤搴忓垪abc0涓垨澶氫釜瀹炰緥銆

mysql> SELECT 'pi' REGEXP '^(pi)*$';                    -> 1
mysql> SELECT 'pip' REGEXP '^(pi)*$';                   -> 0
mysql> SELECT 'pipi' REGEXP '^(pi)*$';                  -> 1

         {1}, {2,3}

{n}{m,n}绗﹀彿鎻愪緵浜嗙紪鍐欐鍒欒〃杈惧紡鐨勬洿閫氱敤鏂瑰紡锛岃兘澶熷尮閰嶆ā寮忕殑寰堝鍓嶈堪鍘熷瓙锛堟垨閮ㄥ垎锛夈mn鍧囦负鏁存暟銆

o        a*

鍙鍐欏叆涓a{0,}

o        a+

鍙鍐欏叆涓a{1,}

o        a?

鍙鍐欏叆涓a{0,1}

鏇村噯纭湴璁诧紝a{n}an涓疄渚嬪噯纭尮閰嶃a{n,}鍖归厤an涓垨鏇村瀹炰緥銆a{m,n}鍖归厤amn涓疄渚嬶紝鍖呭惈mn

mn蹇呴』浣嶄簬0RE_DUP_MAX锛堥粯璁や负255锛夌殑鑼冨洿鍐咃紝鍖呭惈0RE_DUP_MAX銆傚鏋滃悓鏃剁粰瀹氫簡mnm蹇呴』灏忎簬鎴栫瓑浜n

mysql> SELECT 'abcde' REGEXP 'a[bcd]{2}e';              -> 0
mysql> SELECT 'abcde' REGEXP 'a[bcd]{3}e';              -> 1
mysql> SELECT 'abcde' REGEXP 'a[bcd]{1,10}e';           -> 1

         [a-dX], [^a-dX]

鍖归厤浠讳綍鏄紙鎴栦笉鏄紝濡傛灉浣跨敤^鐨勮瘽锛塧銆乥銆乧銆乨鎴朮鐨勫瓧绗︺備袱涓叾浠栧瓧绗︿箣闂寸殑鈥-鈥濆瓧绗︽瀯鎴愪竴涓寖鍥达紝涓庝粠绗1涓瓧绗﹀紑濮嬪埌绗2涓瓧绗︿箣闂寸殑鎵鏈夊瓧绗﹀尮閰嶃備緥濡傦紝[0-9]鍖归厤浠讳綍鍗佽繘鍒舵暟瀛 銆傝鎯冲寘鍚枃瀛楀瓧绗︹淽鈥濓紝瀹冨繀椤荤揣璺熷湪寮鎷彿鈥淸鈥濅箣鍚庛傝鎯冲寘鍚枃瀛楀瓧绗︹-鈥濓紝瀹冨繀椤婚鍏堟垨鏈鍚庡啓鍏ャ傚浜嶽]瀵瑰唴鏈畾涔変换浣曠壒娈婂惈涔夌殑浠讳綍瀛楃锛屼粎涓庡叾鏈韩鍖归厤銆

mysql> SELECT 'aXbc' REGEXP '[a-dXYZ]';                 -> 1
mysql> SELECT 'aXbc' REGEXP '^[a-dXYZ]$';               -> 0
mysql> SELECT 'aXbc' REGEXP '^[a-dXYZ]+$';              -> 1
mysql> SELECT 'aXbc' REGEXP '^[^a-dXYZ]+$';             -> 0
mysql> SELECT 'gheis' REGEXP '^[^a-dXYZ]+$';            -> 1
mysql> SELECT 'gheisa' REGEXP '^[^a-dXYZ]+$';           -> 0

         [.characters.]

鍦ㄦ嫭鍙疯〃杈惧紡涓紙浣跨敤[]锛夛紝鍖归厤鐢ㄤ簬鏍″鍏冪礌鐨勫瓧绗﹀簭鍒椼傚瓧绗︿负鍗曚釜瀛楃鎴栬濡傛柊琛岀瓑瀛楃鍚嶃傚湪鏂囦欢regexp/cname.h涓紝鍙壘鍒板瓧绗﹀悕绉扮殑瀹屾暣鍒楄〃銆

mysql> SELECT '~' REGEXP '[[.~.]]';                     -> 1
mysql> SELECT '~' REGEXP '[[.tilde.]]';                 -> 1

         [=character_class=]

鍦ㄦ嫭鍙疯〃杈惧紡涓紙浣跨敤[鍜宂锛夛紝[=character_class=]琛ㄧず绛夊悓绫汇傚畠涓庡叿鏈夌浉鍚屾牎瀵瑰肩殑鎵鏈夊瓧绗﹀尮閰嶏紝鍖呮嫭瀹冩湰韬紝渚嬪锛屽鏋渙鍜(+)鍧囨槸绛夊悓绫荤殑鎴愬憳锛岄偅涔圼[=o=]]銆乕[=(+)=]]鍜孾o(+)]鏄悓涔夎瘝銆傜瓑鍚岀被涓嶅緱鐢ㄤ綔鑼冨洿鐨勭鐐广

         [:character_class:]

鍦ㄦ嫭鍙疯〃杈惧紡涓紙浣跨敤[鍜宂锛夛紝[:character_class:]琛ㄧず涓庢湳璇被鐨勬墍鏈夊瓧绗﹀尮閰嶇殑瀛楃绫汇傛爣鍑嗙殑绫诲悕绉版槸锛

alnum

鏂囧瓧鏁板瓧瀛楃

alpha

鏂囧瓧瀛楃

blank

绌虹櫧瀛楃

cntrl

鎺у埗瀛楃

digit

鏁板瓧瀛楃

graph

鍥惧舰瀛楃

lower

灏忓啓鏂囧瓧瀛楃

print

鍥惧舰鎴栫┖鏍煎瓧绗

punct

鏍囩偣瀛楃

space

绌烘牸銆佸埗琛ㄧ銆佹柊琛屻佸拰鍥炶溅

upper

澶у啓鏂囧瓧瀛楃

xdigit

鍗佸叚杩涘埗鏁板瓧瀛楃

瀹冧滑浠h〃鍦ctype(3)鎵嬪唽椤甸潰涓畾涔夌殑瀛楃绫汇鐗瑰畾鍦板尯鍙兘浼氭彁渚涘叾浠栫被鍚嶃傚瓧绗绫讳笉寰楃敤浣滆寖鍥寸殑绔偣銆

mysql> SELECT 'justalnums' REGEXP '[[:alnum:]]+';       -> 1
mysql> SELECT '!!' REGEXP '[[:alnum:]]+';               -> 0

         [[:<:]], [[:>:]]

杩欎簺鏍囪琛ㄧずword杈圭晫銆傚畠浠垎鍒笌word鐨勫紑濮嬪拰缁撴潫鍖归厤銆倃ord鏄竴绯诲垪瀛楀瓧绗︼紝鍏跺墠闈㈠拰鍚庨潰鍧囨病鏈夊瓧瀛楃銆傚瓧瀛楃鏄痑lnum绫讳腑鐨勫瓧姣嶆暟瀛楀瓧绗︽垨涓嬪垝绾(_)銆

mysql> SELECT 'a word a' REGEXP '[[:<:]]word[[:>:]]';   -> 1
mysql> SELECT 'a xword a' REGEXP '[[:<:]]word[[:>:]]';  -> 0

瑕佹兂鍦ㄦ鍒欒〃杈惧紡涓娇鐢ㄧ壒娈婂瓧绗︾殑鏂囧瓧瀹炰緥锛屽簲鍦ㄥ叾鍓嶉潰鍔犱笂2涓弽鏂滄潬鈥\鈥濆瓧绗︺MySQL瑙f瀽绋嬪簭璐熻矗瑙i噴鍏朵腑涓涓紝姝e垯琛ㄨ揪寮忓簱璐熻矗瑙i噴鍙︿竴涓備緥濡傦紝瑕佹兂涓庡寘鍚壒娈婂瓧绗︹+鈥濈殑瀛楃涓测1+2鈥濆尮閰嶏紝鍦ㄤ笅闈㈢殑姝e垯琛ㄨ揪寮忎腑锛屽彧鏈夋渶鍚庝竴涓槸姝g‘鐨勶細

mysql> SELECT '1+2' REGEXP '1+2';                       -> 0
mysql> SELECT '1+2' REGEXP '1\+2';                      -> 0
mysql> SELECT '1+2' REGEXP '1\\+2';                     -> 1

杩欐槸MySQL鍙傝冩墜鍐岀殑缈昏瘧鐗堟湰锛屽叧浜嶮ySQL鍙傝冩墜鍐岋紝璇疯闂dev.mysql.com銆傚師濮嬪弬鑰冩墜鍐屼负鑻辨枃鐗堬紝涓庤嫳鏂囩増鍙傝冩墜鍐岀浉姣旓紝鏈炕璇戠増鍙兘涓嶆槸鏈鏂扮殑銆