我试着提取含有数字的句子片段。例如,字符串
“这是一个带数字的句子,而不是一个有数字的句子,因为123。
应该回来
“而这不是一个有数字的句子,因为123。
我知道有从字符串中提取数字的方法,但我不知道如何找到索引并随后提取所需的字符串。任何帮助都是非常感谢的。
发布于 2021-10-13 03:23:57
这应该适用于给定的问题,但正如所评论的,这个问题有错误和假设的余地。
import re
test = "This is a sentence with numbers, and this is not a sentence with numbers because 123"
for t in test.split(','):
num = re.findall(r'\d+', t)
if num:
print(t)发布于 2021-10-13 06:14:51
首先用".“分隔文本,但"1.2”将被溢出。所以我们使用\.(?!\d)|(?<!\d)\.,意思是“左不是数字,右边不是数字”。
import re
text = "Test 1.2,Test A.1 Test,3 Test 4."
for t in re.split(r'\.(?!\d)|(?<!\d)\.|,', text):
num = re.findall(r'\d+', t)
if num:
print(t)https://stackoverflow.com/questions/69549273
复制相似问题