정규 표현식을 사용하는 것이 작업을 수행하는 코드의 짧은 조각 :
이
if {[regexp {(.+),(.+)} $string a b c]} {
# $a is the complete match. But we don't care
# about that so we ignore it
puts $b; #tampa
puts $c; #florida
}
정규 표현식 (.+),(.+)
의미 :
(
. any character
+ one or more of the above
) save it in a capture group
, comma character
(
. any character
+ one or more of the above
) save it in a capture group
의 설명서를 참조하십시오를 정규 표현식에 대한 자세한 내용은 tcl의 정규 표현식 구문을 참조하십시오. https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm
정규 표현식에 익숙하지 않고 다른 방법을 원한다면 다양한 string 명령을 사용할 수 있습니다. 이 그것을 할 한 가지 방법입니다 :이 명령으로 제공
split tampa,florida ,
는 두 문자열 "탬파"를 포함하는 목록을 결과 : 몇 가지 추가 조건이없는 한
set comma_location [string first "," $string]
if {$comma_location > -1} {
set a [string range $string 0 [expr {$comma_location -1}]
set b [string range $string [expr {$comma_location +1}] end]
puts $a; #tampa
puts $b; #florida
}