16

I'm looking for a function that behaves like matchall() but returns an array containing the match index not the string?

2 Answers 2

20

eachmatch will give you an iterator over the regex matches.

So then with a list comprehension you could do this e.g.

[x.offset for x in eachmatch(r"[0-9]","aaaa1aaaa2aaaa3")]

or this

map(x->getfield(x,:offset), eachmatch(r"[0-9]","aaaa1aaaa2aaaa3"))

or even this...

getfield.(collect(eachmatch(r"[0-9]","aaaa1aaaa2aaaa3")), [:offset])

All returning:

3-element Array{Int64,1}: 5 10 15 
Sign up to request clarification or add additional context in comments.

Comments

11

Thanks answer from Alexander Morley. And you can use findall() to get UnitRange of regex.

julia> findall(r"[0-9]+","aaaa1aaaa22aaaa333") 3-element Array{UnitRange{Int64},1}: 5:5 10:11 16:18 

In addition, if you want get string by regex, you can use SubString()

julia> s="aaaa1aaaa22aaaa333" ; julia> SubString.(s, findall(r"[0-9]+",s)) 3-element Array{SubString{String},1}: "1" "22" "333" 

(Above codes tested on v1.3.0)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.