2012-11-22 191 views
1

我對TCL或使用TCL正則表達式並不熟練。但是我需要一個TCL機制/正則表達式,它可以排除或讓一個單詞在給定一行/句子時有一些特殊字符。TCL:正則表達式排除具有字符的字符串

假設我有一個像下方的行/句子的東西:

(space)(space)At 4:00:00AM (not sure) please do your work ... 

現在我儘量拆分行使用的foreach獲得每字循環:

% set fields [split " At 4:00:00AM (not sure) please do your work" " " ] 
{} {} {} At 4:00:00AM (not sure) please do your work 

但我再做一次不想空字段:

% foreach val $fields { 
     puts $val 
} 



At 
4:00:00AM 
(not 
sure) 
please 
do 
your 
work 

除此之外,我想排除foreach廁所中的詞p有特殊字符,如:

(not 
sure) 
4:00:00AM 

排除在單詞的開始,結束或任何位置有'('或':'的單詞。

請讓我怎麼做到這一點。

回答

0

不幸的是,Tcl正則表達式不支持look-behind操作符。否則,它可以通過一個正則表達式來實現。 但是,你可以使用下面的代碼來構建的單詞列表,你需要:

set the_line " At 4:00:00AM (not sure) please do your work" 
set fields {} 
foreach {- val} [regexp -all -inline -- {(?:^|\s)([^:()\s]+(?=\s|$))} $the_line] { 
    lappend fields $val 
} 
1
set str " At 4:00:00AM (not sure) please do your work" 

# split the string into space-delimited words 
set words [regexp -inline -all {\S+} $str] 

# eliminate words containing a character other than letters, numbers, underscore 
set alnum_words [lsearch -inline -regexp -all -not $words {\W}] 

alnum_words現在包含列表{At please do your work}

如果只想僅由字母的話,使用

lsearch -inline -regexp -all $words {^[[:alpha:]]+$} 
相關問題