sql - PostgreSQL 字符串搜索部分模式删除无关字符

标签 sql regex string postgresql

寻找一个允许灵活搜索的简单 SQL (PostgreSQL) 正则表达式或类似解决方案(可能是 soundex)。因此在搜索过程中省略了破折号、空格等。作为搜索的一部分,只在表中搜索原始字符。:

目前正在使用:

SELECT * FROM Productions WHERE part_no ~* '%search_term%'

如果用户键入 UTR-1,它无法调出存储在数据库中的 UTR1 或 UTR 1。

但是当 part_no 有破折号并且用户省略了这个字符(反之亦然)时,匹配不会发生

EXAMPLE 搜索部分 UTR-1 应该会找到下面的所有匹配项。

UTR1 
UTR --1 
UTR  1

任何建议...

最佳答案

您可能会发现 postrgesql 中的官方内置(至少从 8.3 开始)全文搜索功能值得一看:

http://www.postgresql.org/docs/8.3/static/textsearch.html

例如:

It is possible for the parser to produce overlapping tokens from the 
same of text. 

As an example, a hyphenated word will be reported both as the entire word 
and as each component: 

SELECT alias, description, token FROM ts_debug('foo-bar-beta1');

      alias      |               description                |     token     
-----------------+------------------------------------------+---------------
 numhword        | Hyphenated word, letters and digits      | foo-bar-beta1
 hword_asciipart | Hyphenated word part, all ASCII          | foo
 blank           | Space symbols                            | -
 hword_asciipart | Hyphenated word part, all ASCII          | bar
 blank           | Space symbols                            | -
 hword_numpart   | Hyphenated word part, letters and digits | beta1

关于sql - PostgreSQL 字符串搜索部分模式删除无关字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2510732/

相关文章:

python - Python 的 unittest 库如何匹配通过 -p 参数传递的模式?

ruby 递归正则表达式

java - 正则表达式:将语句与空字符串组合

java - 连接或合并字符串 (JAVA)

sql - 没有反向的查询结果

sql - 检测表中随时间变化的已删除行

c++ - 用于格式化 MAC 地址字符串的 Boost Regex

c++ - 如何在for循环中用多个字符组成一个字符串

mysql - 在 MySQL 中合并来自一张表的两个选择查询

sql - BCP 中遇到意外的 EOF