regex - Go 标志:尾部斜杠转义 Windows 上的引号
问题描述
需要验证和清理路径的用户输入。当用户输入以下命令行时:
app.exe -f "C:\dir with space\"
标志值的最后一个引号已转义,因此它的字符串值为:
C:\dir with space"
你们对清理目录/路径的用户输入的干净方法有什么建议?正则表达式,或者 Go 是否有一个类似于 filepath.Clean() 的库来处理这个问题,但会删除尾随引号?
编辑:原因记录在这里: https ://github.com/golang/go/issues/16131
解决方案
例如,
package main
import (
"fmt"
"path/filepath"
"runtime"
"strings"
)
func clean(path string) string {
if runtime.GOOS == "windows" {
path = strings.TrimSuffix(path, `"`)
}
return filepath.Clean(path)
}
func main() {
path := `C:\dir with space"`
fmt.Println(path)
path = clean(path)
fmt.Println(path)
}
输出:
C:\dir with space"
C:\dir with space
推荐阅读
- codeigniter-2 - codeigniter email 在电子邮件中显示 HTML 标签
- jenkins - 如何在 Jenkins 上使用 Docker-compose?
- r - 使用循环在 R 中组合 .txt 文件
- windows-installer - 为 windows 创建安装程序,它将 xla 插件安装到 windows 机器上以用于 excel 应用程序
- java - 如何测试斯坦福情绪模型?
- c++ - 如何将“主参数”传递给成员函数
- jms - 如何根据消息的年龄从 JMS 主题中清除消息
- python - 如何将对象附加到类中的列表(python)
- python - 使用漂亮的肥皂进行网页抓取时缺少值
- python - 多个“使用 tensorflow 后端”消息在训练时淹没输出