SQL Regex last character search not working

后端 未结 2 2023
野的像风
野的像风 2021-01-23 15:19

I\'m using regex to find specific search but the last separator getting ignore.

Must search for |49213[A-Z]| but searches for |49213[A-Z]

SELECT * FROM t         


        
相关标签:
2条回答
  • 2021-01-23 15:56

    Aha. That is rather subtle.

    1. \ escapes certain characters that have special meaning.
    2. But it does not seem to do so for | ("or") or . ("any byte"), etc.
    3. So, \| is the same as |.
    4. But the regexp parser does not like having either side of "or" being empty. (I suspect this is a "bug"). Hence the error message.

    https://dev.mysql.com/doc/refman/5.7/en/regexp.html says

    To use a literal instance of a special character in a regular expression, precede it by two backslash () characters. The MySQL parser interprets one of the backslashes, and the regular expression library interprets the other. For example, to match the string 1+2 that contains the special + character, only the last of the following regular expressions is the correct one:

    The best fix seems to be [|] or \\| instead of \| when you want the pipe character.

    Someday, the REGEXP parser in MySQL will be upgraded to PCRE as in MariaDB. Then a lot more features will come, and this 'bug' may go away.

    0 讨论(0)
  • 2021-01-23 16:03

    Why are you using | in the pattern? Why the +?

    SELECT * FROM table WHERE (data REGEXP '\|49213[A-Z]\|')
    

    If you want multiple:

    SELECT * FROM table WHERE (data REGEXP '\|49213[A-Z]+\|')
    

    or:

    SELECT * FROM table WHERE (data REGEXP '[|]49213[A-Z][|]')
    
    0 讨论(0)
提交回复
热议问题