【发布时间】:2015-08-25 10:40:05
【问题描述】:
我遇到了一种情况,我想在正则表达式模式中使用非贪婪原子.*?。
set input "Device ID: HOST1
Interface: GigabitEthernet0/1, Port ID (outgoing port): GigabitEthernet2/43
Device ID: HOST2
Entry address(es):
Interface: GigabitEthernet0/2, Port ID (outgoing port): GigabitEthernet2/43
"
puts "======== Non-Greedy regex starting with some other patterns ========"
puts [ regexp -inline {Device\s+ID:.*?outgoing\s+port\):\s+} $input]
puts "======== Non-Greedy regex at first ========"
puts [ regexp -inline {.*?outgoing\s+port\):\s+} $input]
输出:
======== Non-Greedy regex starting with some other patterns ========
{Device ID: HOST1
Interface: GigabitEthernet0/1, Port ID (outgoing port): GigabitEthernet2/43
Device ID: HOST2
Entry address(es):
Interface: GigabitEthernet0/2, Port ID (outgoing port): }
======== Non-Greedy regex at first ========
{Device ID: HOST1
Interface: GigabitEthernet0/1, Port ID (outgoing port): }
虽然.*?outgoing\s+port\):\s+ 一直匹配到第一次出现,但模式Device\s+ID:.*?outgoing\s+port\):\s+ 不会在第一次出现匹配时停止。
为什么非贪心匹配的行为会因为原子的位置而受到影响?
【问题讨论】:
标签: regex tcl non-greedy