Home > Net >  How to do automatic regexp in tcl
How to do automatic regexp in tcl

Time:11-11

let's say I have a list

set a [list powerball gummy3 slash]

And I want to do an automatic regexp to find other kind of list that are similar

set b [list notaaball gummy2 slosh]
set c [list notlist notgummy notslash]

I want to match list b but not c, because there is the same number of elements as a and it have the same number of character.

Do I need to count the character in the list member, or do I need to find some \S regexep to match it ?

CodePudding user response:

This is not a job for regexps. Regexps are terrible at counting things. Instead, use llength and foreach and string length. And put it in a procedure:

proc listMatch {list1 list2} {
    if {[llength $list1] != [llength $list2]} {
        return false
    }
    foreach a $list1 b $list2 {
        if {[string length $a] != [string length $b]} {
            return false
        }
    }
    return true
}

Doing it this way has the advantage of handling edge cases that you've not thought of (such as elements with embedded spaces).

  • Related