0

Pulling my hair with this. Any help or direction would be very much appreciated.

Column values:

u-165645,c-934846,f-598715,one, appple
c-997556.41,test,u-4404932, testing,two

Trying to extract values that begins with 'c-'. From above examples, the extracted would be:

934846
997556.41
5
  • what language are you using? Commented Dec 8, 2020 at 14:51
  • How many times in a column could the value you are after occur? At least once? Zero or more? Since you mention column, is this data stored in a database? More info is needed for an accurate answer. What have you tried so far? Commented Dec 8, 2020 at 15:39
  • vmp: language is oracle sql. thanks! Commented Dec 8, 2020 at 17:20
  • Gary_w: c-1234 can occur only once but can occur anywhere in the string, delimited by a comma. thanks! Commented Dec 8, 2020 at 17:21
  • Gary_w: c-1234 can occur only once but can occur anywhere in the string, delimited by a comma. for example, if applied to u-16445,c-194846.41,f-99899,one the result should be 194846.41. thanks! Commented Dec 8, 2020 at 17:28

2 Answers 2

0

Give this a try. The WITH clause just sets up your data using a Common Table Expression (CTE). The regular expression uses a captured group to return what follows the "c-" when followed by a comma or the end of the line, so it does not matter where in the string the pattern occurs.

WITH tbl(ID, DATA) AS (
  SELECT 1, 'u-165645,c-934846,f-598715,one, appple' FROM dual UNION ALL
  SELECT 2, 'c-997556.41,test,u-4404932, testing,two' FROM dual
)
SELECT ID,
       REGEXP_SUBSTR(DATA, 'c-(.*?)(,|$)', 1, 1, NULL, 1) c_element
FROM tbl;


        ID C_ELEMENT                              
---------- ---------------------------------------
         1 934846                                 
         2 997556.41                              

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

1 Comment

Bingo! Thanks Gary_W. I am going to play with your solution to understand how you did it.
0

You capture:

c-([*]*)

and replace by

\1

I don't know which language or editor you are using... I've tried this on Notepad++

1 Comment

when applied to u-16445,c-194846.41,f-99899,one the result is u-16445,194846.41,f-99899,one but it should be just 194846.41. thank you!

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.