我将以下文本字符串存储在oracle 11g表中
"MGK8M76HRT Confirmed. You have received Kshs 6,678.00 from Peter 0700123456 on 1/1/2018"
我想使用regexp从文本中提取以下内容
6,678.00
-支付的金额MGK8M76HRT
-唯一的付款交易代码(每次都会更改模式)0700123456
-电话号码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";
答案 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
请按照您的要求进行整理,这似乎不在问题范围之内。
输出: