描述
如果每个连字符分隔的值都包含在字符串中,则此表达式将匹配字符串。颜色值可以以任何顺序出现在字符串中,因此该表达式将匹配W/Br-b-B/w
和B/w-W/Br-b
... 或包含这些颜色的任何其他组合。
^ # match the start to of the string
(?=.*?(?:^|-)W\/Br(?=-|$)) # require the string to have a w/br
(?=.*?(?:^|-)b(?=-|$)) # require the string to have a b
(?=.*?(?:^|-)B\/w(?=-|$)) # require the string to have a b/w
.* # match the entire string
MySql 并不真正支持环顾四周,因此需要将其分解为一组 where 语句
mysql> SELECT * FROM dog WHERE ( color REGEXP '.*(^|-)W\/Br(-|$)' and color REGEXP '.*(^|-)b(-|$)' and color REGEXP '.*(^|-)B\/w(-|$)' );
+-------+--------+---------+------+------------+---------------------+
| name | owner | species | sex | birth | color |
+-------+--------+---------+------+------------+---------------------+
| Claws | Gwen | cat | m | 1994-03-17 | B-B/w-W/Br-W-Br |
| Buffy | Harold | dog | f | 1989-05-13 | G-R-B, G/R-B-B/W-O |
+-------+--------+---------+------+------------+---------------------+
另请参阅此工作 sqlfiddle: http ://sqlfiddle.com/#!2/943af/1/0
可以在此处找到将正则表达式与 MySql where 语句结合使用:http: //dev.mysql.com/doc/refman/5.1/en/pattern-matching.html