#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    3
    Rep Power
    0

    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,
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    3
    Rep Power
    0
    The site strips out whitespaces so I will use x for whitespace.

    65662xxxxxx0xxxxxxxx912975xxx10197xxxx1456xpdx_rr_sr_ha_idfoundxThuxJanx25x14:59:38x2018
    Spaces between columns varies
  4. #3
  5. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,830
    Rep Power
    9646
    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?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    3
    Rep Power
    0
    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.
  8. #5
  9. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,830
    Rep Power
    9646
    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})/

IMN logo majestic logo threadwatch logo seochat tools logo