Skip to content
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

Request: Provide a constructor (like Regex( . . . ) vs r". . . ") #47

Open
ryofurue opened this issue Jan 4, 2025 · 3 comments
Open

Comments

@ryofurue
Copy link

ryofurue commented Jan 4, 2025

It would be nice if Glob provides a constructor that takes a regular String and converts it to a Glob.

The motivation is the same as #27 ; that is, it would be nice if standard string manipulations are available, but my request draws an analogy with Regex:

julia> a = "hello"
"hello"

julia> r1 = r".*$(a)\.jpg"
r".*$(a)\.jpg"

julia> r2 = Regex(".*$(a)\\.jpg")
r".*hello\.jpg"
@vtjnash
Copy link
Owner

vtjnash commented Jan 4, 2025

You can make either a FilenameMatch or GlobMatch object directly, since the macro is just a simple wrappers over calling one of those. Like regex (and other templates like printf, etc.), it can be sometime risky to generate them from code though instead of using literals, as you risk introducing meta-characters that causes them match more than you intended

@ryofurue
Copy link
Author

ryofurue commented Jan 5, 2025

You can make either a FilenameMatch or GlobMatch object directly

Thanks! I confirm that the following code works.

using Glob
a = "hello"
#g = Glob.FilenameMatch("$(a).jpg")
g = Glob.GlobMatch("$(a).jpg")
@show g
for file in readdir(g)
  println("Found $(file)")
end

I observe that GlobMatch isn't exported from Glob and that FilenameMatch doesn't work with readdir().

Like regex (and other templates like printf, etc.), it can be sometime risky to generate them from code though instead of using literals, as you risk introducing meta-characters that causes them match more than you intended

Understood, but I don't quite know what your intention is. Is your point that one shouldn't use the Regex constructor?

@vtjnash
Copy link
Owner

vtjnash commented Jan 8, 2025

I don't know that it needs to be exported, though it should at least be marked public now. Calling readdir with an fnmatch requires making an array of them, since each fnmatch can only match one part of the filename

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants