I'm 100% new to Regex so I've been floundering about on regex101 trying to figure out how to get my desired output. I am using PostgreSQL to write a query to extract a set of values from the string. Once extracted, I need to convert them to int types and then take the difference between the two values.
A sample of the data I am working with can be found here:
https://regex101.com/r/Twkphj/3 (Each line break is a new record/value of the data.)
https://dbfiddle.uk/ELitHDni
create table t(id int generated always as identity primary key, data text);
insert into t(data)values
('01-08,24-32')
,('38-70')
,('01-25, 27-38')
,('1-6,13-20,25-32')
,('1-4, 7-8, 11-12')
,('1-83,85-112')
,(NULL)
,('NULL')
,('162-169')
,('145-167, 169-214, 217-218, 247-254, 256-257, 382')
,('01-17, 23-27')
,('73-120, 145-192, 217-264, 289-336, 361-408, 433-480, 505-552, 577-624, 649-696, 721-768')
,('1-33, 37-45');
The end goal is to get an output like this:
SELECT
data,
regex(difference of "data" points)
FROM table
| data | difference (inclusive) |
|---|---|
| 01-08,24-32 | 8,9 |
| 145-167, 169-214, 217-218, 247-254, 256-257, 382 | 23, 46, 2, 8, 2, 1 |
From here, I can just use split() if the stakeholder needs to break it down further.
Again I'm not to familiar with regex so regex101 has been great to breakdown and understand why certain "tokens"(?) are used. I think I need to stick to PCRE2 if that matters.
TIA