How to split a string containing a numeric value into three parts in PostgreSQL?

I want to split a given string, which may contain a numeric value, using regexp_matches(). It must identify the first occurrence of a numeric value containing an optional character and optional decimal numbers. Inappropriate parts must also be returned - both the first and last positions of the array.

Some exemplary input and expected output values:

'hello+111123.454545world' -> {hello,+111123.454545,world}
'he-lo+111123.454545world' -> {he-lo,+111123.454545,world}
'hel123.5lo+111123.454545world' -> {hel,123.5,lo+111123.454545world}
'hello+111123.454545world' -> {hello,+111123.454545,world}
'hello+111123.454545world' -> {hello,+111123.454545,world}
'1111.15' -> {"",1111.15,""}
'-.234' -> {"",-.234,""}
'hello-.234' -> {hello,-.234,""}

I am having problems with the first part of the compliance group in the following expression represented by "TODO". It must correspond to all that cannot be identified as a numerical value.

select regexp_matches('input', '(TODO)((?:\+|-)?(?:\d*(?:(?:\.)?\d+)))(.*)')

, '(TODO)', . ( ). , , , .

+4
4
regexp_matches(input, '(^.*?)([+-]?\d*\.?\d+)(.*$)') AS result_arr
  • 1- : (^.*?)
    ^. - *? .
    be the negation of the regular expression in the second match group, . , - , , .

  • 2- : ([+-]?\d*?\.?\d+)
    . , [+-] , (?:\+|-).
    . ( .)
    \d* @maraca.

  • : (.*$)
    $. .

SQL Fiddle .

+2

, , : /'(.*?)([+\-]?[0-9\.]+)(.*?)'/g

: https://regex101.com/r/nF5qV7/1

+1

:

(.*?)((?:\+|-)?(?:\d*(?:(?:\.)?\d+)))(.*)
0

, , :

(.*?)([+-]?[0-9]*\.[0-9]+)(.*)

Or with an extra point, matches 1.,. 7, +.8, -4, 0.0, 42, ...

(.*?)([+-]?(?:\.[0-9]+|[0-9]+\.?[0-9]*))(.*)
0
source

Source: https://habr.com/ru/post/1599686/


All Articles