TCL 正则表达式模式搜索 [英] TCL regexp pattern search
问题描述
我试图找到一个模式匹配如下abc(xxxx):efg(xxxx):xyz(xxxx)
其中 xxxx
- [0-9] 个数字
I am trying to find a pattern match as below
abc(xxxx):efg(xxxx):xyz(xxxx)
where xxxx
- [0-9] digits
我用过
set string "my string is abc(xxxx):efg(xxxx):xyz(xxxx)"
regexp abc(....):efg(....):xyz(....) $string result_str
它返回 0.有人可以帮忙吗?
it returns 0. Can anyone help?
推荐答案
你遇到的问题是 (
和 )
对 Tcl 中的正则表达式有特殊意义(以及许多其他 RE 引擎),因为它们表示捕获子 RE.为了使字符正常",它们必须用反斜杠转义,这意味着最好将正则表达式放在大括号中(因为反斜杠是通用的 Tcl 元字符).
The problem you've got is that (
and )
have special meaning to regular expressions in Tcl (and many other RE engines besides) in that they denote a capturing sub-RE. To make the characters "normal", they have to be escaped with a backslash, and that means that it's best to put the regular expression in braces (because backslashes are general Tcl metacharacters).
因此:
% set string "my string is abc(xxxx):efg(xxxx):xyz(xxxx)"
% regexp {abc\(....\):efg\(....\):xyz\(....\)} $string
1
如果你想还捕获那些括号的内容,你需要一个稍微复杂的 RE:
If you want to also capture the contents of those parentheses, you need a slightly more complex RE:
regexp {abc\((....)\):efg\((....)\):xyz\((....)\)} $string \
all abc_bit efg_bit xyz_bit
请注意,那些 ....
序列总是精确匹配四个字符,但最好更具体一些.在每种情况下匹配任意数量的数字:
Note that those ....
sequences always match exactly four characters, but it's better to be more specific. To match any number of digits in each case:
regexp {abc\((\d+)\):efg\((\d+)\):xyz\((\d+)\)} $string -> abc efg xyz
当使用 regexp
提取字符串的位时,使用 ->
作为整个字符串的(相当奇怪的)变量名是很常见的比赛;它看起来像是在说将提取的片段发送到这些变量".
When using regexp
to extract bits of a string, it's pretty common to use ->
as a (rather strange) variable name for the whole string match; it looks mnemonically like it's saying "send the pieces extracted to these variables".
这篇关于TCL 正则表达式模式搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!