Hazard
Hazard

Reputation: 3

How to extract text with quotations and without quotations?

For example if the text is 'SetVariable "a" "b" "c"' I need to extract both text with quotation ["a","b","c"] and SetVariable. I found the regex to extract text within quotation marks. I need help on how to extract the rest of text

Upvotes: 0

Views: 54

Answers (2)

user107511
user107511

Reputation: 822

It seems like you want to parse a command line. shlex module can do this for you.

import shlex
x = 'SetVariable "a" "b" "c"'
shlex.split(x)

Result:

['SetVariable', 'a', 'b', 'c']

Upvotes: 0

Anand Gautam
Anand Gautam

Reputation: 2101

A simple version:

x = 'SetVariable "a" "b" "c"'
s = x.split()
spl = [i for i in s]
print(spl)

Output:

['SetVariable', '"a"', '"b"', '"c"']

Using Comprehension:

spl = [i for i in x.split()]
print(spl)

Output:

['SetVariable', '"a"', '"b"', '"c"']

Upvotes: 1

Related Questions