sqlregexpostgresqlregex-grouppostgres-9.6

regex for numbers with possible digits after dot or comma in text


I need a regex that can filter out numbers that might have digits after the dot from a text.

(?:[A-Z]\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

For some reason PG only returns the first group of my match. Can someone help me please.

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

Solution

  • You could use a combination of substring() and replace(), like:

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

    substring() captures the relevant part of the string, then replace() replaces the , with . if needed.

    Demo on 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        | convertedvalue
    :------- | :-------------
    H 24     | 24            
    24.5     | 24.5          
    24,5     | 24.5          
    test5    | 5             
    test 5.5 | 5.5           
    50.752   | 50.752