我有一个字符串,可以具有不同的参数和不同数量的参数。我正在尝试获取参数名称和参数值。一个例子是:
FIRST_PARAM=1^SECOND_PARAM=2^THIRD_PARAM=3^FOURTH_PARAM=4^
我能够通过现有的包获取参数值,但这是我遇到的困难。到目前为止,我所做的是:
param_count_ := length(params_) - length(replace(params_,'^',null));
DBMS_OUTPUT.PUT_LINE('Param Count: '||param_count_);
WHILE(index_ <= param_count_)
LOOP
param_value_ := client_sys.Get_Key_Reference_Value(params_,index_);
IF(index_ = 1) THEN
param_name_ := NVL(SUBSTR(params_, 0, INSTR(params_, '=')-1), params_);
ELSE
param_name_ := SUBSTR(params_,
INSTR(params_, '^',1,index_) + 1,
INSTR(params_, '=',1,index_+1) - INSTR(params_, '^',1,index_) - 1);
END IF;
DBMS_OUTPUT.PUT_LINE('Reference Name: '||param_name_);
DBMS_OUTPUT.PUT_LINE('Reference Value: '||param_value_);
index_ := index_+1;
END LOOP;
这将导致以下输出:
Param Count: 4
Reference Name: FIRST_PARAM
Reference Value: 1
Reference Name: THIRD_PARAM
Reference Value: 2
Reference Name: FOURTH_PARAM
Reference Value: 3
Reference Name:
Reference Value: 4
似乎不合时宜。我该如何解决?
答案 0 :(得分:1)
这是满足您要求的一种方法:
BEGIN
FOR cur IN (SELECT REGEXP_SUBSTR(PARAM_, '[^= ]+', 1, 1) AS "KEY",
REGEXP_SUBSTR(PARAM_, '[^= ^]+', 1, 2) AS "VALUE"
FROM
(SELECT REGEXP_SUBSTR(str, '[^\^ ]+', 1, LEVEL) AS PARAM_
FROM (SELECT 'FIRST_PARAM=1^SECOND_PARAM=2^THIRD_PARAM=3^FOURTH_PARAM=4^'
AS STR FROM DUAL) T
CONNECT BY REGEXP_SUBSTR(str, '[^\^ ]+', 1, LEVEL) IS NOT NULL
))
LOOP
DBMS_OUTPUT.PUT_LINE('Reference Name: '|| cur.KEY ||
' <=> '|| 'Reference Value: '|| cur.VALUE);
END LOOP;
END;
输出:
Reference Name: FIRST_PARAM <=> Reference Value: 1
Reference Name: SECOND_PARAM <=> Reference Value: 2
Reference Name: THIRD_PARAM <=> Reference Value: 3
Reference Name: FOURTH_PARAM <=> Reference Value: 4