mysqlescapingunderscores

sql escape sequence "\_", leetcode problem 1517


Problem: To find valid e-mail.

A valid e-mail has a prefix name and a domain where:

The prefix name is a string that may contain letters (upper or lower case), digits, underscore '_', period '.', and/or dash '-'. The prefix name must start with a letter. The domain is '@leetcode.com'.

when I'm using following expression it's working fine '^[a-zA-Z][a-zA-Z0-9\.\_\-]*@leetcode[\.]com$ but

the second expression is not matching to strings with '_'(underscores) '^[a-zA-Z][a-zA-Z0-9\.\-\_]*@leetcode[\.]com$'

testcase: 'bella-@leetcode.com

using mysql

as per my understanding both of the regex should give same output but the first one is matching with testcase containing underscores and second one is not


Solution

  • You didn't phrase the first requirement correctly:

    The prefix name must start with a letter.

    You used ^[a-z][A-Z] to represent the first letter, but this actually says to match a lowercase letter followed by an uppercase letter. You should have used [a-zA-Z]. Try this version:

    ^[a-zA-Z][a-zA-Z0-9._-]*@leetcode\.com$
    

    Note that I place - last in the character class, as [.-_] actually matches the range of characters between . and _.