fs.Glob, fs.DirWalk, etc. does the pattern support complex regex

111 views
Skip to first unread message

pat2...@gmail.com

unread,
Nov 3, 2022, 4:20:16 PM11/3/22
to golang-nuts
This has to be a FAQ, but my google foo for searching it is not clear
The docs say: " The syntax of patterns is the same as in path.Match."

The pattern seems to implement only fairly simple search expressions.

I'd like to search for "./*.MP4", but of course to be platform independant
I have to search for both upper and lower case "mp4" and "MP4" 
and maybe even allow "Mp4".

This is trivial in most regex, including go's regex

Can I do this with Glob or DirWalk? Or do I need to just
get the file names and manually apply the regex?

James

unread,
Nov 3, 2022, 5:34:08 PM11/3/22
to golang-nuts
It makes the pattern a bit ugly, but you can definitely do it with path.Match https://go.dev/play/p/zi6nh8Vj9LF

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2d6cfad4-6a32-458a-89d1-f5546c80df90n%40googlegroups.com.

Konstantin Khomoutov

unread,
Nov 4, 2022, 8:47:51 AM11/4/22
to golang-nuts
The latter.

But please note that I, for one, have not heard of any popular filesystem
on a platform Go runs on which would support regexp matching when traversing
its entries, and fs.Glob is largely modelled after what Unix shells implement.
Hence basically if something like fs.Regexp were introduced, it would anyway
need to walk the hierarchy and apply the supplied regexp to each pathname it
visits.

Please also note that using a regexp for what you're after looks like an
overkill to me: a simple

strings.EqualFold(filepath.Ext(fname), "mp4")

in the callback function to fs.WalkDir wold convey the intent better, IMO.

In any case, such a callback function to find a bunch of files matching their
extensions in a case-insensitive manner would clock at 10-15 lines, I'd say.

pat2...@gmail.com

unread,
Nov 4, 2022, 11:16:44 AM11/4/22
to golang-nuts
Thanks to all who replied.
Reply all
Reply to author
Forward
0 new messages