1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Reduce (or change) count of repeated characters

Discussion in 'Suggestions' started by Rick Reinckens, Feb 4, 2015.

  1. Rick Reinckens

    Joined:
    Oct 18, 2009
    Messages:
    96
    Likes Received:
    4
    My employer uses an OLD (DOS) program that outputs CSV. In some cases the output contains quite a bit of spaces within the record, e.g., LEFT MSG FOR {8spaces} ATTY, PHONE 123-456-7890. WILL CALL NEXT WEEK {6spaces} IF NO RESPONSE

    Right now, the only way I see to handle that is with a loop that repeatedly replaces multiple internal spaces until there are no multiple-space sections left.

    I suggest a function that will replace a number range of repeated characters with a specified number of characters, e.g., %@ReplaceMultiChar [min#-max#, with this many, for this character,LT, string]

    LT means "ignore leading" and "ignore trailing"

    E.g., Replace occurrences of 5 to 25 spaces with 3 spaces, strip leading spaces but leave any trailing spaces.
     
    #1 Rick Reinckens, Feb 4, 2015
    Last edited: Feb 4, 2015
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,971
    Likes Received:
    30
    Here's a simple one
    Code:
    v:\> echo %@rereplace[" *"," ",My      dog      has       fleas.]
    My dog has fleas.
    With fancier regular expressions (including quantifiers and BOL/EOL markers), you could do what you described.

    If TPIPE's /perl were working, you could make TPIPE's perl matching greedy and this should also work.

    Code:
    v:\> echo My     dog     has     fleas. | tpipe /replace=4,0,0,0,0,0,0,0,0" *"," "
     
  3. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    There are already built-in functions that strip leading and trailing spaces. See @TRIM, @RTRIM, and @LTRIM. To replace internal spaces use Vince's suggestion of @REREPLACE for single strings or TPIPE for entire files.
     
  4. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,191
    Likes Received:
    11
    By the way, I think Vince's RE syntax should be "\s+" not " *". The star means zero or more. The plus means one or more. I have no idea how it works. I tried passing in a string with no spaces and it didn't replace anything.
     
  5. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,312
    Likes Received:
    39
    @TRIMALL might also be relevant.
     

Share This Page