代码之家  ›  专栏  ›  技术社区  ›  mpen

PostgreSQL正则表达式单词边界?

  •  55
  • mpen  · 技术社区  · 15 年前

    \b ?

    \bAB\b 但它什么都不匹配 (\W|^)AB(\W|$)

    3 回复  |  直到 15 年前
        1
  •  92
  •   a_horse_with_no_name    7 年前

    PostgreSQL使用 \m \M , \y \Y

    \m   matches only at the beginning of a word
    \M   matches only at the end of a word
    \y   matches only at the beginning or end of a word
    \Y   matches only at a point that is not the beginning or end of a word 
    

    看到了吗 Regular Expression Constraint Escapes 在手册里。

    还有 [[:<:]] [[:>:]] ,匹配单词的开头和结尾。从 the manual :

    括号表达式有两种特殊情况:括号表达式 [:<:]] 是约束,分别在单词的开头和结尾匹配空字符串。一个单词被定义为一系列既不在单词字符前面也不在单词字符后面的单词字符。单词字符是alnum字符(由ctype定义)或下划线。这是一个扩展,与POSIX 1003.2兼容,但不受POSIX 1003.2的指定,在用于其他系统的可移植软件中应谨慎使用。下面描述的约束转义通常更可取(它们不再是标准的,但肯定更容易键入)。

        2
  •  16
  •   MD. Mohiuddin Ahmed    7 年前

    一个简单的例子

    select * from table_name where column ~* '\yAB\y';
    

    这将匹配 AB ab ab - text text ab text AB text-ab-text text AB text ...

    select * from sometable where name ~* '\\yAB\\y';
    

    万一你有 standard_conforming_strings OFF . 注意

    set standard_conforming_strings=on;
    

    select * from table_name where column ~* '\yAB\y'; 应该有用。

        3
  •  5
  •   Pramod Shinde    10 年前

    文本中的精确单词搜索:

    我面临以下问题。

    我想搜索所有的联系人,其中有'首席技术官'作为确切的词在标题,但在结果中得到的结果与标题有'主任',我在使用以下查询

    select * from contacts where title ilike '%cto%';
    

    我还尝试使用通配符“%cto%”周围的whitspaces,它与包含“cto”的文本匹配,得到的结果类似于“vp,cto和manger”,但没有确切标题为“cto”的结果。

    跟踪对我有用

    select * from contacts where title ~* '\\ycto\\y';
    
    ~   Matches regular expression, case sensitive
    ~*  Matches regular expression, case insensitive