Those all seem like good ways - what’s your criteria for “best”? If performance, you can check with BenchmarkTools. If you mean stylistically, that’s more about your own preference. In my own code, I had a similar problem and did
numbers = map(eachmatch(pat, text)) do m
parse(Float64, m.match)
end
Since it does it in one step without intermediate variables, but to me, it’s clearer than the broadcast or anonymous function versions. I don’t think any of these is more or less idiomatic Julia though.
Side note - I had to account for numbers like “1.233e5” in my code, which wouldn’t be matched by your regex. Maybe this doesn’t occur in your input, but I thought I’d mention it
My question was not about performances, but rather about shortness, readability and stylistic. I agree that your solution is a good candidat, but I would have liked another solution to be possible for getting all words.
The incomplete regex for Float was just an example, the question was mainly about getting all words in some text from a rexex.
I think the problem here is that eachmatch can’t be combined well with broadcast and parse, because eachmatch will return Base.RegexMatchIterator, which iterates through Regexmatch instead of directing returning the contents, so it can’t be passed to parse. Maybe you can define a new iterator which wraps Regexmatch and returns what you want, or you can find some other regex libraries and implement what you want in Julia?
matches = collect( eachmatch(pat, txt) )
word1 = matches[1].match
# => ok
words = getfield.(matches, :match)
# => ok
words = matches..match
# => error ; no broadcast with ..
Yes, but it’s not shorter since you have te define the rubyscan method first (and thank you for your parentheses tip)
If one has to explain the code to someboby else, your three lines solution (post4) is yet the best one (with eachmatch replaced by findall, although findall is julia-1.3+ only).
numbers = map(findall(pat, txt)) do range
parse(Float64, txt[range])
end