【问题标题】:SQL Server : pattern to include parenthesis and exclude all other charactersSQL Server:包含括号并排除所有其他字符的模式
【发布时间】:2014-11-17 07:24:38
【问题描述】:

我在 SQL Server 2005 中对 @ProductDescription_Glossary 执行模式匹配以替换完整的单词。

@GlossaryKeyword 变量包含要匹配和替换的单词。

以下代码替换@ProductDescription_Glossary开头、结尾和结尾处的@GlossaryKeyword,但如果@ProductDescription_Glossary在单词开头或结尾包含括号,则此代码无法成功处理替换

案例 1:此案例工作正常 - Heather 没有被替换为带有单词 Heatherd 的工具提示链接

@GlossaryKeyword = Heather
@ProductDescription_Glossary = Heathered

案例 2:这种情况失败 - 在这种情况下,Heather 被替换,我的要求是,heather 不会被替换,就像案例 1 一样,因此请为我提供所需的模式。

@GlossaryKeyword = Heather
@ProductDescription_Glossary = (Heathered

提前致谢。

注意:@GlossaryKeyword 仅包含字母数字、连字符和 / 字符 i,e (0-9, A-Z, a-z, -, /)

@ProductDescription_Glossary 包含 HTML 标签,默认处理(可能是我服务器上的排序规则设置所致)

代码:

if PATINDEX ('%[^a-z]' +  @GlossaryKeyword  + '[^a-z]%','.' + @ProductDescription_Glossary    + '.') > 0  

BEGIN   

    SET @ProductDescription_Glossary = REPLACE(@ProductDescription_Glossary,@GlossaryKeyword,  '<a target="_blank" id="q_' + CAST (@GlossaryID AS VARCHAR(10)) + '" class="anchor_regular_Mehroon" href="javascript: void(0);">' + @GlossaryKeyword + '</a>')       
    SET @GlossaryToolTip = @GlossaryToolTip + '<div id="a_q_' + CAST (@GlossaryID AS VARCHAR(10)) + '"  class="toolTip_glossary" style="display:none;">' + @GlossaryKeywordDescription + '</div>'
END 

【问题讨论】:

  • 首先,PATINDEX() 不支持 RegEx - 它只进行通配符匹配。第二:我添加了变量定义,执行,它按预期工作..
  • @marc_s,我想要那个(当出现以下情况时,Heathered 不会被替换。SET ProductDescription_Glossary = ',poly(Heathered 红色、紫色、皇家和海军)';SET GlossaryKeyword = 'Heather'

标签: sql-server regex tsql sql-server-2005 pattern-matching


【解决方案1】:

就像()到不一样

select PATINDEX('%[^a-z(]'+ 'Heather' + '[^a-z)]%', '(Heathered red, purple, royal and navy)')

而且它不是正则表达式。
而你想排除( - 不包括。

【讨论】:

  • 不,这没有帮助。我想从模式匹配场景中排除括号。 SET ProductDescription_Glossary = ',poly(希瑟红、紫、皇家和海军)'; SET GlossaryKeyword = 'Heather' 那么必须更换石南花
  • 为什么你对这个问题投了反对票?阅读我之前的评论时,您是否感到困倦?然后在来到 Stack Over Flow 之前小睡一会儿。请参阅我的问题中的 If 条件,当鼠标悬停在此文本上时,我必须将 ProductDescription_Glossary 中的纯文本替换为 href 标记以显示工具提示。(石南花)将被替换为 (希瑟)
  • @ShujaatKagathra 接受现实。希瑟到希瑟没有变化。 “不要被替换”不是英语。 Patindex 不是正则表达式。
  • @balm 1. 我不会将 Heather 更改为 Heather,我必须用锚标签替换 Heather,2. 我喜欢说英语,但我不太好,因为英语不是我的母语语言,3。我的问题以“我正在执行模式匹配......”开始,将我的问题放在这里的原因是我对 PatIndex 了解不多。
  • @balm 请参阅这些页面上的产品描述District DT6000pGildan g500B
猜你喜欢
  • 2011-04-09
  • 2018-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
相关资源
最近更新 更多