【问题标题】:How to return multiple occurences with regex in tcl如何在 tcl 中使用正则表达式返回多次出现
【发布时间】: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 应该返回Entityget_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 这样的调用应该返回Entityget_type arch2 应该返回Architecture。 我不确定如果没有正则表达式是否可以更轻松地实现我的目标。

【问题讨论】:

  • $report 包含完整的内容(所有块)还是仅包含一个块?块是如何分开的?
  • 您能否给出一些块的具体示例以及您希望得到的结果?从您所说的来看,听起来您想取回字符串“path/to/my/lib”和“Entity”(听起来不像是所有三个,因为您提到了“架构,实体 文件')。

标签: regex tcl


【解决方案1】:

您需要将-inline-all 选项改为regexp (http://tcl.tk/man/tcl8.5/TclCmd/regexp.htm)。在不了解您的数据的情况下,我无法帮助您制作正则表达式。

【讨论】:

  • 也许-indices 也是如此。
猜你喜欢
  • 2017-05-25
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
相关资源
最近更新 更多