0

我需要一个正则表达式,它可以过滤掉文本中点后可能有数字的数字。

(?:[AZ] \s )(\d+)((.|,)(\d+))?

Text : Expected results : Actual Results
H 24    : 24            : 24
24.5    : 24.5          : 24
24,5    : 24.5          : 24
test5   : 5             : 5
test 5.5: 5.5           : 5
50.752  : 50.752        : 50

出于某种原因,PG 只返回我比赛的第一组。有人能帮助我吗。

select substring('test 5.5', cast('(?:[A-Z]*\s*)(\d+)((.|,)(\d+))?' as character varying)) as convertedvalue
4

2 回答 2

0

您可以使用 and 的组合substring()replace()例如:

select x, replace(substring(x, '(\d+(.|,)*\d*)'), ',', '.') convertedvalue

substring()捕获字符串的相关部分,然后根据需要replace()替换为。,.

DB Fiddle 上的演示

with t(x) as ( values ('H 24'), ('24.5'), ('24,5'), ('test5'), ('test 5.5'), ('50.752') )
select x, replace(substring(x, '(\d+(.|,)*\d*)'), ',', '.') convertedvalue
from t
x | 转换值
:------- | :-------------
H 24 | 24            
24.5 | 24.5          
24,5 | 24.5          
测试5 | 5             
测试 5.5 | 5.5           
50.752 | 50.752        
于 2019-10-24T09:18:39.677 回答
0

我认为你应该能够使用substring()这个:

with t(x) as (
       values ('H 24'),
              ('24.5'),
              ('24,5'), 
              ('test5'),
              ('test 5.5'), 
              ('50.752'),
              ('50.,752'),
              ('H50.12,')
      )
select x, substring(x, '[0-9]+[.,]?[0-9]*') as convertedvalue
from t;

请注意,这将处理诸如 CTE 中最后两个示例的情况。

如果你想用 s 替换','s '.',那么你可以在之后使用一个附加的replace()

于 2019-10-24T11:38:07.083 回答