用多个定界符分割字符串

时间:2020-01-15 06:59:05

标签: python regex split

我有字符串SELECT * FROM nls_database_parameters WHERE PARAMETER='NLS_CHARACTERSET';,我想获取"open this and close that""open this and"。这是我的最佳尝试:

"close that"

我正在使用Python 3.4。

Split string with multiple delimiters in Python替换触发器。我需要它们,脚本必须知道我要关闭还是打开灯,而不仅要知道是哪个灯。

2 个答案:

答案 0 :(得分:1)

如果您知道使用的是字母,则可以简单地使用分组。

import re
line = re.sub(r"(open this and) (close that)", r"\1\t\2", "open this and close that")
print (line)

答案 1 :(得分:0)

假设openclose是您可以使用的关键字:

import regex as re
print(re.search(r'(open.+)(close.+)', "open this and close that").groups())
('open this and ', 'close that')

请注意,您不会在关闭前删除空格字符