Ryanmt
Ryanmt

Reputation: 3265

Returning all matches from a series of regex captures

I've a string which contains many fields I'd like to extract from it. These I can easily parse from the line with regex, but I'd like to grab them all at once.

My string is:

>sp|P31946-2|1433B_HUMAN Isoform Short of 14-3-3 protein beta/alpha OS=Homo sapiens GN=YWHAB

I'd like to use something like this:

id, entry, protein, organism, gene, existence, seq_version = (1..6).each do |i|
  line[/^>sp\|(\w*)\|(\w*)\s(.*)\sOS=(.+)\sGN=(.+)\sPE=(\d*)\sSV=(\d*)/, i]
end

Mainly, I'd just like to have one line of code to capture all of these attributes from the entry string. Is there a way to do it?

Upvotes: 1

Views: 132

Answers (2)

kurumi
kurumi

Reputation: 25609

also,

>> "THX1138".scan(/(.)(.)(\d+)(\d)/)
=> [["H", "X", "113", "8"]]

Upvotes: 2

clyfe
clyfe

Reputation: 23770

MatchData#captures

f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
f1    #=> "H"
f2    #=> "X"
f3    #=> "113"
f4    #=> "8"

Upvotes: 3

Related Questions