开发者

Python regex for unenclosed quotation marks [duplicate]

This question already has answers here开发者_StackOverflow社区: Closed 11 years ago.

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜