Trying to verify if a given string is a valid path and filename, I wrote a horrible regex only to learn that Go does not support positive look ahead. After reading the discussions on the topic, I understand the rationales why but I have still not been able to come up with a reasonable way of doing it.
I was hoping that path/filepath would be the place to be but as I am reluctant to try and open a file, that did not help me since all I want to know is if a string actually forms a valid path regardless of whether there’s a file at that path or not.
Here’s what I am trying to do:
- Verify if a given string is a valid path without having to access a file or create one
- Be able to do this under Linux and/or Windows without having to worry about the underlying os
Btw, here is a part of the horrible regex-code - enjoy:
case runtime.GOOS == "windows":
regExpString = `(^([a-z]|[A-Z]):(?=\\(?![\0-\37<>:"/\\|?*])|\/(?![\0-\37<>:"/\\|?*])|$)|^\\(?=[\\\/][^\0-\37<>:"/\\|?*]+)|^(?=(\\|\/)$)|^\.(?=(\\|\/)$)|^\.\.(?=(\\|\/)$)|^(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+)|^\.\.(?=(\\|\/)[^\0-\37<>:"/\\|?*]+))((\\|\/)[^\0-\37<>:"/\\|?*]+|(\\|\/)$)*()$`
reg, err := regexp.Compile(regExpString)
if err != nil {
return (err)
}
if reg.MatchString(fileString) {
err := doSomethingWithTheFile(fileString)
if err != nil {
return (err)
}
return (nil)
}
Needless to say, the error was "error parsing regexp: invalid or unsupported Perl syntax: (?=
"
This is a seemingly trivial problem, but…
br/brusan