Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Regular Expressions Cookbook | Find a Match Within Another Match

パンくず

書籍 Regular Expressions Cookbook
Find a Match Within Another Match

概要

マッチした内容を、更に別の条件にマッチさせる

サンプル

list = []
subject = "【124\n212\nhige】【hohoho 999】"
# mは複数行モード
subject.scan(/(.*?)/m) {|outergroups|
    # 【】で括られている文字のうち数値のみをリストへ
    list += outergroups[0].scan(/\d+/)
}
pp list #=>["124", "212", "999"]