I'm trying to validate a string with a like pattern, which only consists of the following characters and I'm getting close to it but can still bypass my curernt expression:
valid characters: numbers 0-9, alphanumeric chars + . =
expression:
'%[^[0-9A-Za-z+._-=\]]%' ESCAPE ''
But with that expression, also strings with square brackets and "-"-characters are counted as correct. If I swap the inner brackets to normal brackets like so
'%[^(0-9A-Za-z+._-=\)]%' ESCAPE ''
I still have the same issue but this time with normal brackets. Test data I'm toying with:
IF OBJECT_ID('TEMPDB..#temp') IS NOT NULL DROP TABLE #temp
CREATE TABLE #temp(
string varchar(max),
expectedResult varchar(max),
result varchar(max)
)
insert #temp(string,expectedResult)
values
('da)ssdas', 'invalid'),
('DFGHJ§$%', 'invalid'),
('+=8rt8\//12=+', 'invalid'),
('[BIGBEAR]', 'invalid'),
('BIG-BEAR', 'invalid'),
('x[BIGBEAR]x', 'invalid'),
('(BIGBEAR)', 'invalid'),
('+=8rt8Ahd12=+', 'valid')
update #temp
set result = CASE WHEN string like '%[^[0-9A-Za-z+._-=\]]%' ESCAPE ''
THEN 'invalid'
ELSE'valid'
END
select * from #temp
result
string expectedResult result
da)ssdas invalid invalid
DFGHJ§$% invalid invalid
+=8rt8\//12=+ invalid invalid
[BIGBEAR] invalid valid
BIG-BEAR invalid valid
x[BIGBEAR]x invalid valid
(BIGBEAR) invalid invalid
+=8rt8Ahd12=+ valid valid