我有一套像这样的法式弦:
text = "Français Langues bantoues Presse écrite Gabon Particularité linguistique"
我想将以大写字母开头的子字符串提取到一个列表中,如下所示:
list = ["Français", "Langues bantoues", "Presse écrite", "Gabon", "Particularité linguistique"]
我确实尝试过类似的东西,但它不需要下面的单词,并且因为法语符号而停止
import re
pattern = "([A-Z][a-z]+)"
text = "Français Langues bantoues Presse écrite Gabon Particularité linguistique"
list = re.findall(pattern, text)
list
输出
['Fran', 'Langues', 'Presse', 'Gabon', 'Particularit']
不幸的是,我没有在论坛上找到解决方案
因为这与特定的Unicode字符处理有关,所以我建议使用PyPi regex module(使用
pip install regex
安装),然后可以使用见online Python demo和regex demo详细信息:
(?!\A)
-字符串开头以外的位置\b
-单词边界(?=\p{Lu})
-一个正向前瞻,要求下一个字符为Unicode大写字母李>注意
map(lambda x: x.strip(), matches)
用于从结果块中去除多余的空白你也可以用
re
来做这件事:请参见this Python demo,但请记住,支持的Unicode大写字母数量因版本而异,使用PyPi regex模块使其更加一致
相关问题 更多 >
编程相关推荐