1
我有下面提到的2個字符串作爲輸入。
TCL中的字符串分割
Mon May 22 04:18:11 2017 call duration
和
call duration
我想Mon May 22 04:18:11 2017
作爲輸出。
如何使用TCL編寫正則表達式來獲取此值?
我有下面提到的2個字符串作爲輸入。
TCL中的字符串分割
Mon May 22 04:18:11 2017 call duration
和
call duration
我想Mon May 22 04:18:11 2017
作爲輸出。
如何使用TCL編寫正則表達式來獲取此值?
我這樣做:(累了,請原諒這個蹩腳varnames)
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"
%
請你精確的(它甚至還可以當小字符串未找到)什麼問題你正擁有的?分享不起作用的代碼。 –
你能否至少解釋一下這個問題的'call duration'字符串?你想說你需要從一個更大的字符串中刪除這個子字符串(它是用戶定義的?)嗎? –
剛剛檢查:你不需要任何正則表達式,請參閱http://ideone.com/bfc56s –