Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Submatches behavior

42 views
Skip to first unread message

hss...@googlemail.com

unread,
Dec 17, 2018, 4:54:16 AM12/17/18
to
Hi,

I have the following code to test submatches in regexp:

set line1 "hello first LIB=xy"
set line2 "hello second ((MAKE=a) OR (MAKE=b));"
set line3 "hello third ((MAKE=c)) LIB=yz;"


proc grep_markers {line} {
set make_cont ""
set lib_cont ""
set match ""

set match [regexp {\S+MAKE=(\w+)\W+LIB=(\w+)} $line dummy make_cont lib_cont]

puts "match: $match"
puts "make_cont: $make_cont"
puts "lib_cnt: $lib_cont"

}


puts "line3"
grep_markers $line3
puts ""
puts "line2"
grep_markers $line2
puts ""
puts "line1"
grep_markers $line1

**************************************************************************

As a result I get:

line3
match: 1
make_cont: c
lib_cnt: yz

line2
match: 0
make_cont:
lib_cnt:

line1
match: 0
make_cont:
lib_cnt:

Why is "make_cont" (with line2 as input) not filled with the submatch "a"?
Or are "make_cont" and "lib_cont" only assigned when all matches are in place? How can I match "as much as possible"?

Thank you,
hssig

two...@gmail.com

unread,
Dec 17, 2018, 3:09:18 PM12/17/18
to
On Monday, December 17, 2018 at 1:54:16 AM UTC-8, hss...@googlemail.com wrote:

>
> Why is "make_cont" (with line2 as input) not filled with the submatch "a"?
> Or are "make_cont" and "lib_cont" only assigned when all matches are in place? How can I match "as much as possible"?

Your pattern specifies that MAKE is followed by LIB (plus a few other constraints), and so if there is no match, then there is no sub-expressions to capture.

If you want to capture what follows MAKE= without requiring that LIB follows, then match only that part, and use a second regexp command to match and capture what follows LIB=.

0 new messages