tags:

views:

122

answers:

1

Hello Guys, I am new to TCL and seeking a help to deal with the following expression. I am getting the i/p string from the user to validate any of these strings below & no others in a line in CLI using procedure

{ GHI GII GJI GKI}

and another tricky one is to write regexp to match only the characters which begin with alphabet A & end with B, It also have 1 or more of either YO or OY in between using procedure. Thank you

+2  A: 

If that's your input, then really there's no need to use regular expressions: just check that a supplied word is in that list:

set input { GHI GII GJI GKI}
foreach word {GJI GLI} {
    if {$word in $input} {
        puts "$word is in [list $input]"
    } else {
        puts "$word is not in [list $input]"
    }
}

A regex that matches "begin with alphabet A & end with B, It also have 1 or more of either YO or OY in between":

set re {^A(?:YO|OY)+B$}
foreach word {AYOB AYOOYB AYYB} {
    if {[regexp $re $word]} {
        puts "$word matches"
    } else {
        puts "$word does not match"
    }
}

If you mean "either (1 or more of YO) or (1 or more of OY), then the regex is

set re {^A(?:(?:YO)+|(?:OY)+)B$}
glenn jackman
@ Glenn Jackman Thank you Glenn. Actually, I have to represent as a procedure, get these strings from the user as an input (CLI) <IJK GHI MNO GII LOG GJI HLT GKI> and match that relevant string pattern using single regular expression Example: Pattern is Like IJK GHI MNO GII LOG GJI HLT GKI (User i/p @ CLI)OutputMatches: GHI GII GJI GKI (it's filtering IJK MNO LOG HLT)
Passion
hmm, beginning to smell like homework. So, given "CLI" you have to match words in the list that either have "C" as the 1st char, "L" as the 2nd char or "I" as the last char, is that right?
glenn jackman
@Glenn Thank you. I found the way to do it.
Passion