你如何在Python之间拆分所有内容?包括“s”本身?
例如,我想将print "HELLO"
之类的内容拆分为['print ']
因为我将所有内容拆分为引号,包括引号本身。
其他例子:
1)print "Hello", "World!"
=> ['print ', ', ']
2)if "one" == "one": print "one is one"
=> ['if ', ' == ', ': print ']
感谢任何帮助。
答案 0 :(得分:3)
使用re.split()
:
In [3]: re.split('".*?"', 'print "HELLO"')
Out[3]: ['print ', '']
In [4]: re.split('".*?"', '"Goodbye", "Farewell", and "Amen"')
Out[4]: ['', ', ', ', and ', '']
请注意使用.*?
,非贪婪的全部消费模式。
答案 1 :(得分:3)
您可以将正则表达式'"[^"]*"'
用于re.split
:
示例:
txt='''\
print "HELLO"
print "Hello", "World!"
if "one" == "one": print "one is one"
'''
width=len(max(txt.splitlines(), key=len))
for line in txt.splitlines():
print '{:{width}}=>{}'.format(line, re.split(r'"[^"]*"', line), width=width+1)
打印:
print "HELLO" =>['print ', '']
print "Hello", "World!" =>['print ', ', ', '']
if "one" == "one": print "one is one" =>['if ', ' == ', ': print ', '']
答案 2 :(得分:0)
>>> import re
>>> text = 'print "Hello"'
>>> re.sub(r'".*?"', r'', text)
'print '
帮助OP的单引号错误:
>>> import re
>>> text = 'print \'hello\''
>>> re.sub(r'\'.*?\'', r'', text)
'print '