【问题标题】:regual expression for extracting the number [duplicate]用于提取数字的正则表达式[重复]
【发布时间】:2020-03-08 15:13:40
【问题描述】:
我想从这样的表中提取内存量。
+---------------+
| product_name |
+---------------+
| DIMM - 1GB |
| 8MB Cache /NM |
| RAM - 32 MB |
| DIMM - 256MB |
+---------------+
可能有产品名称可以是另一个数字,所以我需要提取“GB”或“MB”附近的数字。需要使用 regexp_replace 函数。我在构建正则表达式时遇到了问题。
【问题讨论】:
标签:
sql
regex
oracle-sqldeveloper
【解决方案1】:
如果你只想捕获字符串中第一次出现的连续数字,那么你可以使用regexp_substr(),如下所示:
regexp_substr(product_name, '\d+')
您可以使用regexp_replace() 更具体。以下表达式捕获后跟MB 或GB 的数字序列:
regexp_replace(product_name, '(^|.*\D)(\d+)\s*[MG]B.*', '\2')
Demo on DB Fiddle:
PRODUCT_NAME | REGEXP_SUBSTR(PRODUCT_NAME,'\D+') | REGEXP_REPLACE(PRODUCT_NAME,'(^|.*\D)(\D+)\S*[MG]B.*','\2')
:------------ | :-------------------------------- | :------------------------------------------------ ---------
DIMM - 1GB | 1 | 1
8MB 缓存/NM | 8 | 8
内存 - 32 MB | 32 | 32