Daniel Fortunov
Daniel Fortunov

Reputation: 44323

Parse quoted text from within batch file

I would like to do some simple parsing within a batch file.

Given the input line:

Foo: Lorem Ipsum 'The quick brown fox' Bar

I want to extract the quoted part (without quotes):

The quick brown fox

Using only the standard command-line tools available on Windows XP.

(I had a look at find and findstr but they don't seem quite flexible enough to return only part of a line.)

Upvotes: 3

Views: 2075

Answers (1)

Patrick Cuff
Patrick Cuff

Reputation: 29786

Something like this will work, but only if you have one quoted string per line of input:

@echo OFF
SETLOCAL enableextensions enabledelayedexpansion

set TEXT=Foo: Lorem Ipsum 'The quick brown fox' Bar
@echo %TEXT%

for /f "tokens=2 delims=^'" %%A in ("abc%TEXT%xyz") do (
    set SUBSTR=%%A
)

@echo %SUBSTR%

Output, quoted string in the middle:

Foo: Lorem Ipsum 'The quick brown fox' Bar
The quick brown fox

Output, quoted string in the front:

'The quick brown fox' Bar
The quick brown fox

Output, quoted string at the end:

Foo: Lorem Ipsum 'The quick brown fox'
The quick brown fox

Output, entire string quoted:

'The quick brown fox'
The quick brown fox

Upvotes: 4

Related Questions