简体   繁体   中英

T-SQL RegEx Matching “One or More” Operator

In MS SQL, is there an operator that allows the matching of one or more character? (I'm curious about whether its implemented explicitly in T-SQL - other solutions are certainly possible, one of which I use in my question example below. . .)

I know in SQL, this could be explicitly implemented to varying degrees of success with the wildcard/like approach:

SELECT *
FROM table
-- finds letters aix and then anything following it
WHERE column LIKE 'aix_x%'

In Python, the '+' operator allows for this:

import re

str = "The rain in Spain falls mainly in the plain!"

#Check if the string contains "ai" followed by 1 or more "x" characters:
# finds 'ai' + one or more letters x
x = re.findall("aix+", str)

print(x)

if (x):
  print("Yes, there is at least one match!")
else:
  print("No match")

Check if the string contains "ai" followed by 1 or more "x" characters: finds 'ai' + one or more letters x

If this is what you want, then:

where str like '%aix%'

does what you want.

If you want an underscore, then an underscore is a wildcard in LIKE expressions. Probably the simplest method in SQL Server is to use a character class:

where str like '%ai[_]x%'

another solution is:

where str like '%ai$_x%' escape '$'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM