Remove parts of columns values with regex

54 Views Asked by At

I have a dataframe and in 2 columns i have to change values removing all that is not a number or ".". The final result should be only dotted numbers. thank you very much for those of you can help me. I attach how columns areenter image description here

1

There are 1 best solutions below

2
chatnoir On

The regex you're after is:

/[0-9.]+/

Explanation:

[0-9.] Looks for one character that is a digit or .

+ repeats last character unlimited times

let regex = /[0-9.]+/;

console.log("1.0.0".match(regex)); //["1.0.0"]

console.log("1.2.4".match(regex)); //["1.2.4"]

console.log("NaN".match(regex)); //[]

console.log("1.1".match(regex)); //["1.1"]

console.log("6.1.61.1".match(regex)); //["6.1.61.1"]

console.log("4.0.3 and up".match(regex)); //["4.0.3"]

This will match any combination of numbers and . of any character length.

For example: