Tcl 正则表达式 [英] Tcl regular expressions
本文介绍了Tcl 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
set d(aa1) 1
set d(aa2) 1
set d(aa3) 1
set d(aa4) 1
set d(aa5) 1
set d(aa6) 1
set d(aa7) 1
set d(aa8) 1
set d(aa9) 1
set d(aa10) 1
set d(aa11) 1
set regexp "a*\[1-9\]"
set res [array names d -glob $regexp]
puts "res = $res"
在这种情况下,结果是:
In this case, the result is:
res = aa11 aa6 aa2 aa7 aa3 aa8 aa4 aa9 aa5 aa1
但是当我将正则表达式从 a*\[1-9\]
更改为 a*\[1-10\]
时,结果变为:>
But when I change the regexp from a*\[1-9\]
to a*\[1-10\]
, the result becomes:
res = aa11 aa10 aa1
推荐答案
您的字符类有错误.
[1-10]
不是 1 到 10 的数字- 表示
1-1
,是一个从1
到1
的字符(即简单的一个1
code>) 或0
.这解释了您的输出. - 要表示从 1 到 10 的数字,请使用:
(?:10?|[2-9])
(作为几种方法之一. - 因此你的正则表达式变成了
a*(?:10?|[2-9])
- 注意,如果您的引擎不允许非捕获组,则需要删除
?:
,为:a*(?:10?|[2-9])
[1-10]
does not mean a digit from 1 to 10- It means
1-1
, which is a character ranging from1
to1
(i.e., simply a1
), or a0
. This explains your output. - to express a digit from 1 to 10, use this:
(?:10?|[2-9])
(as one of several ways to do it. - therefore your regex becomes
a*(?:10?|[2-9])
- note that if your engine does not allow non-capturing group, you need to remove the
?:
, for:a*(?:10?|[2-9])
这篇关于Tcl 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文