Reduce (or change) count of repeated characters

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Oct 18, 2009
104
4
#1
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.
 
Last edited:
#2
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" *"," "
 

samintz

Scott Mintz
May 20, 2008
1,228
11
Solon, OH, USA
#3
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.
 

samintz

Scott Mintz
May 20, 2008
1,228
11
Solon, OH, USA
#4
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.