Hive 从字符串中提取数值

Hive extract numeric value from a string

我有一个 table 作为:

column1                         column2 
The first value is 200 gb        need to restart (2 times)
The 2nd value is 700 gb          need (optional) to restart (10 times)

我正在尝试从 table 中获取数值。预期输出为

column1_numeric      column2_numeric 
200                   2
700                   10

对于第 1 列:我尝试使用以下方法获取数据:regexp_replace(column1, '[^0-9]', '') as column1_numeric; 但这不适用于第二行和 returns 2700

对于第 2 列:我正在尝试:regexp_replace(regexp_extract(column2,'\((.*?)\)'), '[^0-9]', '') as column2_numeric 但这也不适用于第二行和 returns 空值

有什么建议吗?

如果您使用下面的正则表达式,它将适用于两列并且仅从字符串中提取数字。

var numberPattern = /\d+/g;
'The first value is 200 gb'.match( numberPattern ).join('') // 200

'need to restart (2 times)'.match( numberPattern ).join('') // 2

请试试这个
select REGEXP_REPLACE('The first value is 200 gb','[^0-9]','')
我得到的结果是 200
试试这个:
select REGEXP_REPLACE(substring('The 2nd value is 200 gb',-6),'[^0-9]','')

从字符串中提取最后一个数值 '(\d+)([^0-9]*)$':

select 
      regexp_extract(column1,'(\d+)([^0-9]*)$',1) as column1_numeric,
      regexp_extract(column2,'(\d+)([^0-9]*)$',1) as column2_numeric
   ...

它提取

column1_numeric      column2_numeric 
200                   2
700                   10

您还可以使用 \D 而不是 [^0-9](不是数字),它更短一些:

'(\d+)(\D*)$'