February 15th, 2018, 05:28 PM
-
Replacing whitespace(s) with comma but not all
Hi All,
I am trying to use regex to replace multiple consecutive whitespaces with a single comma. But I do not what to do this for the entire string.
Example Line:
65662 0 912975 10197 1456 pdx_rr_sr_ha_idfound Thu Jan 25 14:59:38 2018
To be...
65662,0,912975,10197,1456,pdx_rr_sr_ha_idfound,Thu Jan 25 14:59:38 2018
Whitespaces may not be consecutively the same number so I can only use just a string of whitespaces.
I need to stop the replacement at the day "Thu" as the date will be in its own column.
It seems to me that ([ ]+){6} would work but it always stops at 1456.
Any help would be appriecated
Thanks,
February 15th, 2018, 05:31 PM
-
The site strips out whitespaces so I will use x for whitespace.
65662xxxxxx0xxxxxxxx912975xxx10197xxxx1456xpdx_rr_sr_ha_idfoundxThuxJanx25x14:59:38x2018
Spaces between columns varies
February 15th, 2018, 05:55 PM
-
It's possible to do this with just a regular expression but it would be nicer if you didn't have to. Can you explain more about how you're processing these lines? Are these fixed-width columns using spaces for padding?
February 15th, 2018, 06:20 PM
-
Hi,
No they are not fixed width as the larger these numbers get the spaces between the columns is reduced and can shift right with one space between.. Bottom line I am trying to turn this into a csv file with timestamp in single column.
February 15th, 2018, 08:36 PM
-
Originally Posted by whattodo2018
the larger these numbers get the spaces between the columns is reduced
That sounds like fixed width. +1 digit means -1 space. Whatever.
The date portion at the end of the string has four spaces, so the pure regex answer is to replace all spaces except for the last four. Which translates to replacing spaces so long as there are at least four more somewhere after.
Code:
/ +(?=\S+(\s\S+){4})/