输入如下:
Roger Federer is a tennis player. Rafael Nadal Parera is also a tennis player. Another legend player is Novak Djokovic.
我期望得到如下输出:
Roger_Federer is a tennis player. Rafael_Nadal_Parera is also a tennis player. Another legend player is Novak_Djokovic.
我尝试使用正向查找(使用Python re
包)的一个解决方案是:
re.sub(r"(?<=\w)\s([A-Z])", r"_\1", above_string)
但是在这里,由于\w
,我得到了一个输出:
Roger_Federer is a tennis player. Rafael_Nadal_Parera is also a tennis player. Another legend player is_Novak_Djokovic.
当然,我无法使用r"(?<=[A-Z]\w*)\s([A-Z])"
使其工作,因为
error: look-behind requires fixed-width pattern
我必须将这个正则表达式应用于大量(而且非常多样化)的文章,所以我负担不起任何循环或str.replace
暴力。我想知道是否有人能提供一个有效的解决方案
如果不关心所有Unicode大写字母,可以使用
见Python demo。见regex demo详细信息:
\b
-单词边界([A-Z]\w*)
-group1(\1
):一个大写字母和零个或多个单词字符\s+
-一个或多个空格(?=[A-Z])
-与紧跟大写字母的位置匹配的正向前瞻李>如果需要支持所有Unicode字母,建议使用
pip install regex
和见this Python demo。这里,
\p{Lu}
匹配任何Unicode大写字母相关问题 更多 >
编程相关推荐