gpt4 book ai didi

regex - 正则表达式拆分 : FutureWarning: split() requires a non-empty pattern match

转载 作者:行者123 更新时间:2023-12-03 15:47:22 25 4
gpt4 key购买 nike

当我使用 split() 时,我在 Python 3 版本中收到警告命令如下:

pattern = re.compile(r'\s*')
match = re.split(pattern, 'I am going to school')
print(match)

python3.6/re.py:212: FutureWarning: split() requires a non-empty pattern match. return _compile(pattern, flags).split(string, maxsplit)



我不明白为什么我会收到这个警告。

最佳答案

您收到此警告是因为使用 \s*您要求在零个或多个空格的子字符串上拆分的模式

但是......空字符串匹配该模式,因为其中有零个空格!

不清楚是什么re.split应该这样做。这是什么str.split做:

>>> 'hello world'.split('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: empty separator
>>>
re.split决定只扔掉那个空的子字符串选项,而是在一个或多个空格上拆分。在 python3.6 中,它发出 FutureWarning你看到了,告诉你那个决定。

你可以说你自己替换 *+ :
$ python3.6 -c "import re; print(re.split('\s*', 'I am going to school'))"
/usr/lib64/python3.6/re.py:212: FutureWarning: split() requires a non-empty pattern match.
return _compile(pattern, flags).split(string, maxsplit)
['I', 'am', 'going', 'to', 'school']

$ python3.6 -c "import re; print(re.split('\s+', 'I am going to school'))"
['I', 'am', 'going', 'to', 'school']

关于regex - 正则表达式拆分 : FutureWarning: split() requires a non-empty pattern match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47564710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com