如果我想从句子的数字中分割字符串:"It was amazing in 2016"
我用:
re.split('\s*((?=\d+))
out: 'It was amazing in', '2016'
现在我想做相反的事情,所以如果一个句子以数字开头,那么后跟一个字符串,如:'2016 was amazing'
我希望结果为:'2016', 'was amazing'
答案 0 :(得分:3)
使用外观可以在两种情况下使用单个正则表达式:
5, 6, 7, 8, 9
<强>代码:强>
\s+(?=\d)|(?<=\d)\s+
RegEx分手:
>>> str = "It was amazing in 2016"
>>> re.split(r'\s+(?=\d)|(?<=\d)\s+', str)
['It was amazing in', '2016']
>>> str = "2016 was amazing"
>>> re.split(r'\s+(?=\d)|(?<=\d)\s+', str)
['2016', 'was amazing']
- 匹配1个或更多空格\s+
- Lookbehind断言下一个字符是数字(?=\d)
- 或|
- Lookbehind声明前一个字符是数字(?<=\d)
- 匹配1个或更多空格答案 1 :(得分:0)
在我看来,RegEx对于该任务来说太过分了,所以除非你已经在你的程序上使用RegEx或者它需要(赋值或其他),我建议使用一些字符串操作函数来获得你想要的东西。 / p>
def ends_in_digit(my_string):
separated = my_string.rsplit(maxsplit=1)
return separated if separated[-1].isdigit() else False
def starts_with_digit(my_string):
separated = my_string.split(maxsplit=1)
return separated if separated[0].isdigit() else False
答案 2 :(得分:0)
另一种轻松拆分为数字和非数字的方法是与\d+|\D+
正则表达式匹配。它会产生带有前导/尾随空格的块,但是它们可以很容易地被删除(或者如果这不重要的话就保留它们):
import re
r = re.compile(r'\d+|\D+')
ss = [ 'It was amazing in 2016', '2016 was amazing']
for s in ss:
print(r.findall(s)) # to get chunks with leading/trailing whitespace
print([x.strip() for x in r.findall(s)]) # no leading/trailing whitespace
请参阅Python demo。