【问题标题】:How to extract units from text如何从文本中提取单位
【发布时间】:2020-04-23 08:17:26
【问题描述】:

我有产品名称。它们包含单位:长度(M),质量(KG,G),数量(KS)和体积(L)。

样本数据:

Towels bam.m.3vrs.100%C.80ks
Towels Pet Expert 2x19m,2vrs.
Desserts 165g TK
Desserts AM 100g

单位跟在数字后面。我怎样才能提取它们?

【问题讨论】:

  • 不是真正的“编号后”。 3vrs - “vrs”也是unit吗?还是2x 中的“x”?尝试更精确。
  • @Littlefoot 它们在数字之后.. 系统中只存在这些单位,在此列出(M,KG,G,KS,L)。

标签: sql regex oracle select


【解决方案1】:

您可以使用regexp_replace(),如:

regexp_replace(mycol, '.*\d(m|kg|g|ks|l).*', '\1')

正则表达式搜索一个预定义的单位字符串,前面有一个数字。

Demo on DB Fiddle

with t as (
    select 'Towels bam.m.3vrs.100%C.80ks' mycol from dual
    union all select 'Towels Pet Expert 2x19m,2vrs' from dual
    union all select 'Desserts 165g TK' from dual
    union all select 'Desserts AM 100g' from dual
)
select mycol, regexp_replace(mycol, '.*\d(m|kg|g|ks|l).*', '\1') myunit from t
麦科尔 | MYUNIT :---------------------------- | :----- 毛巾 bam.m.3vrs.100%C.80ks | ks 毛巾宠物专家 2x19m,2vrs |米 甜品 165g TK | G 甜品 AM 100g | G

【讨论】:

  • @Georgy: regexp_replace(mycol, '.*\d(m|kg|g|ks|l).*', '\1', 1, 1, 'i')
猜你喜欢
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多