re.search寻找某物的第一个实例。在以下代码中,“ \ t”出现两次。有没有办法让它跳到第二个实例?
code = ['69.22\t82.62\t134.549\n']
list = []
text = code
m = re.search('\t(.+?)\n', text)
if m:
found = m.group(1)
list.append(found)
结果:
list = ['82.62\t134.549']
预期:
list = ['134.549']
答案 0 :(得分:2)
对于“第二个”标签,只有一个解决方案。
您可以这样:
^(?:[^\t]*\t){2}(.*?)\n
解释
^ # BOS
(?: # Cluster
[^\t]* # Many not tab characters
\t # A tab
){2} # End cluster, do 2 times
( .*? ) # (1), anything up to
\n # first newline
Python代码
>>> import re
>>> text = '69.22\t82.62\t134.549\n'
>>> m = re.search('^(?:[^\t]*\t){2}(.*?)\n', text)
>>> if m:
>>> print( m.group(1) )
...
134.549
>>>
答案 1 :(得分:1)
此表达式的修改后的版本确实返回了所需的输出:
import re
code = '69.22\t82.62\t134.549\n'
print(re.findall(r'.*\t(.+?)\n', code))
['134.549']
我在猜测,也许您想设计一个表达式,有点类似于:
(?<=[\t])(.+?)(?=[\n])