Home > front end >  How to extract text with quotations and without quotations?
How to extract text with quotations and without quotations?

Time:03-22

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

CodePudding user response:

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"']

CodePudding user response:

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']
  • Related