2

How to regexp_replace for Unicode in PostgreSQL

i read this http://www.regular-expressions.info/unicode.html

select regexp_replace('s4y8sds', '\\p{Number}', '')

or

select regexp_replace('s4y8sds', '\\p{N}', '')

but not work

i have this following code work in PHP

preg_replace( "/[^\p{Ll}|\p{Lm}|\p{Lo}|\p{Lt}|\p{Lu}|\p{Zs}]/u", "", "string1212.," );

Please help me

0

1 Answer 1

3

For ordinary numbers use digit character class as [[:digit:]] or shorthand \d:

SELECT regexp_replace('s4y8sds', $$\d+$$, '', 'g');

Result:

 regexp_replace
----------------
 sysds
(1 row)

For other numbers (for example ¼) is not that simple, more precisely as documentation says it's ctype (locale) dependent:

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.

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;

Check Chapter 41 from doc for more info how to write such functions.

Sign up to request clarification or add additional context in comments.

4 Comments

owh, i c. So i must use plperl, and can't directly use \p{N} in regexp_replace ? Thanks
@Thessa: Yes, \p{N} syntax isn't supported with regexp_replace. There are more procedural languages, even PL/PHP if you want, but PL/Perl seems to best candidate for such issue.
I'm getting error : createlang: language installation failed: ERROR: could not load library "C:/Pro gram Files (x86)/PostgreSQL/9.0/lib/plperl.dll": The specified module could not be found.
i fix the language installation issue. Thanks so much Grzegorz Szpetkowski

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.