regex - Tcl 正则表达式

标签 regex 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"

在这种情况下,结果是:

res = aa11 aa6 aa2 aa7 aa3 aa8 aa4 aa9 aa5 aa1

但是当我将正则表达式从 a*\[1-9\] 更改为 a*\[1-10\] 时,结果变为:

res = aa11 aa10 aa1

最佳答案

你的字符类有错误。

  • [1-10] 不代表 1 到 10 的数字
  • 意思是1-1,它是一个从11的字符(即简单的1),或者 0。这解释了您的输出。
  • 要表示从 1 到 10 的数字,请使用:(?:10?|[2-9])(作为实现此目的的几种方法之一。
  • 因此你的正则表达式变成了a*(?:10?|[2-9])
  • 请注意,如果您的引擎不允许非捕获组,您需要删除 ?:,因为:a*(?:10?|[2-9])

关于regex - Tcl 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325775/

相关文章:

java - 使用 java 中的正则表达式对特定字符串的第一次出现取反。

MySQL REGEXP 未能限制出现次数(?!)

bash - Expect 脚本不会将日期命令的 bash 替换扩展为 send

Python:使用正则表达式匹配一行HTML

regex - 如何在scala中截断一个单词后的字符串

javascript - ng-pattern 正则表达式匹配多个条件

comments - 有什么方法可以在 Tcl 命令中嵌入评论?

tcl - 为什么 tcl 的 "if"不返回值?

linux - Unix 随机决定拒绝创建目录的权限

c++ - 如何告诉TCL和TK在本地搜索默认 `.tcl`语言文件?