How to: grep an Array of strings

680 views
Skip to first unread message

Jason McConochie

unread,
Dec 3, 2015, 2:52:34 PM12/3/15
to julia-users

Is there grep for an Array of AbstractStrings?  See code below


# A. Read a file into memory (nLines pre-determined)

fID=open(fName)

iLine=0;

rLines=Array(ASCIIString,nLines);

while !eof(fID)

  iLine+=1

  rLines[iLine]=readline(fID)

end


# B. Find all strings in rLines with "parameter"

???? Is something like this possible?

indices=grep(rLines,r"parameter")




Erik Schnetter

unread,
Dec 3, 2015, 2:54:01 PM12/3/15
to julia...@googlegroups.com
You are looking for `filter`:

filter(line->match(r"parameter", line), rLines)

-erik

David P. Sanders

unread,
Dec 3, 2015, 3:29:31 PM12/3/15
to julia-users


El jueves, 3 de diciembre de 2015, 13:54:01 (UTC-6), Erik Schnetter escribió:
You are looking for `filter`:

filter(line->match(r"parameter", line), rLines)

Apparently this needs to be

filter(line->ismatch(r"3", line) != nothing, rLines)  

(replace "match" with "ismatch" to get a Boolean expression instead of a RegexMatch object).

Seth

unread,
Dec 3, 2015, 3:53:00 PM12/3/15
to julia-users
If you don't like regexes,

julia> a = ["apple","bear","ape","collar"]
4-element Array{ASCIIString,1}:
 
"apple"
 
"bear"
 
"ape"
 
"collar"


julia
> filter(x->contains(x,"ap"),a)
2-element Array{ASCIIString,1}:
 
"apple"
 
"ape"


julia
> filter(x->contains(x,"ar"),a)
2-element Array{ASCIIString,1}:
 
"bear"
 
"collar"

Stefan Karpinski

unread,
Dec 3, 2015, 3:55:50 PM12/3/15
to Julia Users
You can just pass a Regex object to filter:

filter(r"a.*b.*c"i, map(chomp,open(readlines,"/usr/share/dict/words")))

This gives all dictionary words containing "a", "b" and "c" in order but not contiguous.

Seth

unread,
Dec 3, 2015, 4:01:12 PM12/3/15
to julia-users
That's really elegant. Is there a reason filter() is defined for regex strings but not ASCIIStrings?

Stefan Karpinski

unread,
Dec 3, 2015, 4:06:16 PM12/3/15
to Julia Users
There's an obvious predicate implied by a Regex: does it match a string? What's the obvious predicate for a string? Checking whether it is contained in another string is one option but that's pretty arbitrary. You could just as well check for containment the other way. Or prefix, or suffix, etc.

Seth

unread,
Dec 3, 2015, 4:09:46 PM12/3/15
to julia-users
Makes sense. Thanks!

Jason McConochie

unread,
Dec 3, 2015, 4:24:07 PM12/3/15
to julia-users
Thank you to all.  In particular, how can the indices of the matching lines be returned - I've a matlab background so am used to working from the indices.

Seth

unread,
Dec 3, 2015, 5:00:47 PM12/3/15
to julia-users
One way using my previous code:

julia> find(x->contains(x,"ap"),a)
2-element Array{Int64,1}:
 
1
 
3

julia
> find(x->contains(x,"ar"),a)
2-element Array{Int64,1}:
 
2
 
4

Reply all
Reply to author
Forward
0 new messages