基于正则表达式拆分字符串 [英] Split string based on regex

查看:39
本文介绍了基于正则表达式拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用大写单词(在 Python 中)分割像 "HELLO there HOW are you" 这样的字符串的最佳方法是什么?

What is the best way to split a string like "HELLO there HOW are YOU" by upper case words (in Python)?

所以我最终会得到一个这样的数组:results = ['HELLO there', 'HOW are', 'YOU']

So I'd end up with an array like such: results = ['HELLO there', 'HOW are', 'YOU']

我已经尝试过:

p = re.compile("\b[A-Z]{2,}\b")
print p.split(page_text)

不过,它似乎不起作用.

It doesn't seem to work, though.

推荐答案

我建议

l = re.compile("(?<!^)\s+(?=[A-Z])(?!.\s)").split(s)

查看这个演示.

Check this demo.

这篇关于基于正则表达式拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆