Python在点上分割字符串,后跟空格

时间:2019-10-15 15:07:52

标签: python regex string split

我有一个字符串,其中包含点作为行尾的点,其后是一个空格以及属于描述性单词的点,该描述性单词后没有空格,例如带有日期的示例:

let favorites  = '[{"baseLayer":"Swiss Chard","condiment":"Vindaloo Sauce","mixing":"Veggies for Fish Tacos","seasoning":"Sriracha Marinade","shell":"Perfect Flour Tortillas"},{"baseLayer":"Pork Shoulder with Chile and Onions","condiment":"Salsa Sauce","mixing":"Veggies for Fish Tacos","seasoning":"Sriracha Marinade","shell":"Low Carb Tacos"}]';
favorites = Array.isArray(JSON.parse(favorites)) && JSON.parse(favorites); // <--- right 
console.log(favorites);

我需要做的是仅在出现点后接空格的情况下拆分字符串,而不是在其他空格处进行拆分。在这里它应该导致2个新列。

2 个答案:

答案 0 :(得分:3)

您可以这样做:

import re

s = "We were here today. You were here on 24.10.2018."

result = re.split("\.\s+", s)
print(result)

输出

['We were here today', 'You were here on 24.10.2018.']

答案 1 :(得分:2)

您可以将str.split函数用作:

"We were here today. You were here on 24.10.2018.".split('. ')

函数split可以使用任何字符串分隔符