Python 无法拆分零宽度 anchor ?

标签 python regex

<分区>

import re

s = 'PythonCookbookListOfContents'

# the first line does not work
print re.split('(?<=[a-z])(?=[A-Z])', s ) 

# second line works well
print re.sub('(?<=[a-z])(?=[A-Z])', ' ', s)

# it should be ['Python', 'Cookbook', 'List', 'Of', 'Contents']

如何使用 Python re 从小写字符和大写字符的边界拆分字符串?

为什么第一行不行,第二行可以用?

最佳答案

根据 re.split :

Note that split will never split a string on an empty pattern match. For example:

>>> re.split('x*', 'foo')
['foo']
>>> re.split("(?m)^$", "foo\n\nbar\n")
['foo\n\nbar\n']

如何使用 re.findall反而? (与其关注分隔符,不如关注你想要获得的项目。)

>>> import re
>>> s = 'PythonCookbookListOfContents'
>>> re.findall('[A-Z][a-z]+', s)
['Python', 'Cookbook', 'List', 'Of', 'Contents']

更新

使用 regex module (替代正则表达式模块,替换 re),您可以在零宽度匹配上拆分:

>>> import regex
>>> s = 'PythonCookbookListOfContents'
>>> regex.split('(?<=[a-z])(?=[A-Z])', s, flags=regex.VERSION1)
['Python', 'Cookbook', 'List', 'Of', 'Contents']

注意:指定 regex.VERSION1 标志以启用零长度匹配行为拆分。

关于Python 无法拆分零宽度 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34317442/

相关文章:

python - pyparsing 不适用于 Windows 文本文件,但适用于 Linux 文本文件

python - 如何使用python读取加密文件夹

python - python keydown 无法同时移动鼠标?

regex - 使用 Regex 和 AltSearch 删除空行

python - 将文件中的行保存到列表

python:快速轻量级持久化

php - PHP-从User-Agent获取确切的iOS版本

php - 此日期格式的 preg 表达式是什么

javascript - 使用 javascript 验证输入的顶级域

regex - 如何grep以数字或空格开头的行