How to change a comma, by a point, to a decimal number inside a string

Asked

Viewed 648 times

4

Here’s what I was trying to do:

String: ajskjnsjs, eeiisois, 10,98 oismsnsjh;

In this case I wanted to change only the comma of the number by a dot getting the rest of the string the same. I was using the command regexp_replace mysql.

Someone has an idea of a solution?

1 answer

6


PHP:

Use the REGEX:

/(?<=\d),+(?=\d)/

Use the function preg_replace of PHP:

preg_replace('/(?<=\d),+(?=\d)/', '.', $string);

Test it out here.


Mariadb:

Requires support for ?.

Use the REGEX:

(?<=[0-9]),+(?=[0-9])

Use the function regexp_replace on Mariadb:

SELECT REGEXP_REPLACE(coluna, '(?<=[0-9]),+(?=[0-9])', '.')

Alternative without ?:

Use the REGEX:

([0-9]),([0-9])

Use the function regexp_replace in Mariadb (maybe Mysql (UDF)):

SELECT REGEXP_REPLACE(coluna, '([0-9]),([0-9])', '\\1.\\2')
  • version PHP 100% functional. In version Mysql obeyed the following error: #1139 - Got error 'Repetition-Operator operand invalid' from regexp.

  • @Redfenix added another option, no use of ?. I don’t have Mysql, only Mariadb, it’s impossible to test.

  • 100% Working Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.