3

I'm trying to extract both ints and chars from names such as 123A America, 234B Britania. I only want the the number and the attached letter (i.e. 123A) . I'm using regexp_matches(name, '(\d+)(\D)') and it results as:

{123,A}, {456,B}

I thought using concatenation, getting the first element of an array and the second element using two different functions (regexp_matches(name, '(\d+)(\D)' )) [1] || (regexp_matches(name, '(\d+)(\D)' )) [2]

But it generates an error: ERROR: functions and operators can take at most one set argument

How can I get the two element as one string?

1
  • What, in general, does the error message refer to? i.e. Why does, what seems to be a simple value concatenation, cause ERROR: functions and operators can take at most one set argument? Commented Aug 27, 2014 at 17:59

2 Answers 2

1

You don't have to get the two items you're searching for as different sets, just get them as a single set. Remove the )( between \d+ and \D and that will return a set containing the entire string you're looking for.

Results in this - regexp_matches('123A America, 234B Britania', '(\d+\D)' )

This will only find the first match. To get all matching substrings, use the g flag - regexp_matches('123A America, 234B Britania', '(\d+\D)', 'g')

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

Comments

0

good answer by @Scott S however if you can't achieve what you need within one capture group the solution is to write a function, assign the regexp result to a variable and then use it.

CREATE OR REPLACE FUNCTION do_something(_input character varying)
  RETURNS character varying AS
$BODY$
DECLARE
    matches text[];
BEGIN
    matches := regexp_matches(_input, '^([0-9]{1,}_[^_]{1,})_[a-z]{1,}(.*)$','i');
    return substring(matches[1], 0, 24)||matches[2];
END
$BODY$
  LANGUAGE plpgsql;

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.