I have a long column of numbers, some are made of just digits (15.75) but the large ones appear with "M" for million, for example 10.1M, 4.23M, or 2.567M (three decimal places max). How can I use regex to write these large numbers with digits only? Can I use TextMate which seems to have regex Find and Replace, or do I need a different tool?
Asked
Active
Viewed 21 times
0
-
Do you want PCRE regex? – Martin Mar 28 '20 at 20:19
-
1Oh boy. TextMate and emacs both allow you to do this, but you will need to capture the things that look like `\d+(.?\d*)?(M)?` and look at the second capture group in your language of choice. TextMate will only allow you to find them. You will want to look at Python's regexs as these are the easier to get into. – Alex Petrosyan Mar 28 '20 at 20:21
-
I don't know of any native *replacement* regex functions so please share what languages you're using, and that language will have a `regex_replace` type of function in it. – Martin Mar 28 '20 at 20:21
-
If the value contains an `M` then simply remove the M and multiply by 1,000,000 – Martin Mar 28 '20 at 20:26
-
Do you wish to replace `10.1M` with `10100000`? With `10,100,000`? – Cary Swoveland Mar 28 '20 at 20:56
-
Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Mar 28 '20 at 21:20
-
You may be able to match `(?<!\d)(\d+)(?:\.(\d+)M)`. The third match of `"for example 10.1M, 4.23M, or 2.567M (three"` would be `2.567M` and the contents of capture groups `$1` and `$2` would be `2` and `567`, respectively. In pseudo-code you could then replace the match with `"#{$1}#{2}#{"0"*(6-$2.size)}" #=> "2567000"`. In this example, `$2.size #=> "567".size #=> 3`, and `"0"*3` would mean `"000"`. [Demo](https://regex101.com/r/AiJpfc/4/). – Cary Swoveland Mar 28 '20 at 21:23
-
@Martin and others: many thanks for your help. I was hoping for a quick and easy way, but from your comments I gather that TextMate is inadequate. I will write an appropriate function in Python then. Thanks to everyone for your input. – Adam Mar 29 '20 at 03:59