0

I have table with comments like this

'Payment amount: 11000,50 from 144232'

'Payment amount: 13 450,20 from 144232'

Sometimes white spaces occurs in number, because people typing this manually. I need to get first numbers like 11000,50 and 13 450,20 from example.

I'm trying to use regexp_replace('Payment amount: 11000,20 from 144232','([a-zA-Z:\s])','','g') and get result '11000,20144232', but I need only '11000,00'

How can I improve regex or what function I need to use to get this numbers?

2
  • you want to extract only that value? Commented Jun 29, 2020 at 12:07
  • I need to get first number with two numbers after comma Commented Jun 29, 2020 at 12:25

4 Answers 4

1

To get the first number in the string with a comma and 2 digits after it, and spaces can occur between the digits due to typing:

^[a-zA-Z:\s]*(\d[\s\d]*,\s*\d\s*\d)

Explanation

  • ^ Start of string
  • [a-zA-Z:\s]* Match 0+ times any of the listed chars in the character class
  • ( Capture group 1 (this will contain the value)
    • \d Match a digit
    • \d[\s\d]* Match 0+ times a whitespace char or digit
    • ,\s*\d\s*\d Match a comma and 2 digits with optional whitespace chars (Add \M if there can be no more word character following)
  • ) Close group 1

Regex demo | Postgresql demo


A broader match could be to match 0+ times any char except a digit \D* instead of using [a-zA-Z:\s]*

Regex demo

Sign up to request clarification or add additional context in comments.

Comments

0

This works for your examples, with or without decimals:

^[a-zA-Z:\s]+([\d\s]*(,\d{1,2})?)

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

Comments

0

I would do it like this:

SELECT (regexp_match(
           'Payment amount: 11000,00 from 144232',
           '[[:digit:]][[:digit:] ]*(?:,[[:digit:]]+)')
       )[1];

 regexp_match 
--------------
 11000,00
(1 row)

2 Comments

Sometimes happens that 1st value like '11 000,00' with whitespace between hundredths. How can I solve this case?
I changed the expression to cover that. But don't expect any expression like that to magically always do the right thing with arbitrary data.
0

Somewhat simpler but less strict, ([+\-]?\d[\d ,\.]+).*

SELECT (regexp_matches('Payment amount: 11,000.00 from 144232', '([+\-]?\d[\d ,\.]+).*'))[1];

Result: 11,000.00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.