如何使用 PostgreSQL 和 Umlauts 的不区分大小写的模式匹配?

发布于 2024-09-07 11:30:43 字数 470 浏览 4 评论 0原文

当字符串包含非 ASCII 字符(如德语变音符号)时,我试图让 PostgreSQL 8.4.3 与其 ~* 运算符进行不区分大小写的模式匹配。数据库、终端和其他所有内容都配置为使用 UTF-8。

简而言之,问题是这样的:

SELECT 'Ö' ~* 'ö';      -- false

还有其他可行的变体:

SELECT 'Ö' ILIKE 'ö';     -- true
SELECT 'Ö' ~* '[Öö]';     -- true
SELECT LOWER('Ö') ~* 'ö'; -- true

这些替代方案都没有让我特别高兴。 ILIKE 不使用正则表达式。 [Öö] 涉及重写搜索词。 LOWER() 可能是最好的解决方法,但我真的很想让 ~* 运算符像预期的那样工作。

提前致谢。

I'm trying to get PostgreSQL 8.4.3 to do case insensitive pattern matching with its ~* operator when the strings contain non-ASCII characters like German umlauts. The database, terminal, and everything else is configured to use UTF-8.

Here's the problem in a nutshell:

SELECT 'Ö' ~* 'ö';      -- false

There are other variants which do work:

SELECT 'Ö' ILIKE 'ö';     -- true
SELECT 'Ö' ~* '[Öö]';     -- true
SELECT LOWER('Ö') ~* 'ö'; -- true

None of these alternatives make me especially happy. ILIKE doesn't use regular expressions. [Öö] involves rewriting the search term. LOWER() is probably the best workaround, but I'd really like to get the ~* operator working like it's supposed to.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-09-14 11:30:43

这是 9.0 之前的 PostgreSQL 版本中的错误。
它位于 9.0 变更日志中: http://www. postgresql.org/docs/9.0/static/release-9-0.html#AEN99075

这是我使用 Ubuntu 在 9.0 beta2 中进行的测试:

SELECT 'Ö' ~* 'ö';
 ?column? 
----------
 t
(1 row)

This is a bug in PostgreSQL versions prior to 9.0.
It's in the 9.0 changelog: http://www.postgresql.org/docs/9.0/static/release-9-0.html#AEN99075

Here is my test in 9.0 beta2 using Ubuntu:

SELECT 'Ö' ~* 'ö';
 ?column? 
----------
 t
(1 row)
如痴如狂 2024-09-14 11:30:43

我对这个查询的理解是正确的:

SELECT 'Ö' ~* 'ö'; -- true

但我确实在 OS X 10.5.8 上使用了版本 9.0beta2 并进行了这些设置:

CREATE DATABASE test
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'nl_NL.UTF-8'
       LC_CTYPE = 'nl_NL.UTF-8'
       CONNECTION LIMIT = -1;

编辑:版本 8.3.7 上的结果相同。看来您的编码有问题。

I get true with this query:

SELECT 'Ö' ~* 'ö'; -- true

But I did use version 9.0beta2 at OS X 10.5.8 with these settings:

CREATE DATABASE test
  WITH OWNER = postgres
       ENCODING = 'UTF8'
       TABLESPACE = pg_default
       LC_COLLATE = 'nl_NL.UTF-8'
       LC_CTYPE = 'nl_NL.UTF-8'
       CONNECTION LIMIT = -1;

Edit: Same result on version 8.3.7. Looks like you have a problem with the encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文