-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1517-FindUsersWithValidE-Mails.sql
More file actions
92 lines (85 loc) · 4.95 KB
/
1517-FindUsersWithValidE-Mails.sql
File metadata and controls
92 lines (85 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-- 1517. Find Users With Valid E-Mails
-- Table: Users
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | user_id | int |
-- | name | varchar |
-- | mail | varchar |
-- +---------------+---------+
-- user_id is the primary key (column with unique values) for this table.
-- This table contains information of the users signed up in a website. Some e-mails are invalid.
-- Write a solution to find the users who have valid emails.
-- 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'.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Users table:
-- +---------+-----------+-------------------------+
-- | user_id | name | mail |
-- +---------+-----------+-------------------------+
-- | 1 | Winston | winston@leetcode.com |
-- | 2 | Jonathan | jonathanisgreat |
-- | 3 | Annabelle | bella-@leetcode.com |
-- | 4 | Sally | sally.come@leetcode.com |
-- | 5 | Marwan | quarz#2020@leetcode.com |
-- | 6 | David | david69@gmail.com |
-- | 7 | Shapiro | .shapo@leetcode.com |
-- +---------+-----------+-------------------------+
-- Output:
-- +---------+-----------+-------------------------+
-- | user_id | name | mail |
-- +---------+-----------+-------------------------+
-- | 1 | Winston | winston@leetcode.com |
-- | 3 | Annabelle | bella-@leetcode.com |
-- | 4 | Sally | sally.come@leetcode.com |
-- +---------+-----------+-------------------------+
-- Explanation:
-- The mail of user 2 does not have a domain.
-- The mail of user 5 has the # sign which is not allowed.
-- The mail of user 6 does not have the leetcode domain.
-- The mail of user 7 starts with a period.
-- Create table If Not Exists Users (user_id int, name varchar(30), mail varchar(50))
-- Truncate table Users
-- insert into Users (user_id, name, mail) values ('1', 'Winston', 'winston@leetcode.com')
-- insert into Users (user_id, name, mail) values ('2', 'Jonathan', 'jonathanisgreat')
-- insert into Users (user_id, name, mail) values ('3', 'Annabelle', 'bella-@leetcode.com')
-- insert into Users (user_id, name, mail) values ('4', 'Sally', 'sally.come@leetcode.com')
-- insert into Users (user_id, name, mail) values ('5', 'Marwan', 'quarz#2020@leetcode.com')
-- insert into Users (user_id, name, mail) values ('6', 'David', 'david69@gmail.com')
-- insert into Users (user_id, name, mail) values ('7', 'Shapiro', '.shapo@leetcode.com')
-- SELECT column1, column2, ... FROM table_name WHERE column_name REGEXP 'pattern';
-- ^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
-- $ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
-- . 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用像 '[.\n]' 的模式。
-- [...] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
-- [^...] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
-- p1|p2|p3 匹配 p1 或 p2 或 p3。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。
-- * 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}。
-- + 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
-- {n} n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
-- {n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。
-- 正则表达式匹配的字符类
-- .:匹配任意单个字符。
-- ^:匹配字符串的开始。
-- $:匹配字符串的结束。
-- *:匹配零个或多个前面的元素。
-- +:匹配一个或多个前面的元素。
-- ?:匹配零个或一个前面的元素。
-- [abc]:匹配字符集中的任意一个字符。
-- [^abc]:匹配除了字符集中的任意一个字符以外的字符。
-- [a-z]:匹配范围内的任意一个小写字母。
-- \d:匹配一个数字字符。
-- \w:匹配一个字母数字字符(包括下划线)。
-- \s:匹配一个空白字符。
-- Write your MySQL query statement below
SELECT
user_id,name,mail
FROM
Users
WHERE
-- mail REGEXP '^[A-Za-z]{1}[A-Za-z0-9_\-\.]*@leetcode(\.){1}com$';
mail REGEXP '^[A-Za-z][A-Za-z0-9_\.\-]*@leetcode(\\?com)?\\.com$';