Re: regexp_replace usage - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: regexp_replace usage
Date
Msg-id 20061002035609.GA42444@winnie.fuhr.org
Whole thread Raw
In response to regexp_replace usage  (chester c young <chestercyoung@yahoo.com>)
Responses Re: regexp_replace usage  (chester c young <chestercyoung@yahoo.com>)
List pgsql-sql
On Fri, Sep 29, 2006 at 02:31:12PM -0700, chester c young wrote:
> column name in table bue has miscapitalized Mc names, eg, 'John Mcneil'
> instead of 'John McNeil'.
> 
> (this should be easy but) how do you construct the update query?
> 
> also, regexp_string( 'Mcneil', 'Mc(.*)', initcap('\\1') ) => 'neil'
> _not_ Neil' - is this correct?

I assume you mean regexp_replace() as indicated in the subject.
You're calling initcap() on the literal value '\\1'; the result,
which is the same string, is then passed to regexp_replace(), so
in effect you're calling

regexp_replace('Mcneil', 'Mc(.*)', '\\1')

Offhand I can't think of a way to do what you want with regexp_replace()
but you could use PL/Perl.  Something like this should work:

CREATE FUNCTION mcfix(text) RETURNS text AS $$ $_[0] =~ s/\bMc([a-z])/Mc\u$1/g; return $_[0];
$$ LANGUAGE plperl IMMUTABLE STRICT;

SELECT mcfix('John Mcneil');   mcfix    
-------------John McNeil
(1 row)

You could use
  $_[0] =~ s/\b(Ma?c)([a-z])/$1\u$2/g;

to change Mcdonald and Macdonald into McDonald and MacDonald,
respectively.  However, since both Macdonald and MacDonald are used,
determining which is correct would be impossible.

-- 
Michael Fuhr


pgsql-sql by date:

Previous
From: Ragnar
Date:
Subject: Re: tree-structured query
Next
From: chester c young
Date:
Subject: Re: regexp_replace usage