开发者

Returning all matches from a series of regex captures

开发者 https://www.devze.com 2023-02-18 07:26 出处:网络
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.

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|1433开发者_如何学JAVAB_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?


MatchData#captures

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


also,

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号