For Example in the string:
create or replace procedure SYSPROC.Q2SPJ016(
p_SLNO in INTEGER,
p_HIRE_DATE in DATE,
p_BUSINESS_GROUP in VARCHAR(25)
)
Desired Output:
p_SLNO in INTEGER,
p_HIRE_DATE in DATE,
p_BUSINESS_GROUP in VARCHAR(25)
For Example in the string:
create or replace procedure SYSPROC.Q2SPJ016(
p_SLNO in INTEGER,
p_HIRE_DATE in DATE,
p_BUSINESS_GROUP in VARCHAR(25)
)
Desired Output:
p_SLNO in INTEGER,
p_HIRE_DATE in DATE,
p_BUSINESS_GROUP in VARCHAR(25)
Shot Answer : Regex alone can not solve this problem
Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion. You can use regular expressions and a stack to do this
Here is how you may do it
//function that matches outer most paranthesis
function matchOuterBracket(str) {
let re = /\(|\)/gi
let stack = []
let startI = -1,
endI = -1
while (match = re.exec(str)) {
if (match[0] == '(') {
stack.push(match.index)
} else {
startI = stack.pop()
}
if (stack.length == 0) {
endI = match.index
break
}
}
if (startI == undefined ||
startI == -1 ||
endI == -1 ||
endI == undefined) {
return ""
}
return str.slice(startI +1 , endI)
}
//testing
var str = `create or replace procedure SYSPROC.Q2SPJ016(
p_SLNO in INTEGER,
p_HIRE_DATE in DATE,
p_BUSINESS_GROUP in VARCHAR(25)
)`
console.log(matchOuterBracket(str))