使用REGEXP ORACLE从文本字符串中选择多个文本

时间:2018-07-20 19:23:38

标签: sql regex oracle11g

我将以下文本字符串存储在oracle 11g表中

"MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018" 

我想使用regexp从文本中提取以下内容

  1. 6,678.00-支付的金额
  2. MGK8M76HRT-唯一的付款交易代码(每次都会更改模式)
  3. 0700123456-电话号码
  4. 1/1/2018-付款日期

我尝试了多种oracle regexp模式来提取文本,但没有成功。任何帮助/想法将不胜感激。

我尝试过:

CONFIRMATION_CODE_PATTERN = "[A-Z0-9]+ Confirmed."; 
PHONE_PATTERN = "07[\\d]{8}"; 
AMOUNT_PATTERN = "Ksh[,|.|\\d]+"; 
DATETIME_PATTERN = "d/M/yy hh:mm a";

1 个答案:

答案 0 :(得分:0)

请注意,在括号表达式中,在Oracle regex中,不能使用regex转义。 [\d]不匹配数字,而是匹配\d个字符。您应该改用[0-9] / [[:digit:]]。接下来,您应该使用capturing groups(...)来包装要执行的模式部分。

您可以使用以下正则表达式:

select regexp_substr('MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018', 
                     'Kshs\s*(\d([,.0-9]*\d)?)', 1, 1, NULL, 1) as Paid from dual
\\
select regexp_substr('MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018', 
                     '(\D|^)(07\d{8})(\D|$)', 1, 1, NULL, 2) as Phone from dual
\\
select regexp_substr('MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018', 
                     '(\S+)\s+Confirmed\.', 1, 1, NULL, 1) as Code from dual
\\
select regexp_substr('MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018', 
                     '\d{1,2}/\d{1,2}/\d{4}') as TrDate from dual

请按照您的要求进行整理,这似乎不在问题范围之内。

输出:

enter image description here