Skip to content

Instantly share code, notes, and snippets.

@ekd123
Created August 28, 2013 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekd123/6362987 to your computer and use it in GitHub Desktop.
Save ekd123/6362987 to your computer and use it in GitHub Desktop.
提取文本中的漢字,不包含標點。支持所有Unicode標準區和擴展 AB 區。
#!/usr/bin/env tclsh
package require Tk
pack [ttk::label .l1 -text "在下面的框中輸入要檢查的文本"] -fill x
pack [text .input] -fill both -expand 1
pack [ttk::label .l2 -text "結果"] -fill x
pack [text .output] -fill both -expand 1
pack [ttk::button .start -text "開始" -command startFind] -side right
proc charIsChinese {ch} {
set code [scan $ch %c]
if { $code > 0x3400 && $code < 0x4DB5 } {
return true
} elseif { $code > 0x4E00 && $code < 0x9FA5 } {
return true
} elseif { $code > 0x9FA6 && $code < 0x9FBB } {
return true
} elseif { $code > 0xF900 && $code < 0xFA2D } {
return true
} elseif { $code > 0xFA30 && $code < 0xFA6A } {
return true
} elseif { $code > 0xFA70 && $code < 0xFAD9 } {
return true
} elseif { $code > 0x20000 && $code < 0x2A6D6 } {
return true
} elseif { $code > 0x2F800 && $code < 0x2FA1D } {
return true
} else {
return false
}
}
proc startFind {} {
foreach ch [split [.input get 1.0 {end -1c}] {}] {
if { [charIsChinese $ch] } {
.output insert end $ch
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment