简体   繁体   English

TCL 正则表达式包含 word 和 var 的整个表达式

[英]TCL regexp whole expression containing word and var

I got a list of strings.我得到了一个字符串列表。 I want to use it to create new lists containing strings with matching templates: *ram*$key* when ram is a known expression and $key is a variable in a foreach iteration.我想用它来创建包含匹配模板的字符串的新列表: *ram*$key* 当 ram 是一个已知表达式并且 $key 是 foreach 迭代中的一个变量时。 I tried running the following code:我尝试运行以下代码:

set key_words {adr addr wr_data din}
foreach key $key_words {
set ram_key [regexp -all -inline  ".*ram.*$key.*" $list]
puts "$ram_key";
}

but it just returns all the list as is但它只是按原样返回所有列表

The problem is that you are using .* in your pattern and this matches whole list variable.问题是您在模式中使用.*并且这匹配整个list变量。 Try using a more specific pattern.尝试使用更具体的模式。 See my example ("ram" is "pro"):看我的例子(“ram”是“pro”):

set list "The problem is not having a real pro33blem to pronounce"
set key_words {adr addr blem din noun}
foreach key $key_words {
    set ram_key [regexp -all -inline  "pro\\d*$key" $list]
    puts "\n$key: Found [llength $ram_key] items"
    puts "$ram_key";
}

and the result is:结果是:

adr: Found 0 items


addr: Found 0 items


blem: Found 2 items
problem pro33blem

din: Found 0 items


noun: Found 1 items
pronoun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM