how to replace all occurrences of ;;;; with ; in a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: how to replace all occurrences of ;;;; with ; in a strin

#31 Post by Squashman » 01 Jun 2012 13:56

MrKnowItAllxx wrote:Call me lazy, but I didn't want to read 2 pages of comments, so I'm posting this solution w/o knowing if it has been suggested already

I will let Dave be the Judge of that. :lol:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to replace all occurrences of ;;;; with ; in a strin

#32 Post by dbenham » 01 Jun 2012 14:38

The MrKnowItAllxx code is nearly identical to what I've already posted, except:

1) His solution handles up to 256 consecutive commas, whereas the other solutions have been expanded to handle the max possible number of 8k commas.

2) His code fails if the string happens to be empty (undefined)

3) His code assumes the string begins and ends with commas. It will give the wrong result if the code begins and/or ends with a value.


Dave Benham

MrKnowItAllxx
Posts: 43
Joined: 20 Mar 2012 20:53

Re: how to replace all occurrences of ;;;; with ; in a strin

#33 Post by MrKnowItAllxx » 01 Jun 2012 22:28

Well my apologies...

I'll just let you guys deal with the problems :|

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: how to replace all occurrences of ;;;; with ; in a strin

#34 Post by jeb » 02 Jun 2012 05:53

dbenham wrote:@jeb - interesting non looping algorithm. But you assume the string does not contain <LF> :wink:

I only hoped that you will not see it. :)

dbenham wrote:I believe I have a bullet-proof variation that avoids using any awkward characters like <LF> and supports all possible string values. Though I'm not convinced I've done enough testing.
...
The code above requires 7 search and replaces, the same as the modified Squashman technique. So I don't think it has any advantage over it, especially if you flatten out the Squashman technique.


I have a search & replace variant with only three replacings.

Code: Select all

@echo off
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"
setlocal enableDelayedExpansion
if defined str (
   rem Suround each comma "," -> "a,b"
   echo #%%~L#
   set ^"str=!str:,=a,,!"

   rem replace ",a,"->"" (empty)
   set ^"str=!str:,a,=!"

   rem replace "a,,"->","
   set ^"str=!str:a,,=,!"
)

set str


timbertuck wrote:i do have a question for jeb, could you explain your code to me, i can understand some of it, but the rest i am lost. i get the LF portion, but the set strings i don't get. thanks!


I will try to explain the new solútion.
I want to use the replacing of a search pattern with nothing to remove all but one comma.
Obviously this can't work with simply replacing ","->"".
So I create first a better pattern
"," -> "a,,"
abc,,,,cba,,,,end
will be expanded to
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
Then each ",a," will be replaced with nothing
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
->
abca,,cbaa,,end

Now, exactly one pattern remains and will be replaced with the rule "a,," -> ","

Hope, I explained it so that someone will understand it :?


jeb

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: how to replace all occurrences of ;;;; with ; in a strin

#35 Post by dbenham » 02 Jun 2012 07:33

Wow, that is impressive jeb. Much more elegant and efficient than I thought was possible. I think we have a winner :!: :D

You forgot to strip any leading and/or trailing commas, but that is easily added.

The method can handle any string with up to 2730 commas. Any more commas and it fails because of the space tripling of each comma before any shrinking. That certainly meets the needs of the OP, and probably meets the needs of nearly every real world batch situation.

Here is jeb's algorithm amended with the leading/trailing comma stripping.

Code: Select all

@echo off
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"
set str
setlocal enableDelayedExpansion

::Comma collapsing begins here
::Limited to at most 2730 commas in a string
if defined str (
  set ^"str=!str:,=a,,!"
  set ^"str=!str:,a,=!"
  set ^"str=!str:a,,=,!"
  if "!str:~0,1!"=="," set "str=!str:~1!"
  if defined str if "!str:~-1!"=="," set "str=!str:~0,-1!"
)
::comma collapsing ends here

set str


Dave Benham

Aacini
Expert
Posts: 1910
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how to replace all occurrences of ;;;; with ; in a strin

#36 Post by Aacini » 02 Jun 2012 19:31

My solution is based on a basic FOR command that use standard Batch delimiters (space, comma, semicolon and equal-sign) to separate the tokens. For this reason, it is necessary to replace the delimiters (excepting comma) and wild-cards:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "str=,,,!Hello Goodbye!,,This&That,value2 opt,,,value3,,value4 opt,,,value5,,,,,"

rem Replace question-mark by Ascii-223
set "str=!str:?=▀!"

rem Replace standard delimiters: space by Ascii-192 and semicolon by Ascii-219:
set "str=!str: =└!"
set "str=!str:;=█!"

rem Collect string tokens and separate they with one comma
set result=
for %%a in (!str!) do set "result=!result!%%a,"

rem Replace back original characters
set "result=!result:▀=?!"
set "result=!result:└= !"
set "result=!result:█=;!"

rem The result have one additional comma at end
echo !result:~0,-1!

This method don't have any limit in the number of commas, but it fail if the string contain asterisk or equal-sign (unless, of course, that these characters be replaced before the FOR).

Antonio

PS - Did you realize that original topic requested to change semicolons, not commas :?: :wink:

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: how to replace all occurrences of ;;;; with ; in a strin

#37 Post by Squashman » 02 Jun 2012 20:45

Aacini wrote:PS - Did you realize that original topic requested to change semicolons, not commas :?: :wink:

The thread title shows semi colons but his initial question showed commas for the example and he even said commas.

Post Reply