Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何在Regex和之后的一切中分手_Python_Regex - Fatal编程技术网

Python 我如何在Regex和之后的一切中分手

Python 我如何在Regex和之后的一切中分手,python,regex,Python,Regex,我有一个正则表达式,它成功地捕获了一个日期。现在我想把它分开,这样约会之后的一切都会被拿走。我现在有: data = "11/07/2020 apple\n juice\n 11/07/2020 pear" dateRegex = re.compile('([0-9]+\/[0-9]+\/[0-9]+)') splittedData = re.split(dateRegex, data) # Splits into: ['11/07/2020', ' apple\n jui

我有一个正则表达式,它成功地捕获了一个日期。现在我想把它分开,这样约会之后的一切都会被拿走。我现在有:

data = "11/07/2020 apple\n juice\n 11/07/2020 pear"
dateRegex = re.compile('([0-9]+\/[0-9]+\/[0-9]+)')
splittedData = re.split(dateRegex, data)

# Splits into: ['11/07/2020', ' apple\n juice\n ', '11/07/2020' ' pear']
# Desired:     ['11/07/2020 apple\n juice\n ', '11/07/2020 pear']
提前感谢。

您可以使用

(?您可以使用


(?我不明白为什么不在换行符上拆分,然后剥离元素。请尝试在换行符上拆分。因为在一个日期后的数据中可能有多个换行符,所以我都不希望拆分。我更新了示例以使其更清楚。使用
re.finditer
查找日期的起点,然后使用此list要从一个开始捕获到下一个开始,请在列表中添加一个结束文本开始sentinel。我不明白为什么不在换行符上拆分元素,然后在换行符上拆分。尝试在换行符上拆分。因为在一个日期后的数据中可能有多个换行符,所以我都不希望拆分。我更新了示例以进一步说明这一点clear.use
re.finditer
定位日期的起始点,然后使用此列表从一个开始捕获到下一个开始,在列表中添加一个结束文本开始标记