Python regex for unenclosed quotation marks [duplicate]
Possible Duplicate:
Split a string by spaces — preserving quoted substrings — in Python
Given the following string:
term1 term2 "the second term has spaces" term3 bad term 4
What regex will give me this list:
["term1", "term2", "the second term has spaces", "term3", "bad", "term", "4"]
For your simple example, this works fine:
import re
quotestring = 'term1 term2 "the second term has spaces" term3 bad term 4'
# uses a lookahead and lookbehind to check for quoted strings
stringlist = re.findall(r'((?<=\").+(?=\")|\w+)', quotestring)
print(stringlist) # works on Python 2 or 3
Or, from the linked post:
import shlex
quotestring = 'term1 term2 "the second term has spaces" term3 bad term 4'
stringlist = shlex.split(quotestring)
print(stringlist)
精彩评论