Regex isn't the simplest solution in this case. This is easier than you are making it seem. Use the example below and add as many switches as you would like.
Code: Select all
... add this inside your proc ...
# init search variables, create empty lists
set search [list] ; set search_a [list] ; set search s [list]
# iterate user input, parse out switches
foreach word [split $text] {
# parse switches with the switch statement. oh the irony ;)
# [string range $word 3 end] removes the switch itself from
# the word before adding the rest of the word to those lists.
switch -glob -- $word {
"-a=*" { lappend search_a [string range $word 3 end] }
"-s=*" { lappend search_s [string range $word 3 end] }
default { lappend search $word }
}
}
# join the search, search_a, and search_s lists into strings we can use
set search [join $search] ; set search_a [join $search_a] ; set search_s [join $search_s]
# Use these 3 variables in your script now.
# $search = users search terms
# $search_a = all switches starting with -a combined
# $search_s = all switches starting with -s combined
Doing it this way, allows for any combinations and placements as well as stacking switches or search elements. This way is intuitive.
Any terms with known switches are applied to each switches list, which makes it possible to stack switches or search elements like the example below:
-s=foo my%search% -a=we -a=also -a=this -a=too -s=bar
in the above case, this is how the three variables you get would look:
$search = my%search%
$search_a = we also this too
$search_s = foo bar
This should be how you envisioned it.