如何使用正则表达式验证 SQL 查询?
通常,一行数据将由电子邮件地址、电话号码、字母/字母数字/数字值等组成,通常,前端验证用于验证电子邮件地址/电话号码等,在本文中,我们将了解如何在编写 SQL 查询时使用正则表达式对其进行验证。因为在后端传递相关数据总是好的。每当数据中出现错误时,正则表达式会快速验证并通知用户是成功还是失败。
第一步:创建数据库
询问:
SELECT * FROM sys.databases WHERE name = 'GEEKSFORGEEKS'
BEGIN
CREATE DATABASE [GEEKSFORGEEKS]
END
第 2 步:在“GEEKSFORGEEKS”数据库下创建“GeekAuthors”表
询问:
use GEEKSFORGEEKS
CREATE TABLE GeekAuthors (
ID INT IDENTITY NOT NULL PRIMARY KEY,
AuthorName VARCHAR(255) NOT NULL,
AuthorEmail VARCHAR(255) NOT NULL,
Age INT,
Skillsets VARCHAR(255),
NumberOfPosts INT
);
GO
由于 AuthorEmail 是一个 varchar 列,因此在“GeekAuthors”表中输入了错误无效的电子邮件地址。我们的任务是找出获得无效电子邮件地址的行。
第 3 步:为了实现这一点,让我们将样本数据放在每一行中
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Rachel','rachel@gmail.com',25,'Java,Python,.Net',5);
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Phoebe','phoebegmailcom',22,'Android,Python,.Net',15);
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Monica','monica@gmailcom',23,'IOS,GO,R',10);
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Joey','joey@.com',24,'Java,Python,GO',5);
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Chandler','chandler@gmail',23,'IOS,GO,R',10);
INSERT INTO GeekAuthors (AuthorName,AuthorEmail,Age,Skillsets,NumberOfPosts)
VALUES ('Ross','ross@gmail.com',24,'Java,Python,GO',5);
第 4 步:现在我们验证电子邮件地址。
我们需要验证输入的电子邮件地址是否正确。为此,我们可以轻松实现
询问:
-- Get all email address
SELECT AuthorEmail FROM GeekAuthors
-- Get ValidEmail Address
SELECT AuthorEmail AS ValidEmail
FROM GeekAuthors
WHERE AuthorEmail LIKE '%_@__%.__%'
AND PATINDEX('%[^a-z,0-9,@,.,_,\-]%', AuthorEmail) = 0
GO
-- use NOT condition in the WHERE clause and select all the invalid emails as well.
SELECT AuthorEmail AS NotValidEmail
FROM GeekAuthors
WHERE NOT AuthorEmail LIKE '%_@__%.__%'
AND PATINDEX('%[^a-z,0-9,@,.,_,\-]%', AuthorEmail) = 0
GO
输出:
第 5 步:用于使用正则表达式从给定值中定位数值。可以通过两种方式找到
我们的输入表达式可能由字母数字值组成。为了找出数值在数据中的位置,我们可以使用下面的格式。
询问:
--pattern to check is: Not of A-Z (both A-Z and a-z(search pattern))
-- in the input string and
--finding the position of the non-matching pattern
-- As we are checking numeric values, it checks for numeric value position
--and it is displaying position of the character
Way 1 :
SELECT 'GeekPremierLeague2022' as ContestName,
PATINDEX('%[^A-Za-z]%', 'GeekPremierLeague2022') as
NumericCharacterPosition;
Way 2 :
SELECT 'GeekPremierLeague2022' as ContestName,
PATINDEX('%[0-9]%', 'GeekPremierLeague2022') as
NumericCharacterPosition;
输出:
如果数字不可用,我们将得到 0 作为答案
输出:
第 6 步:在少数情况下,我们可能只需要从输入字符串中提取字母 (az)。
可以通过组合 2 个函数,即 PATINDEX 和 STUFF 来实现。
- 必须在 PATINDEX 上应用正则表达式。使用上面的查询,我们将获得数字位置
- 通过使用 STUFF函数,我们可以替换所有数值。
询问:
-- At the 18th position, one character is replaced.
We need to remove numeric value.
-- Hence STUFF function is used
1st Param -- Input string
2nd Param -- Start location . As numeric is present
at 18th location, here it is given
3rd Param -- Number of characters to be replaced
4th Param - Replacing value
SELECT STUFF('GeekPremierLeague2022', 18, 1, '' );
-- One character is replaced at the index 18
-- If we want to remove all the numeric above
SELECT STUFF('GeekPremierLeague2022', 18, 4, '' );
-- Four characters are replaced starting from the index 18
输出:
因此,通过同时应用 PATINDEX 和 STUFF,我们可以删除给定字符串中的数值:
询问:
-- Below code will produce only alpha text only
-- Scenario: A column is created in a table to accept
only alphabets. As there is no direct way available,
-- we can use this approach and extract the alphabets alone
DECLARE @inputData NVARCHAR(MAX) = 'GEEKS PREMIER LEAGUE 2022'
--input string
DECLARE @intPosition INT
--get the position of the integer from the input string
SET @intPosition = PATINDEX('%[^A-Za-z]%', @inputData)
-- We can use PATINDEX('%[0-9]%') also
print @intPosition
--run loop until no integer is found in the input string
WHILE @intPosition > 0
BEGIN
--remove the integer from that position
SET @inputData = STUFF(@inputData, @intPosition, 1, '' )
--PRINT @inputData
SET @intPosition = PATINDEX('%[^A-Za-z]%', @inputData )
--Again get the position of the next integer in the input string
--PRINT @intPosition
END
SELECT 'GEEKS PREMIER LEAGUE 2022' as InputString, @inputData AS AlphabetsOnly
输出:
第 7 步:单独获取数值假设当数据馈送发生时,包括员工姓名、电话号码、地址和电子邮件 id,我们可以使用以下方法检索下面的电话号码(即数值)方式。
询问:
DECLARE @inputString VARCHAR(MAX)
DECLARE @validchars VARCHAR(MAX)
SET @inputString = 'GeekAuthor1 123456789 Address1 geek@gmail.com'
--We are going to take continuous set of numbers
and it should not have any spaces also in between
SET @validchars = '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
DECLARE @idx INT
SET @idx = PATINDEX('%'+ @validchars +'%',@inputString )
IF @idx > 0 AND
(@idx = LEN(@inputString )-8
OR PATINDEX(SUBSTRING(@inputString ,@idx+9,1),'[0-9]')=0)
SET @inputString =SUBSTRING(@inputString,
PATINDEX('%'+ @validchars +'%',@inputString ), 9)
ELSE SET @inputString = ''
SELECT @inputString as NumericAlone
因此,正则表达式在许多地方用于验证电子邮件、地址、单独验证 alpha 或单独验证数字。