Home  >  Q&A  >  body text

Equivalent MySQL regular expression, equivalent to the following Python regular expression

python mode => ^(?=.\bABDUL\b)(?=.\bHAI\b.)(?=.\bMANSOOR\b).* $
Need equivalent mysql schema
Can you help me?

P粉496886646P粉496886646409 days ago402

reply all(1)I'll reply

  • P粉314915922

    P粉3149159222023-09-08 00:58:02

    The regex in the question is a weird way to match simple words. It's not clear what the expected input is. Perhaps, input can justify this approach.

    ^(?=.\bABDUL\b)(?=.\bHAI\b.)(?=.\bMANSOOR\b).*$

    This means: the beginning must be any character that is not a word, followed by ABDUL, a non-word character, HAI, a non-word character, MANSOOR, a non-word character or the end of the string.

    ^[^[:alnum:]]ABDUL[^[:alnum:]]HAI[^[:alnum:]]MANSOOR([^[:alnum:]]?.*)?$

    This means: the beginning is not a number or alphanumeric character (alphanumeric character), ABDUL, a non-alphanumeric character, HAI, a non-alphanumeric character, MANSOOR, a non-alphanumeric character or the end of the string.

    I haven't tested it and have no intention of making it exactly the same as the first one, but it should be close enough.

    For anyone who wants to copy this into their code:

    • Matching the first character is unusual and may be a bug in the original regex.
    • (?=...) is a "lookahead assertion" that consumes no characters, the POSIX version does not have it, but may not matter for simple string searches.
    • Both versions should match strings like !ABDUL$HAI)MANSOOR - make sure this is what you want.

    For those who want to understand the regular expressions I use, you can refer to the following link:

    https://dev.mysql.com/doc/refman/8.0/en/regexp.html (POSIX syntax for mysql) and https://docs.python.org/ 3/library/re.html (PCRE for python = Perl compatible syntax)

    reply
    0
  • Cancelreply