如果您只是从字符串中获取子集,我不明白为什么要使用正则表达式。
运作方式相同:
if line.startswith('test:'): print(line[5:line.find('.')])
例:
>>> line = "test: match this.">>> print(line[5:line.find('.')]) match this
正则表达式速度慢,设计笨拙且难以调试。肯定有occassions使用它,但如果你只是要提取的文本
test:和
.,然后我不认为是这些场合之一。
参见:https :
//softwareengineering.stackexchange.com/questions/113237/when-you-should-not-
use-regular-
expressions
为了获得更大的灵活性(例如,如果要遍历要在字符串开头找到的字符串列表,然后将其索引出去),请用替换索引中的5(“
test:”的长度)
len(str_you_looked_for)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)