0

Given an input like one of the following, I need to check if it matches given text, of the format 'ABCD1234':

  • ABCD1### matches ABCD1943, ABCD1300, but does not match ABCD2042 or FOOB3075
  • DCBA[98]### matches DCBA9012, DCBA8899 but not DCBA6211
  • ####1### matches ABCD1848 and BARR1093
  • (ABCD|FOOB|BARR)2### matches ABCD2999, FOOB2533, BARR2222
  • ABCD1234,WXYZ9876 matches only ABCD1234, WXYZ9876
  • DCBA9###,DCBA8### matches the same things as DCBA[98]### above

I'm writing this like a function that has the following structure:

create or replace function modifiedRegEx(pattern text, tester text) 
RETURNS boolean
AS $re$
DECLARE
    isMatch boolean;
BEGIN
    isMatch := TRUE;
    return isMatch;
END;
$re$ LANGUAGE 'plpgsql';

Obviously the above will return TRUE every time. I was wondering if anyone could please suggest some string functions that might be helpful in checking the above? I'm at a loss, even after reading the PostgreSQL string functions docs.

Thank you.

2
  • Just to make sure - are You asking for a translator from Your "modifiedRegEx" to a normal regex? I hope "DCBA[98]### matches DCBA4012, DCBA3899" is a typo. I guess if You just replace "###" with "[0-9]*", "####" with "[A-Z]*" and "," with "|" - it'd do what You asked... Commented Sep 29, 2013 at 6:17
  • Yep it was a typo. And no, I don't want translation to regular regex, I want the function to return TRUE if the input pattern matches the input tester, and false otherwise. Commented Sep 29, 2013 at 7:05

1 Answer 1

2

Your answer is here: http://www.postgresql.org/docs/9.0/static/functions-matching.html

This one returns true:

SELECT 'DCBA9012' similar to 'DCBA[98]___';

or:

SELECT 'DCBA9012' ~ 'DCBA[98]...';

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

Comments

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.