Executing regex in C code

Started by Gabriel Furstenheim Milerudover 8 years ago3 messagesgeneral
Jump to latest
#1Gabriel Furstenheim Milerud
furstenheim@gmail.com

Hi,
I'm trying to execute the equivalent to '~' with regexp from within C code.
According to pg_operator
<https://github.com/postgres/postgres/blob/c7b8998ebbf310a156aa38022555a24d98fdbfb4/src/include/catalog/pg_operator.h&gt;this
operator for type text corresponds to textregexeq:

DATA(insert OID = 641 ( "~" PGNSP PGUID b f f 25 25 16 0 642 textregexeq
regexeqsel regexeqjoinsel ));
DESCR("matches regular expression, case-sensitive");

I've tried to execute it from the C code:

text * s = cstring_to_text("aa");
text * p = cstring_to_text("a");
return DatumGetBool(DirectFunctionCall2(textregexeq, PointerGetDatum(s),
PointerGetDatum(p)));

But I'm getting an error with collate:

ERROR: could not determine which collation to use for regular expression
HINT: Use the COLLATE clause to set the collation explicitly.

Any hints on how to proceed? Of course if I do select 'aa'~'a'; from psql
it works like a charm.

Thanks in advance.

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Gabriel Furstenheim Milerud (#1)
Re: Executing regex in C code

Gabriel Furstenheim Milerud <furstenheim@gmail.com> writes:

I'm trying to execute the equivalent to '~' with regexp from within C code.

text * s = cstring_to_text("aa");
text * p = cstring_to_text("a");
return DatumGetBool(DirectFunctionCall2(textregexeq, PointerGetDatum(s),
PointerGetDatum(p)));

But I'm getting an error with collate:
ERROR: could not determine which collation to use for regular expression
HINT: Use the COLLATE clause to set the collation explicitly.

Yes, you need to use DirectFunctionCall2Coll() if you're trying to invoke
a collation-aware function. It's probably good enough to pass
DEFAULT_COLLATION_OID, although if you're inside a SQL function of
your own, passing down whatever collation was passed to you would
be a better plan.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Gabriel Furstenheim Milerud
furstenheim@gmail.com
In reply to: Tom Lane (#2)
Re: Executing regex in C code

Thanks a lot,
That did the trick

Best regards

On 30 July 2017 at 18:26, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

Gabriel Furstenheim Milerud <furstenheim@gmail.com> writes:

I'm trying to execute the equivalent to '~' with regexp from within C

code.

text * s = cstring_to_text("aa");
text * p = cstring_to_text("a");
return DatumGetBool(DirectFunctionCall2(textregexeq,

PointerGetDatum(s),

PointerGetDatum(p)));

But I'm getting an error with collate:
ERROR: could not determine which collation to use for regular expression
HINT: Use the COLLATE clause to set the collation explicitly.

Yes, you need to use DirectFunctionCall2Coll() if you're trying to invoke
a collation-aware function. It's probably good enough to pass
DEFAULT_COLLATION_OID, although if you're inside a SQL function of
your own, passing down whatever collation was passed to you would
be a better plan.

regards, tom lane