python - 拆分具有多个分隔符的字符串
问题描述
我有字符串"open this and close that"
,我想获得"open this and"
and "close that"
。这是我最好的尝试:
>>>print( re.split(r'[ ](?=(open|close)+)', "open this and close that") )
['open this and', 'close', 'close that']
我正在使用 Python 3.4。
Python 中具有多个分隔符的拆分字符串替换了触发器。我需要它们,脚本必须知道我是要关灯还是开灯,而不仅仅是哪个灯。
解决方案
如果您知道您使用的是什么字母,您可以简单地使用分组。
import re
line = re.sub(r"(open this and) (close that)", r"\1\t\2", "open this and close that")
print (line)
推荐阅读
- django - Django如何查询树结构
- postgresql - Clob(长度 1048576)DB2 到 PostgreSQL 的最佳数据类型?
- android - Config.locale 已弃用
- r - R数据帧内成对观察之间的成对差异
- c# - 将文本框的值传递给datagrid WPF C#中特定列的第一个空行?
- javascript - Firefox:captureVisibleTab 似乎挂起
- javascript - 在 Imgbb 中使用 Java http post 时出现错误 400
- php - 如何停止将 4 到 5 舍入的两位小数(Wordpress)?
- python - 二进制编码的 AVRO 与 JSON 编码的 AVRO 之间的区别
- javascript - 有什么方法可以更改 Chart.js v3 中条形图中标签的字体大小?