MySQL REGEXP word boundaries [[:<:]] [[:>:]] and double quotes

前端 未结 4 1478
自闭症患者
自闭症患者 2020-12-01 21:27

I\'m trying to match some whole-word-expressions with the MySQL REGEXP function. There is a problem, when there are double quotes involved.

The MySQL documentation s

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 21:38

    In MySQL 8 and above

    Adding to Oleksiy Muzalyev's answer

    https://dev.mysql.com/doc/refman/8.0/en/regexp.html#regexp-compatibility

    In MySQL 8.04 and above, you have to use:

    \bword\b
    

    Where \b represents the ICU variant for word boundary. The previous Spencer library uses [[:<:]] to represent a word boundary.

    When actually using this as part of a query, I've had to escape the escape character \ so my query actually looked like

    SELECT * FROM table WHERE field RLIKE '\\bterm\\b'
    

    When querying from PHP, use SINGLE quotes to do the same thing

    $sql = 'SELECT * FROM table WHERE field RLIKE ?';
    $args = ['\\bterm\\b'];
    ...
    

提交回复
热议问题