I try to get all data included in [] (not specific tokens!)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I try to get all data included in (not specific tokens!)



I try to get all data included in (not specific tokens!)
I try with loop but its give only 1 parameter.



Example what I need:


set "x=Stackoverflow [I need it] and [I need this too] and [this too]"

output => I need it, I need this too, this too



Tried to do:


for /f "delims=" %%a in ("%x%") do (
echo %%a
)





What we now need is an example of what you've tried, with an explanation of what happened when you did so. Note that this site is not a free code writing service.
– Compo
Aug 10 at 13:05






I edit my question
– dauzduz TV
Aug 10 at 13:09






So what happened, and how was it contrary to your expectations? Enter For /? at the Command Prompt, read the usage information, paying particular attention to the tokens as well as delims .
– Compo
Aug 10 at 13:11



For /?


tokens


delims





the code looping only 1 time, like code running and giving 1 echo of x variable: output => Stackoverflow
– dauzduz TV
Aug 10 at 13:14






As I told you, read the usage for the command you're using.
– Compo
Aug 10 at 13:17




2 Answers
2



A very simple method:


@echo off
setlocal EnableDelayedExpansion

set "x=Stackoverflow [I need it] and [I need this too] and [this too]"

set "output=" & set "p=%x:]=" & set "output=!output!, !p:*[=!" & set "p=%"

echo output =^> %output:~2%



Output:


output => I need it, I need this too, this too



For an explanation of the method used, you should remove the @echo off line, run the program and carefully review the executed code. If you want further explanations, then review this topic.


@echo off



you could use "tokens=2,4,6,8,10,12 delims=", but it is difficult to post-process the result (removing additional commas/spaces).

The same effect can be reached by preprocessing the string and splitting with a plain for loop. The flag variable takes care of using each second token only. I added a _ in front of the string to correctly process strings that start with a [. set /a "flag=(flag+1) %% 2" alternates the flag variable between 0 and 1.


"tokens=2,4,6,8,10,12 delims="


for


flag


_


[


set /a "flag=(flag+1) %% 2"


flag


0


1


@echo off
setlocal enabledelayedexpansion
set "x=[this] not [that] not [yes] no [in] out"
set "y=_%x:[=","%"
set "y=%y:]=","%"

set flag=0
for %%a in ("%y%") do (
if !flag!==1 set "result=!result!, %%~a"
set /a "flag^=1"
)
if defined result set "result=%result:~2%"
echo output = %result%



Output:


output = this, that, yes, in





set /a "flag=(flag+1) %% 2" can be simplified to set /a "flag^=1". And maybe set result=!result!, "%%~a" so later on each extracted token can be accessed separately by another naked FOR though not sure if that is what the OP wanted. +1
– sst
Aug 10 at 16:33


set /a "flag=(flag+1) %% 2"


set /a "flag^=1"


set result=!result!, "%%~a"


FOR





I give u upvote but I said I dont know where placed in string and not a fact that the exists any 2 tokens, I used other way
– dauzduz TV
Aug 10 at 16:34





@dauzduzTV, Did you test the code at all? It doesn't matter where in the string, appear, and the nice thing about this approach is that it doesn't matter how many of you have in the string be it 1 or 100 it extracts them all. If there is a chance for to not present in the string then it can easily resolved by if "%x%"=="%x:[=%". And what was the other way you used?
– sst
Aug 10 at 17:07





if "%x%"=="%x:[=%"





added a line to care for "no " and implemented @sst's "flag" method (never used OR before with set /a, but yes - very useful)
– Stephan
Aug 10 at 17:53


"


OR


set /a






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard