我有以下字符串
random_string = '12:58 PM word \n12:20PM person \n12:39PM'
I am doing the following:
re.split(r'[\n]+\d',random_string)
And I get:
['12:58 PM word ', '2:20PM person ', '2:39PM']
But I want:
['12:58 PM word ', '12:20PM person ', '12:39PM']
知道怎么做吗?
答案 0 :(得分:1)
在正则表达式中使用前瞻以避免在换行符后匹配数字:
>>> random_string = '12:58 PM word \n12:20PM person \n12:39PM'
>>> re.split(r'\n+(?=\d)', random_string)
['12:58 PM word ', '12:20PM person ', '12:39PM']
\n+(?=\d)
将在换行符上拆分。\n
放入角色等级。答案 1 :(得分:1)
来自https://docs.python.org/2/library/re.html:
(?=...)
匹配如果...
匹配下一个,但不消耗任何字符串。这称为先行断言。例如,Isaac (?=Asimov)
只有在'Isaac '
之后才匹配'Asimov'
。
所以,在你的情况下:
>>> re.split(r'[\n]+(?=\d)', '12:58 PM word \n12:20PM person \n12:39PM')
['12:58 PM word ', '12:20PM person ', '12:39PM']