W
W
whats2014-08-01 19:18:16
PostgreSQL
whats, 2014-08-01 19:18:16

Regular expressions (POSIX), how to convert a string to a float?

Good afternoon. There is Postgres, it has POSIX regular expressions.
The task is to convert any string to a floating point number. Possible input strings

$15,500 .22
15,400.22$
15400.22$
$15400.22
15 400.22$
$15 400.22
15.400.2222$
$15.400.22
$ 15,400
15,400 $
15.400 $
$ 15.400

The question is more theoretical than practical. It would be desirable to make 1 regular expression instead of some enclosed.
Also, experts on this issue, please tell me what this design means
regexp_replace('14 32,33р', '([\d\s\w]+)([,.]+)', E'\\1', 'g')

The letter E before the 3rd parameter we are replacing with. I haven't been able to find what that means in the standard. At random, I found that there is also the letter b, which also works.
Well, a couple more questions.
Using the substitution method from the group, I can substitute not the entire line, but also process it?
For example, in the example above, I would like to insert a string into the substitution, but without a space between characters. Is it possible ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yttrium, 2014-08-02
@whats

E - means that this is not just a string to be replaced with, but a regular expression in which there is a link to the first group (the first pair of parentheses) You
can solve it with regular expressions, but why if there is to_number .
Your delimiters are too diverse, it will be necessary to somehow reveal the meaning of the delimiter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question