【发布时间】:2013-09-17 23:49:23
【问题描述】:
我试图从下面的字符串中提取子字符串
package: name='com.example.tracker' versionCode='1' versionName='1.0'
作为字符串 1:versionCode='1' 并作为字符串 2:versionName='1.0'
我使用 str.find('versionCode) 返回版本代码中“v”的索引,并使用字符串长度访问“1”。但是有时版本代码可能是两位数,所以我无法修复数字的位置。有没有办法做到这一点?
如果字符串是
package: name='com.example.tracker' versionCode='12' versionName='12.0'
我需要提取 12 和 12.0。 我的实现可以支持单个数字,但数字会有所不同。
if line.find('versionCode') != -1:
x = line.find('versionCode')
versionCode = line[x+13:x+15]
【问题讨论】:
-
使用正则表达式来做到这一点,或者编写一个真正的解析器。