以下の文字列を分割したい:
lin=' <abc<hd <> "abc\"d\" ef" '
の中へ
[<abc<hd <>, "abc\"d\" ef"]
ただし、私の問題は、を使用して文字列を分割するときですre.findall(r'"(.*?)"', lin, 0)
。私は得る
['abc', 'ef']
Pythonで文字列を分割する方法を教えてください。
以下の文字列を分割したい:
lin=' <abc<hd <> "abc\"d\" ef" '
の中へ
[<abc<hd <>, "abc\"d\" ef"]
ただし、私の問題は、を使用して文字列を分割するときですre.findall(r'"(.*?)"', lin, 0)
。私は得る
['abc', 'ef']
Pythonで文字列を分割する方法を教えてください。
正規表現を使用したソリューションを次に示します。
import re
line = ' <abc<hd <> "abc\"d\" ef" '
match = list(re.findall(r'(<[^>]+>)\s+("(?:\"|[^"])+")', line)[0])
print(match)
#['<abc<hd <>', '"abc"d" ef"']
それを行う別の方法。
print(re.split(r'\s+(?=")', line.strip())) #split on white space only if followed by a quote.
#['<abc<hd <>', '"abc"d" ef"']
>>> lin=' <abc<hd <> "abc\"d\" ef" '
>>> lin.split('"', 1)
[' <abc<hd <> ', 'abc"d" ef" ']