It is a bit like using a hammer to make buttered toast, but you can use the regex support in the xml datatype via the pattern facet. Then you determine if the regex matches or not by trying to parse as typed xml, if you jump to the catch block with err 6926 then your regex didn't match (simple type validation err). Here is an example that enforces the regex of 3 decimal digits followed by 6 letters:
create xml schema collection regexTest1 as '<xs:schema targetNamespace="http://example/regexTest"
elementFormDefault="qualified"
xmlns="http://example/regexTest"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="regexTest">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\p{Nd}{3}\p{L}{6}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>'
declare @x xml(dbo.regexTest1), @regexPass bit
begin try
set @x = '<regexTest xmlns="http://example/regexTest">111abcdef</regexTest>'
set @regexPass=1
end try
begin catch
if (error_number()=6926)
begin
set @regexPass=0
end
else begin
declare @errMsg varchar(8000), @errSev int, @errState int
select @errMsg='Regex check was unable to process, native error: ('
+ cast(error_number() as varchar(16))
+ ') - '
+ error_message(),
@errSev=error_severity(),
@errState=error_state()
raiserror(@errMsg, @errSev, @errState)
end
end catch
select @regexPass
Edit - if you go this route, the spec for the regex support in XSD is at http://www.w3.org/TR/xmlschema-2/#dt-regex