regex in plpgsql function

Started by culley harrelsonabout 22 years ago3 messagesgeneral
Jump to latest
#1culley harrelson
culley@fastmail.fm

I am using 7.4.1

Basically I am trying to use a regex that will match:

john smith
john r smith
john r. smith

When I use:

siteadmin=# select 'john r. smith' ~ '^\\w+\\s+\\w?\\.?\\s?\\w+$';
?column?
----------
t
(1 row)

all is well but:

CREATE OR REPLACE FUNCTION f_tmp(TEXT) RETURNS BOOLEAN AS ' --{{{

BEGIN
RETURN $1 ~ ''^\\w+\\s+\\w?\\.?\\s?\\w+$'';
END;
' language 'plpgsql';

siteadmin=# select f_tmp('john r. smith');
f_tmp
-------
f
(1 row)

makes the same regex seem to behave differently. What am I doing wrong?

culley

#2Bruno Wolff III
bruno@wolff.to
In reply to: culley harrelson (#1)
Re: regex in plpgsql function

On Thu, Jan 22, 2004 at 09:36:14 -0800,
Culley Harrelson <culley@fastmail.fm> wrote:

makes the same regex seem to behave differently. What am I doing wrong?

I think you have to double the \s again in the function so that there
are 4 \s for each \ in the RE.

#3culley harrelson
culley@fastmail.fm
In reply to: Bruno Wolff III (#2)
Re: regex in plpgsql function

I think you have to double the \s again in the function so that there
are 4 \s for each \ in the RE.

Thanks. This worked:

RETURN $1 ~
''^[[:alnum:]_]+[[:space:]]+[[:alnum:]_]?\\.?[[:space:]]?[[:alnum:]_]+$'';