【问题标题】:How to use regsub in tcl如何在 tcl 中使用 regsub
【发布时间】:2020-08-13 23:29:30
【问题描述】:

我正在使用 regsub 命令替换非字母数字字符,如 . \ 等,但它没有帮助。你能帮帮我吗

文字:

This is the new begin.\With old memories

需要将".\"替换为. 预期:

This is the new begin.With old memories

尝试

regsub -all {\.\\} $string {\.}

提前致谢

【问题讨论】:

  • 对于文字字符串,string map 可能会更好。
  • 真正的问题是什么? regsub 实际上给了你什么输出?
  • 请记住,替换字符串不是模式,而是文字字符串。而不是使用{\.},而是使用{.}——我怀疑你的“问题”是你看到输出This is the new begin\.With old memories在点之前带有反斜杠。

标签: regex tcl


【解决方案1】:

正如多纳尔所说,只需使用string map 例如

% set a {This is the new begin.\With old memories}
This is the new begin.\With old memories
% string map {.\\ .} $a
This is the new begin.With old memories

如您所见,我不得不用第二个反斜杠来转义反斜杠。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2013-07-26
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多