Oracle 中的正则表达式

Regular Expressions in Oracle

我正在尝试从包含药品目录的 Oracle 数据库中的字符串中提取度量单位。我一直在使用 regexp_substr 从字符串中获取药物浓度

Name col in schema:
CYCLOSPORINE 100MG 30 CAPS
TERBUTALINE 2.5MG 100 TABS 

查询输出:

col 1: CYCLOSPORINE 100MG 30 CAPS, Col 2: 100MG
col 1: TERBUTALINE 2.5MG 100 TABS, Col 2: 2.5MG     



select name, 
regexp_substr(upper(name), 
'(\d*\.*\d+\s*ML|\d*\.*\d+\s*MG|\d*\.*\d+\s*OZ|\d*\.*\d+\s*LB)') 
CONCENTRATION 
from schema.table t 
where t.discontinuedflag=0
and t.restrictioncode <> 0
and t.distributor_id =19

有人知道我如何使用 Oracle 中的 regexp_substr() 从下面的字符串中提取 200MG/mL 吗?

'TESTOSTERONE CYP 200MG/mL 10ML VIAL C3' 

您似乎想要字符串中以数字开头的第一个 "token"。如果是:

select regexp_substr(name || ' ', ' [0-9.]+[^ ]+ ') as concentration

这会将 space 连接到 name 的末尾,因此模式可以以 space 结束,即使它位于 name 的末尾。

到目前为止,这似乎对这些特定示例有效,但就像我在上面的评论中所说的那样,您需要确定数据,因此在没有进行更广泛的测试之前不要过于相信它。我有一个 NDC table 并做了一些检查,似乎集中在描述中,但我没有检查每个代码,所以非常仔细地测试!

正则表达式将括号放在要记住的组周围,从左到右读取,并返回第一个和第二个记住的组。它可以理解为:从行首开始,寻找一个或多个不是数字的字符,后跟一个或多个数字,然后是可选的小数点和零个或多个数字,然后是零个或多个空格,然后是可选度量之一(管道是逻辑或),然后是可选的“/ML”,然后是字符串的其余部分。

SQL> with tbl(drug_name) as (
     select 'CYCLOSPORINE 100MG 30 CAPS' from dual union
     select 'TERBUTALINE 2.5MG 100 TABS' from dual union
     select 'TESTOSTERONE CYP 200MG/mL 10ML VIAL C3' from dual union
     select 'GEMCITABINE 1 GM-26.3 ML VL' from dual union
     select 'NOREPINEPHRINE 1MG/mL 4mL 10 AMP' from dual union
     select 'AMOXI-DROP (50MG)' from dual union
     select 'DARVOCET-N 100 TABLET' from dual union
     select 'ALBON ORAL SUSP 5% 16OZ' from dual
   )
   select drug_name,
   regexp_replace(upper(drug_name), '^\D+(\d+\.?\d*) *((GM|ML|MG|OZ|LB|%)?(/ML)?).*$', '') CONCENTRATION
   from tbl;

DRUG_NAME                              CONCENTRATION
-------------------------------------- ------------------------------
ALBON ORAL SUSP 5% 16OZ                5%
AMOXI-DROP (50MG)                      50MG
CYCLOSPORINE 100MG 30 CAPS             100MG
DARVOCET-N 100 TABLET                  100
GEMCITABINE 1 GM-26.3 ML VL            1GM
NOREPINEPHRINE 1MG/mL 4mL 10 AMP       1MG/ML
TERBUTALINE 2.5MG 100 TABS             2.5MG
TESTOSTERONE CYP 200MG/mL 10ML VIAL C3 200MG/ML

8 rows selected.

SQL>

Notes:- If the regex does not find a match, the DRUG_NAME column will be returned.
      - Since you upshift the drugname, the original 'mL' spelling becomes 'ML'.  
        Technically it's the same thing but you are altering data which may matter to the 
        consumers of the data.
      - Some drug names like the DARVOCET example don't seem to have a measure in the 
        description.  You need to decide if that's ok.
      - The space between the number and measure is removed.

哦,我使用了 REGEXP_REPLACE,因为它允许使用 '\1' shorthand 引用多个保存的组,其中 REGEXP_SUBSTR 不允许(仅 1 个子组)。