0

假设我有一个字符串:

mystr = "my name is some good name"

# I want to split at white space except for the part "name is"
expectedoutput = ["my", "name is", "some", "good", "name"]

有无 shlex 怎么办?

我试图做的方式是:

Import shlex
def careful_split(inputstr, donot_split = "name is"):
    strlex = shlex.shlex(inputstr, commenters =?, posit = ?)
    strlex.wordchars = ?
    #and other shlex function

    return list(strlex)
4

1 回答 1

3

您可以使用带有负前瞻的正则表达式。

import re

re.split(r'(?!name)\s+(?!is)', mystr)

更多案例的示例:

>>> mystr = "my name is some good name is hi name"
>>> re.split(r'(?!name)\s+(?!is)', mystr)
['my', 'name is', 'some', 'good', 'name is', 'hi', 'name']

请注意,这不会拆分任何*.name is.*短语。所以“name is not”也不会被拆分。我不确定在这些情况下您想要的行为是什么。

于 2019-10-16T15:50:09.130 回答