-
-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Escaping glob patterns #51
Comments
Escaping isn't currently supported by the parser. However you should be able to achieve it with the fluent builder: var glob = new GlobBuilder() Supporting it at the parser level is something I'd like to add in the future. I guess you have to give some thought to the escape sequence. If we went with a backslash, I guess you could now necessarily assume it to always be an escape character as someone might be just matching a path like this: |
Fluent builder wouldn't work for my use case, as I need to parse arbitrary patterns (one I used was just an example). Escaping with a backslash and only treating it as an escape sequence if followed by a special character sounds generally like the correct approach to me too. |
So, I thought I'd have a crack at this! I thought I was done, and was just adding some more test cases... then I realised that I don't think escaping with a backslash is going to work :( Consider this pattern (which is likely to be very common): The intent is of course to look for Same deal:
If we use backslash as an escape character, the result would be a literal You might think that at least it's not an issue on Linux, but technically you can have backslashes in filenames (even if it's uncommon). I'm not sure what a good solution is here. A simple solution is to make the escape character configurable, effectively pushing the problem to devs that use this library, but at least allowing them to choose something that works for their scenario. |
Yeah its tricky, thanks for having a go at it. I was just reading how other glob libraries approach it and came across this https://docs.python.org/3/library/glob.html It mentions:
:-) perhaps thats an easier approach |
Huh, that could be quite a clever solution! I'll give it a try tonight and see how it looks. |
I have done some more investigation on this, and it turns out that no logic changes are necessary for handling escaping.. It should already just work. So I ended up removing the escape sequence parsing. I added these test cases to
I noted that one of the test cases you added I think you were expecting these to match:
This isn't actually how this is currently interpreted. The above pattern is actually still a character list, so it will expect to match any one character in that list, which means
To match
Hopefully that makes sense. This feature should already work, but as part of the feature branch we need to just extend the README with a section explaining how escaping works. I'll get around to that at some point no doubt. |
Also negation also passes:
|
^ emphasis mine :) Yeah, I messed that one up!
Excellent 👍 |
Cool. Well thanks for the PR, the added tests, and the removal of the unnecessary options. I think this has helped tidy it up a bit. I've merged this to develop, and i'll probably merge to master pretty soon as an incremental release. |
Is escaping glob patterns supported?
For example, let's say I have a literal path like:
/my*files/more[stuff]/is-there-more?/
Is there a supported mechanism my which
*
,[
,]
and?
can be escaped, such that they will be treated as literals instead of glob characters? For example, can I escape characters with a backslash?/my\*files/more\[stuff\]/is-there-more\?/
The text was updated successfully, but these errors were encountered: