TCL中的字符串拆分

时间:2017-05-24 18:20:12

标签: regex tcl

我在下面提到了2个字符串作为输入。

Mon May 22 04:18:11 2017       call duration

call duration

我想将Mon May 22 04:18:11 2017作为输出。

如何编写正则表达式以使用TCL获取此值?

1 个答案:

答案 0 :(得分:0)

我会这样做:(累了,请原谅那些糟糕的变种)

proc prefix {big_str little_str} {
    # find the position of the first "little string" in the "big string"
    set index [string first $little_str $big_str]
    # get the substring that precedes that index
    return [string range $big_str 0 $index-1]
}

测试(甚至在找不到小字符串时也能正常工作)

% prefix "Mon May 22 04:18:11 2017       call duration" "call duration" 
Mon May 22 04:18:11 2017       
% prefix "Mon May 22 04:18:11 2017       call duration" "XYZ" 
% 
相关问题