I suggest pulling your REGEXEC stuff into a test program. You don't mention any error code being returned so I'd include regerror() in my test program. It can be a life saver.

The test program will allow you to play with the pattern until you find what you want.

You can also look at they have a phone number matching pattern of \b\d{3}-\d{4}.

Theirs \b\d{3}-\d{4}
Yours \d{3}-\d{4}

Maybe all you are missing is the \b?


Gary Monnier

-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx [mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Thomas Garvey
Sent: Wednesday, March 28, 2012 3:31 PM
To: 'Midrange Systems Technical Discussion'
Subject: Anybody experienced with regular expressions and UDFs?


I'm trying to develop a User Defined Function that will test a given value against a regular expression pattern. I've found code online by Scott Klement that utilizes the regcomp (Compile Regular Expression) and regexec (Execute Compiled Regular Expression) APIs. I've put this into an RPGLE module and created a service program, then registered the UDF using the CREATE FUNCTION command in SQL. Then I use the scalar function to test values in a character field of a table.

For example...

FROM strTable
WHERE regexpsrv(str, '\d{3}-\d{4}') = 1

The function gets passed the test value (str), and the regular expression pattern (inside the single quotes), and a value of 0 or 1 is returned (1 if the test value contains the regular expression pattern, 0 if not).

The problem is, it ALWAYS returns a zero. My test database has values that should sometimes return a zero, but mostly a 1. Incidentally, the regular expression essentially is looking for 3 digits, followed by a dash, followed by 4 digits (a phone number of the form xxx-xxxx).

I can put the thing in debug and the expression is compiled and returns a zero for the result of the regcomp call. But the call to regexec just always returns a zero, as in 'it doesn't match. The thing is, I can go to a Regular Expression test site (, which allows me to essentially do the same thing (enter a test value and an expression) to test the expression against various values. It tells me the correct results based on the sample data I have. MY UDF just rejects everything.

If anyone can point me in a direction, or tell me what I'm doing wrong, I'd appreciate it very much.

Thanks in advance,

Thomas Garvey

This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at

This thread ...


Return to Archive home page | Return to MIDRANGE.COM home page