Question

I am new to PostgreSQL and decided to play around with it a little bit. Thus I want to extract Strings from a Marked-Up text, but I do not get the result I wanted.

To do that I have a simple table named book and a column with a text data type called page.

The data looks something like this:

Simon the Sorcerer is a [[teenager]] transported into a fantasy world as a 
[[Sorcerer (person)|sorcerer]] dressed in a cloak and [[pointy hat]]; his 
cloak and hat are purple in the first game, but change to red for the rest 
of the series (aside from possible magical colour changes in the third game). 
He must use his logic and magical skills to solve puzzles as he progresses 
through the games. 

Now I want to extract each String between the brackets [[ ]]. But how do I do that correctly? Or is that even possible without Plug-ins?

I tried different versions with LIKE which did not produce any results, e.g.:

SELECT * FROM book WHERE page LIKE '[[%]]';
SELECT * FROM book WHERE page LIKE '[[teenager]]';
...

I also played a bit with regex, but I only got the whole dataset back instead of the String between the brackets:

SELECT * from book where page ~ '[^[[]+(?=\]])';

Is there a solution for my problem? Help will be much appreciated!

Was it helpful?

Solution

Use the regexp_matches function:

select regexp_matches(page,'[^[[]+(?=\]])','g') from book
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top