user9575308
user9575308

Reputation:

How to extract second group from second item of list from a regexp pattern in tcl

How to extract the second group from the second item of the list. I'm printing all results to see.

Extract the date from this:

<span class="rel_date">26 Sep at 12:18 am</span>
(...)
    set result [regexp -inline -all -line {(?:class="rel_date">)(.*)(?:<\/sp)} [http::data $token]]
    foreach j $result {
        foreach {group0 group1 group2} $j {
        puts $group1
        }       
    }
(...)

Upvotes: 1

Views: 45

Answers (1)

Jerry
Jerry

Reputation: 71538

You are on the right track. You don't need the outer foreach:

set result [regexp -inline -all -line {(?:class="rel_date">)(.*)(?:<\/sp)} [http::data $token]]
foreach {group0 group1} $result {
    puts $group1
    # => 26 Sep at 12:18 am
}

There is really only two groups in the results. One containing the main match, the second containing the first capture group (( ... ) is a capture group, but (?: ... ) is not)

If you happen to have many such spans in the same page, then the above loop will still work:

set page {
    <span class="rel_date">26 Sep at 12:18 am</span>
    <span class="rel_date">26 Sep at 12:19 am</span>
}

set result [regexp -inline -all -line {(?:class="rel_date">)(.*)(?:<\/sp)} $page]
foreach {group0 group1} $result {
    puts $group1
}
# => 26 Sep at 12:18 am
# => 26 Sep at 12:19 am

Upvotes: 1

Related Questions