Python 正则表达式获取字符串子集
如何使用regex获得基于fullstops的子字符串?我们只希望在句号后得到数据Python 正则表达式获取字符串子集,python,regex,string,substring,Python,Regex,String,Substring,如何使用regex获得基于fullstops的子字符串?我们只希望在句号后得到数据 Str = “i like cows. I also like camels” // Regex Code here Output : “I also like camels” 不需要使用正则表达式。使用split()方法 splitted = Str.split('.') # splitted[0] will be 'i like cows' # splitted[1] will be 'I also li
Str = “i like cows. I also like camels”
// Regex Code here
Output : “I also like camels”
不需要使用正则表达式。使用
split()
方法
splitted = Str.split('.')
# splitted[0] will be 'i like cows'
# splitted[1] will be 'I also like camels'
试试这道菜
字符串dataIWant=mydata.split(“.”[1]
结果:我也喜欢camels使用split('.'),选择最后一个元素通常更好,但对于乐趣这是一个正则表达式解决方案:
import re
Str = "i like .cows. I also like camels"
pattern = r"([^\.]*$)"
results = re.search(pattern, Str)
print(results.group(1).strip())
您可以使用以下方法:
str1 = 'i like cows. I also like camels'
print(str1.split('.')[1:][0].strip())
输出:
I also like camels
这个
(?:[.]\s([A-Z].+)
选择了“我也喜欢骆驼”
你必须使用正则表达式吗.split('.')
似乎更容易。别忘了使用trim()
删除前导空格。