首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Server 2016如何在T中使用简单的正则表达式?

Server 2016如何在T中使用简单的正则表达式?
EN

Stack Overflow用户
提问于 2017-08-15 17:46:08
回答 2查看 13.3K关注 0票数 0

我有一个列,列的名字格式如下:“姓,名”

  • 只允许上层案件
  • 逗号后的空格可选

我想使用一个正则表达式,比如:A+,A+,但是我不知道如何在T中这样做。在甲骨文中,我会使用REGEXP_LIKE,Server 2016有类似的地方吗?

我需要这样的东西:

代码语言:javascript
运行
复制
UPDATE table 
SET is_correct_format = 'YES'
WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+');
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-15 18:27:34

首先,区分大小写取决于DB的排序规则,尽管使用LIKE可以指定大小写比较。用那个..。下面是一些布尔逻辑来处理您所述的情况。不过,如果发现一些虚假输入,则可能需要添加额外的子句。

代码语言:javascript
运行
复制
declare @table table (Person varchar(64), is_correct_format varchar(3) default 'NO')
insert into @table (Person)
values
('LowerCase, Here'),
('CORRECTLY, FORMATTED'),
('CORRECTLY,FORMATTEDTWO'),
('ONLY FIRST UPPER, LowerLast'),
('WEGOT, FormaNUMB3RStted'),
('NoComma Formatted'),
('CORRECTLY, TWOCOMMA, A'),
(',COMMA FIRST'),
('COMMA LAST,'),
('SPACE BEFORE COMMA , GOOD'),
(' SPACE AT BEGINNING, GOOD')


update @table
set is_correct_format = 'YES'
where 
        Person not like '%[^A-Z, ]%'                                                    --check for non characters, excluding comma and spaces
    and len(replace(Person,' ','')) = len(replace(replace(Person,' ',''),',','')) + 1   --make sure there is only one comma
    and charindex(',',Person) <> 1                                                      --make sure the comma isn't at the beginning
    and charindex(',',Person) <> len(Person)                                            --make sure the comma isn't at the end
    and substring(Person,charindex(',',Person) - 1,1) <> ' '                            --make sure there isn't a space before comma
    and left(Person,1) <> ' '                                                           --check preceeding spaces
    and UPPER(Person) = Person collate Latin1_General_CS_AS                             --check collation for CI default (only upper cases)

select * from @table
票数 2
EN

Stack Overflow用户

发布于 2017-08-15 18:27:44

tsql的等价物可能是这样的。我不能保证这个解决方案的效率。

代码语言:javascript
运行
复制
declare @table as table(name varchar(20), is_Correct_format varchar(5))
insert into @table(name) Values
('Smith, Jon')
,('se7en, six')
,('Billy bob')


UPDATE @table 
SET is_correct_format = 'YES'
WHERE
replace(name, ', ', ',x')
     like (replicate('[a-z]', charindex(',', name) - 1)
         + ','
         + replicate('[a-z]', len(name) - charindex(',', name)) )


select * from @table

可选的空间很难解决,所以因为它是一个法律字符,所以当它存在的时候,我只是用另一个法律字符来代替它。

TSQL没有在regex中提供*或+的“重复模式”,因此您必须在搜索模式中多次计算字符和构造模式。

我在逗号处拆分字符串,计算前后的alpha,并构建一个匹配的搜索模式。

笨重但可行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45698496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档