python - 如何匹配异构的空白字符序列?
问题描述
例如:"\t\t \v"
和" \f"
应该匹配。"\t\t\t"
," "
并且"\f\f\f"
不应该匹配。
所以基本上,我想排除第一个捕获的字符,类似于 this (\s)\1*[^\S\1]+\s*
。但这不起作用,因为我们不能将捕获的组放入[^]
.
我怎样才能做到这一点?
解决方案
我不确定我是否正确满足了您的要求,但您可以尝试使用否定的 Lookahead:
(\s)\1*(?!\1)\s+
这样的事情对你有用吗?
这是一个 Python 示例:
regex = r"(\s)\1*(?!\1)\s+"
inputs = ["\t\t \v", "\f", "\t\t\t", " ", "\f\f\f", "\f \f"]
for s in inputs:
if re.match(regex, s):
print "Found a match."
else:
print ("No matches!")
输出:
Found a match.
No matches!
No matches!
No matches!
No matches!
Found a match.
如果不是,我不确定您为什么希望\f
成为匹配项。如果这不是错误,你能澄清一下吗?
推荐阅读
- c# - 发布动态创建的 HTML 页面以形成操作
- php - 如何从 mysql db 中检索数据,而不是使用 php 中的 while 循环?
- android - Android - spotShadowAlpha 仅适用于线性布局
- ios - 当领域上的对象无效时该怎么办
- node.js - 在 AWS-EC2 实例上使用 Nginx 启动节点应用程序的问题
- php - 从会话中删除 Laravel
- python - pandas 中是否有一个选项来查看列中的值是否小于一行中的另一列,然后随着时间的推移而变化?
- javascript - localStorage 删除一个值并保留现有值
- r - 如何在列中搜索多个值,如果找到,则在同一行中填充另一列?
- windows - 在 IIS 上使用 .srf 文件部署网站时出现问题