PostgreSQL中的regexp_replace Unicode [英] regexp_replace Unicode in PostgreSQL
问题描述
如何在PostgreSQL中为Unicode进行regexp_replace
How to regexp_replace for Unicode in PostgreSQL
我阅读了 http://www.regular-expressions.info/unicode.html
select regexp_replace('s4y8sds', '\\p{Number}', '')
或
select regexp_replace('s4y8sds', '\\p{N}', '')
但不起作用
我在PHP中使用以下代码
i have this following code work in PHP
preg_replace( "/[^\p{Ll}|\p{Lm}|\p{Lo}|\p{Lt}|\p{Lu}|\p{Zs}]/u", "", "string1212.," );
请帮助我
推荐答案
对于普通数字,请使用 digit
字符类作为 [[:digit:]]
或简写 \d
:
For ordinary numbers use digit
character class as [[:digit:]]
or shorthand \d
:
SELECT regexp_replace('s4y8sds', $$\d+$$, '', 'g');
结果:
regexp_replace
----------------
sysds
(1 row)
对于其他数字(例如¼)并不是那么简单,更确切地说,就像文档说它是ctype(语言环境)相关的:
For other numbers (for example ¼) is not that simple, more precisely as documentation says it's ctype (locale) dependent:
在方括号表达式中,用
[:和:]括起来的字符类的名称表示该
类中所有字符的列表。标准字符类名称是:字母,字母,空格,cntrl,
位,图形,小写,打印,点,空格,大写,xdigit。这些代表
代表ctype中定义的字符类。语言环境可以提供其他
。
Within a bracket expression, the name of a character class enclosed in [: and :] stands for the list of all characters belonging to that class. Standard character class names are: alnum, alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper, xdigit. These stand for the character classes defined in ctype. A locale can provide others.
但是您可以使用内部PL / Perl过程语言并使用所需的Unicode字符类 \p {}
编写服务器端函数:
However you could use internal PL/Perl procedural language and write server-side function with wanted Unicode characters classes \p{}
:
CREATE OR REPLACE FUNCTION removeNumbersUnicode(text)
RETURNS text AS $$
$s = $_[0];
$s =~ s/\p{N}//g;
return $s;
$$ LANGUAGE plperl;
检查第41章(来自doc)以获取有关如何编写此类函数的更多信息。
Check Chapter 41 from doc for more info how to write such functions.
这篇关于PostgreSQL中的regexp_replace Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!