Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a string in Oracle?

How can I parse the value of "request" in the following string in Oracle?

<!-- accountId="123" activity="add" request="add user" -->

The size and the position of the request is random.

like image 805
ed1t Avatar asked Jun 21 '11 21:06

ed1t


3 Answers

You can use regular expressions to find this:

regexp_replace(str, '.*request="([^"]*)".*', '\1')
like image 55
Craig Avatar answered Oct 19 '22 11:10

Craig


Use INSTR(givenstring, stringchartosearch,start_position) to find the position of 'request="' and to find the position of the closing '"'.

Then use substr(string, starting_position, length).

like image 4
Oleg Pavliv Avatar answered Oct 19 '22 11:10

Oleg Pavliv


You'd use a combination of instr and substr

THIS EXAMPLE IS FOR EXAMPLE PURPOSES ONLY. DO NOT USE IT IN PRODUCTION CODE AS IT IS NOT VERY CLEAN.

substr(my_str, 
       -- find request=" then get index of next char.
       instr(my_str, 'request="') + 9, 
       -- This is the second " after request. It does not allow for escapes
       instr(substr(my_str,instr(my_str, 'request="')), 2))
like image 1
cwallenpoole Avatar answered Oct 19 '22 11:10

cwallenpoole