• 1
  • 2
  • 3
  • 4
  • 5
mysql数据库问题 首 页  »  帮助中心  »  数据库  »  mysql数据库问题
MySQL 的正则表达式
发布日期:2016-4-18 21:4:31

  MySQL 的正则表达式

  介绍

  正则表达式用来描述或者是匹配符合规则的字符串。它的用法与like比较相似,但是它又要比like更加强大,能够实现一些很特殊的规则匹配;正则表达式需要使用REGEXP命令,匹配上返回"1"匹配不上返回"0",REGEXP相当于like '%%'。



  $

  在字符串末尾开始匹配

  mysql> SELECT 'abc' REGEXP 'a$';

  +-------------------+

  | 'abc' REGEXP 'a$' |

  +-------------------+

  | 0 |

  +-------------------+

  1 row in set (0.01 sec)

  mysql> SELECT 'abc' REGEXP 'c$';

  +-------------------+

  | 'abc' REGEXP 'c$' |

  +-------------------+

  | 1 |

  +-------------------+

  1 row in set (0.00 sec)

  ^

  在字符串开始处进行匹配

  mysql> SELECT 'abc' REGEXP '^a';

  +-------------------+

  | 'abc' REGEXP '^a' |

  +-------------------+

  | 1 |

  +-------------------+

  1 row in set (0.00 sec)

  [...]

  匹配括号内的任意单个字符

  mysql> SELECT 'abc' REGEXP '[xyz]';

  +----------------------+

  | 'abc' REGEXP '[xyz]' |

  +----------------------+

  | 0 |

  +----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'abc' REGEXP '[xaz]';

  +----------------------+

  | 'abc' REGEXP '[xaz]' |

  +----------------------+

  | 1 |

  +----------------------+

  1 row in set (0.00 sec)

  .

  匹配任意字符

  mysql> SELECT 'abc' REGEXP '.a';

  +-------------------+

  | 'abc' REGEXP '.a' |

  +-------------------+

  | 0 |

  +-------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'abc' REGEXP '.b';

  +-------------------+

  | 'abc' REGEXP '.b' |

  +-------------------+

  | 1 |

  +-------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'abc' REGEXP '.c';

  +-------------------+

  | 'abc' REGEXP '.c' |

  +-------------------+

  | 1 |

  +-------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'abc' REGEXP 'a.';

  +-------------------+

  | 'abc' REGEXP 'a.' |

  +-------------------+

  | 1 |

  +-------------------+

  1 row in set (0.00 sec)

  [^...]:注意^符合只有在[]内才是取反的意思,在别的地方都是表示开始处匹配

  匹配不出括号没的任意个字符,注意:REGEXP 前的匹配字符作为一个整体

  mysql> SELECT 'a' REGEXP '[^abc]';

  +---------------------+

  | 'a' REGEXP '[^abc]' |

  +---------------------+

  | 0 |

  +---------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'x' REGEXP '[^abc]';

  +---------------------+

  | 'x' REGEXP '[^abc]' |

  +---------------------+

  | 1 |

  +---------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'abc' REGEXP '[^a]';

  +---------------------+

  | 'abc' REGEXP '[^a]' |

  +---------------------+

  | 1 |

  +---------------------+

  1 row in set (0.00 sec)

  'abc'作为一个整体,所以它匹配不了a

  a*

  匹配0个或多个a,包括空字符串。

  mysql> SELECT 'stab' REGEXP '.ta*b';

  +-----------------------+

  | 'stab' REGEXP '.ta*b' |

  +-----------------------+

  | 1 |

  +-----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'stb' REGEXP '.ta*b';

  +----------------------+

  | 'stb' REGEXP '.ta*b' |

  +----------------------+

  | 1 |

  +----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT '' REGEXP 'a*';

  +----------------+

  | '' REGEXP 'a*' |

  +----------------+

  | 1 |

  +----------------+

  1 row in set (0.00 sec)

  a?

  匹配0个或者1个a

  mysql> SELECT 'stb' REGEXP '.ta?b';

  +----------------------+

  | 'stb' REGEXP '.ta?b' |

  +----------------------+

  | 1 |

  +----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'stab' REGEXP '.ta?b';

  +-----------------------+

  | 'stab' REGEXP '.ta?b' |

  +-----------------------+

  | 1 |

  +-----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'staab' REGEXP '.ta?b';

  +------------------------+

  | 'staab' REGEXP '.ta?b' |

  +------------------------+

  | 0 |

  +------------------------+

  1 row in set (0.00 sec)

  a+

  匹配1个或者多个a,但是不包括空字符

  mysql> SELECT 'stab' REGEXP '.ta+b';

  +-----------------------+

  | 'stab' REGEXP '.ta+b' |

  +-----------------------+

  | 1 |

  +-----------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'stb' REGEXP '.ta+b';

  +----------------------+

  | 'stb' REGEXP '.ta+b' |

  +----------------------+

  | 0 |

  +----------------------+

  1 row in set (0.00 sec)

  a1|a2

  匹配a1或者a2,

  mysql> SELECT 'a' REGEXP 'a|b';

  +------------------+

  | 'a' REGEXP 'a|b' |

  +------------------+

  | 1 |

  +------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'b' REGEXP 'a|b';

  +------------------+

  | 'b' REGEXP 'a|b' |

  +------------------+

  | 1 |

  +------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'b' REGEXP '^(a|b)';

  +---------------------+

  | 'b' REGEXP '^(a|b)' |

  +---------------------+

  | 1 |

  +---------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'a' REGEXP '^(a|b)';

  +---------------------+

  | 'a' REGEXP '^(a|b)' |

  +---------------------+

  | 1 |

  +---------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'c' REGEXP '^(a|b)';

  +---------------------+

  | 'c' REGEXP '^(a|b)' |

  +---------------------+

  | 0 |

  +---------------------+

  1 row in set (0.00 sec)

  注意:^符合只有在[]内才是取反的意思,在别的地方都是表示开始处匹配

  a{m,}

  匹配m个或者更多个a

  mysql> SELECT 'auuuuc' REGEXP 'au{3,}c';

  +---------------------------+

  | 'auuuuc' REGEXP 'au{3,}c' |

  +---------------------------+

  | 1 |

  +---------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'auuuuc' REGEXP 'au{4,}c';

  +---------------------------+

  | 'auuuuc' REGEXP 'au{4,}c' |

  +---------------------------+

  | 1 |

  +---------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'auuuuc' REGEXP 'au{5,}c';

  +---------------------------+

  | 'auuuuc' REGEXP 'au{5,}c' |

  +---------------------------+

  | 0 |

  +---------------------------+

  1 row in set (0.00 sec)

  a{m}

  匹配m个a

  mysql> SELECT 'auuuuc' REGEXP 'au{4}c';

  +--------------------------+

  | 'auuuuc' REGEXP 'au{4}c' |

  +--------------------------+

  | 1 |

  +--------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'auuuuc' REGEXP 'au{3}c';

  +--------------------------+

  | 'auuuuc' REGEXP 'au{3}c' |

  +--------------------------+

  | 0 |

  +--------------------------+

  1 row in set (0.00 sec)

  a{m,n}

  匹配m到n个a,包含m和n

  mysql> SELECT 'auuuuc' REGEXP 'au{3,5}c';

  +----------------------------+

  | 'auuuuc' REGEXP 'au{3,5}c' |

  +----------------------------+

  | 1 |

  +----------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'auuuuc' REGEXP 'au{4,5}c';

  +----------------------------+

  | 'auuuuc' REGEXP 'au{4,5}c' |

  +----------------------------+

  | 1 |

  +----------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'auuuuc' REGEXP 'au{5,10}c';

  +-----------------------------+

  | 'auuuuc' REGEXP 'au{5,10}c' |

  +-----------------------------+

  | 0 |

  +-----------------------------+

  1 row in set (0.00 sec)

  a{,n}

  这个暂时还没有测出来,意思应该不是匹配0到n个a,如果是表达0到n个a也可以用{0,n}代替

  (abc)

  将abc作为一个序列匹配,不用括号括起来都是用单个字符去匹配,如果要把多个字符作为一个整体去匹配就需要用到括号,所以括号适合上面的所有情况。

  mysql> SELECT 'xababy' REGEXP 'x(abab)y';

  +----------------------------+

  | 'xababy' REGEXP 'x(abab)y' |

  +----------------------------+

  | 1 |

  +----------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'xababy' REGEXP 'x(ab)*y';

  +---------------------------+

  | 'xababy' REGEXP 'x(ab)*y' |

  +---------------------------+

  | 1 |

  +---------------------------+

  1 row in set (0.00 sec)

  mysql> SELECT 'xababy' REGEXP 'x(ab){1,2}y';

  +-------------------------------+

  | 'xababy' REGEXP 'x(ab){1,2}y' |

  +-------------------------------+

  | 1 |

  +-------------------------------+

  1 row in set (0.00 sec)

  总结

  特别是要注意最后的()的命令,如果不使用()那么所有的都是指单个字符去做匹配,如果需要使用多个字符作为一个整体去匹配就需要将多个字符使用()给括起来