ackCount = "Acknowledgement of #2352 on component \"lOrA-1\""
try:
OAC = int(re.search(r"#(\d+)", ackCount).group(1))
print(OAC)
# error handling if the cast to `int` fails, or there is no returned match
except ValueError, AttributeError:
print("No match found.")
如果字符串始终相同,则可以使用
ackCount = ackCount.split()
这将返回一个列表,其中每个元素都是原始字符串中的一个单词。默认情况下
split()
使用空格作为分隔符然后使用
ackCount[2][1:]
获取所有数字,再次假设它是相同的常规字符串,但只是数字不同。所以,索引列表的2,然后是从索引1开始的字符串的所有字符(因为字符串的索引0是“#”)为此,可以使用正则表达式。确保你做出了正确的图案!以下内容将返回所有匹配项的列表:
输出:
['9090']
字符串
string = "he hallo #9090 8080 fdsf sfd222 f222 #888"
将返回['9090', '888']
等这将在
ackCount
字符串中搜索第一个出现的八进制('#'),后跟一个或多个(+
)数字(\d
),仅捕获由re.search()
返回的Match
对象的捕获组(.group(1)
)中的数字就你的问题而言,这将成为:
相关问题 更多 >
编程相关推荐