2021年3月21日星期日

How to use support for Windows path separator into a Regex?

I have the following code to check if someConfigFilePath value, which comes from configuration file, matches the current filePath and must support Windows and Unix-like platforms:

regexp.MatchString(someConfigFilePath, filePath)  

The regex works in Unix-like platforms, but not in Windows. After some debugging, I could find out that I need to do something like this before, then regex works inside Windows:

ps := string(os.PathSeparator)  someConfigFilePathReplaced = strings.ReplaceAll(someConfigFilePath, ps, "\\\\")  

Question is: which would be the best way to handle this kind of situation, using Golang, to accomplish support in regex regarding the path separator, for all OS platforms?

P.S.: All file paths were already handled by path/filepath.FromSlash

https://stackoverflow.com/questions/66739468/how-to-use-support-for-windows-path-separator-into-a-regex March 22, 2021 at 10:05AM

没有评论:

发表评论