【发布时间】:2014-02-04 00:00:43
【问题描述】:
我有一个带有一些块的字符串,如下所示:
Entity: ent
Architecture: arch
Library: path/to/my/lib0
File: path/to/my/file.txt
Instances: 1
Package: pname
Library: path/to/my/other/lib1
File: path/to/my/other/file.txt
Instances: 2
Entity: another-ent
Architecture: arch2
Library: path/to/my/other/lib2
File: path/to/my/other/file.txt
Instances: 1
现在我想在 tcl 中获取库的每个值(不仅针对此块)。但仅当“实体”也是此块的一部分时(上面两行)。 我想要一个程序返回所有库的列表,但前提是之前提到了“实体”。 在这个例子中,我希望程序返回
lib0 lib2
我的做法:
regexp {.*Entity: *.*Library: .*/([^/\.]*)Source} $report -> all_libs
问题:它只返回一次。
我也在尝试获取“类型”。即它是架构、实体还是文件?例如get_type ent 应该返回Entity。 get_type arch 应该返回 Architecture。
我的做法:
proc get_type { name } {
set report " .... " # some blocks, defined above
regexp ".* ( .*): $name" $report -> type
echo "type: $type"
return $type
}
但这仅适用于少数类型,而不是所有类型。特别是当我在本例中使用路径中的字符串调用 proc 时,例如“path”。它返回“File”但不应该。只为“file”我要回File。
像get_type ent 这样的调用应该返回Entity 和get_type arch2 应该返回Architecture。
我不确定如果没有正则表达式是否可以更轻松地实现我的目标。
【问题讨论】:
-
$report包含完整的内容(所有块)还是仅包含一个块?块是如何分开的? -
您能否给出一些块的具体示例以及您希望得到的结果?从您所说的来看,听起来您想取回字符串“path/to/my/lib”和“Entity”(听起来不像是所有三个,因为您提到了“架构,实体或 文件')。