Search code examples
mysqlescapingregexp-like

two back slash in SQL procedure with Regular expression


i'm working on a SQL procedure where i came across a Regular expression which contains this piece -

REGEXP '\\s*count\\s*\\('

i do not understand why there are two backslash ? what can be inferred from this code.


Solution

  • Backslash is processed at multiple levels.

    It's the escape prefix in regular expressions: it makes special characters like . and ( be treated literally, and is used to created escape sequences like \s (which means any whitespace character).

    But it's also the escape prefix in string literals, used for things like \n (newline) and \b (backspace). So in order for the regular expression character to get a literal backslash, you need to escape the backslash itself.

    This is common in many programming languages, although a few have "raw string literals" where escape sequences are not processed, specifically to avoid having to double the slashes so much.