使用正则表达式从字符串中获取多个值

时间:2013-03-20 15:31:39

标签: regex oracle oracle11g

我有一个表名为“REMARKS”

的表格

创建脚本

Create table temp (id number,remarks varchar2(2000));

插入脚本

Insert into temp values (1,'NAME =GAURAV Amount=981 Phone_number =98932324 Active Flag =Y');
Insert into temp values (2,'NAME =ROHAN Amount=984 Phone_number =98932333  Active Flag =N');

现在,我想从表的备注栏中获取NAME,Amount,phone_number,active_flag的相应值。

我想过使用正则表达式,但我不习惯使用它。 我尝试使用substr和instr从remakrs列中获取名称,但如果我想获取所有四个,我需要编写一个pl sql。我们可以使用正则表达式来实现这一点。

我可以获得输出(CURSOR),如

id  Name    Amount phone_number Active flag
------------------------------------------
1  Gaurav   981    98932324     Y
2  Rohan    984    98932333     N
-------------------------------------------

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你可以使用类似的东西:

SQL> select regexp_replace(remarks, '.*NAME *=([^ ]*).*', '\1') name,
  2         regexp_replace(remarks, '.*Amount *=([^ ]*).*', '\1') amount,
  3         regexp_replace(remarks, '.*Phone_number *=([^ ]*).*', '\1') ph_number,
  4         regexp_replace(remarks, '.*Active Flag *=([^ ]*).*', '\1') flag
  5    from temp;

NAME                 AMOUNT               PH_NUMBER            FLAG
-------------------- -------------------- -------------------- --------------------
GAURAV               981                  98932324             Y
ROHAN                981                  98932324             N
相关问题