Chris Richardson
Chris Richardson

Reputation: 325

Integers and Floats mixed without separators

So whether I solve this problem with regex, a sed script or a spreadsheet function I am not sure but, at this point I am not quite sure how to attack this issue.

I have a field that represents 3 distinct fields and I need to either divide them up or accurately select each discreet section. My problem is that each value can be either an integer or a float in any order. eg:

[int][int][int] -->432

[int][float][int] -->40.52

[float][int][float] -->1.583.5

I was thinking about writing multiple regex statements until I cover all possible cases but, I was wondering if there is some way to call . a separator and split anything without a separator.

Upvotes: 1

Views: 53

Answers (1)

maraca
maraca

Reputation: 8743

Actually this is not too difficult with the restrictions you gave in the comment (ints from 0 to 9 and floats from 0.0 to 9.9):

(\d\.\d|\d)(\d\.\d|\d)(\d\.\d|\d)

Why this works: regex are greedy and the options are checked from left to right (if the number isn't a float then it has to be an int) and you have the numbers in groups 1 to 3.

https://regex101.com/r/3z7TL3/1

Upvotes: 1

Related Questions