Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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 从字符串中查找部分子字符串,然后显示其子字符串_Python_Python 3.x - Fatal编程技术网

Python 从字符串中查找部分子字符串,然后显示其子字符串

Python 从字符串中查找部分子字符串,然后显示其子字符串,python,python-3.x,Python,Python 3.x,我有一个字符串“Halo设备编号:6232123123客户ID:16926” 我想用这个关键字搜索:“62” 如果有,我想显示整个子串“6232123123” 输出: 62 found in index: 22 substring : Device 如果您正在查找一个特定的“完整字符串”,它在只有一个空格时结束,您可以再次使用.find(): string=str('Halo设备编号:6232123123客户ID:16926') 目标='62' ind=string.find(目标) ind

我有一个字符串“Halo设备编号:6232123123客户ID:16926”
我想用这个关键字搜索:“62”
如果有,我想显示整个子串“6232123123”

输出:

62 found in index:  22
substring : Device

如果您正在查找一个特定的“完整字符串”,它在只有一个空格时结束,您可以再次使用
.find()

string=str('Halo设备编号:6232123123客户ID:16926')
目标='62'
ind=string.find(目标)
ind_end=string.find(“”,ind+len(目标))
单词=字符串[ind:ind\u end]
现在,如果您想要更健壮的东西,我们可以使用正则表达式:

string=str('Halo设备编号:6232123123客户ID:16926')
word=re.findall('(62\d+),string.groups()[0]

这将从以“62”开头的字符串中提取第一个匹配项,并捕获所有剩余的数字。

代码中的
b
是什么?这是否回答了您的问题@RoshinRaphel抱歉,我是说ind,我只是编辑它谢谢你的更正。@Karthik我想这不是因为我想显示它的整个子字符串(如果有的话)
62 found in index:  22
substring : Device