Removing characters from regex groups

Purpose: I want to convert a number from the format "10.234.56" to "10234.56"

Using this simple approach, we almost get

/([\d\.]+),(\d\d)/ => '\1.\2' 

The problem is that the first group of the match (of course) still contains '.' character.

So the questions are:

  • Is there any way to exclude a character from a group?
  • How would you solve this with a single regex

(I know this is a trivial problem if you don't use one regex)

+4
source share
3 answers

There seems to be no way to do this, and I set off on an easier route

As in practice, the numbers are somewhat limited when I finish using /(\d*).?(\d*).?(\d+),(\d\d)/=> '\ 1 \ 2 \ 3. \ 4 '

0
source

Why use regexp? Just replace the '.' on '' and then ',' on '.' .

+1
source

Instead, replace the dots ( Rubular ):

 /\.(?=[\d.]+,\d\d)/ => '' 

If you want to remove only points that seem like thousands of separators ( Rubular ):

 /\.(?=\d{3}(?:\.\d{3})*,\d\d)/ 
0
source

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


All Articles