我需要正则表达式或 Python 方面的帮助,以便从一组字符串中提取子字符串。该字符串由字母数字组成。我只想要在第一个空格之后开始并在最后一个空格之前结束的子字符串,如下例所示。
Example 1:
A:01 What is the date of the election ?
BK:02 How long is the river Nile ?
Results:
What is the date of the election
How long is the river Nile
当我在做的时候,有没有一种简单的方法可以提取某个字符之前或之后的字符串?例如,我想从示例 2 中给出的字符串中提取日期或日期。
Example 2:
Date:30/4/2013
Day:Tuesday
Results:
30/4/2013
Tuesday
我实际上已经阅读过有关正则表达式的内容,但它对我来说非常陌生。谢谢。
最佳答案
我推荐使用split
>>> s="A:01 What is the date of the election ?"
>>> " ".join(s.split()[1:-1])
'What is the date of the election'
>>> s="BK:02 How long is the river Nile ?"
>>> " ".join(s.split()[1:-1])
'How long is the river Nile'
>>> s="Date:30/4/2013"
>>> s.split(":")[1:][0]
'30/4/2013'
>>> s="Day:Tuesday"
>>> s.split(":")[1:][0]
'Tuesday'
关于python - 在Python中的第一个空格后提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16313840/