Reputation: 79
I'm trying to extract alpha numeric characters from strings that also have some info within square brackets.
Ex:
I want the output to be:
I tried using negative look ahead, extracting patterns before'[' (works only for a few cases).
Upvotes: 0
Views: 1328
Reputation: 313
You could try to use a Regex for this, but I think that making your own function for this would work well.
function getText(bracketedText) {
let idx = 0
let newIdx = 0
let str = ''
while (newIdx !== -1) {
newIdx = bracketedText.indexOf('[', idx)
if (newIdx < 0) {
str += bracketedText.slice(idx, bracketedText.length)
} else {
str += bracketedText.slice(idx, newIdx)
}
idx = bracketedText.indexOf(']', newIdx + 1) + 1
}
return str
}
This should be fairly efficient at stripping out anything in brackets.
Upvotes: 0
Reputation: 74605
Per my comment, and after a quick look at the fine presto manual, could you:
SELECT regexp_replace('[second sentence][important] some more sample text', ' *\[.*?\] *');
Regex is any number of spaces, then sq bracket then any number of any char up to the next sq bracket, then sq bracket, then any number of spaces.
I dug the function out of the manual (no access to presto/never used), I presume by providing only two arguments it implicitly replaces matches with nothing
Upvotes: 1