パンくず 書籍 Regular Expressions Cookbook Match Complete Lines That Do Not Contain a Word 概要 ある単語を含まない行について サンプル str =<<"EOS" line1 kuroko line2 EOS puts str puts "------------------------------------" print str.gsub(/…
パンくず 書籍 Regular Expressions Cookbook Find a Specific Word 概要 単語のマッチングについて 構文 /bword/b サンプル puts "My name is hoge.Your name is hoge too.".scan(/\bhoge\b/) 出力 hoge hoge
パンくず 書籍 Regular Expressions Cookbook Search Line by Line 概要 コマンドラインのように、各行に対して正規表現を行う方法について サンプル subject =<<"EOS" one two three EOS lines = subject.split(/\r?\n/) re = /two/ lines.each do |line| i…
パンくず 書籍 Regular Expressions Cookbook get left or right or match String 概要 正規表現のマッチング結果の取得 構文 利用する構文 ・マッチした内容より前方を取得 $` ・マッチした内容を取得 $& ・マッチした内容より後方を取得 $' サンプル "Hoge…
パンくず 書籍 Regular Expressions Cookbook Capture and Name Parts of the Match 概要 名前付きキャプチャについて 構文 (?<name>regexp) # => 名前付き後方参照宣言 \k<name># => 名前付き後方参照利用 サンプル 後方参照にyear,month,dayという名前を付けて利用 tar</name></name>…
パンくず 書籍 Regular Expressions Cookbook Replace All Matches Within the Matches of Another Regex 概要 正規表現で抽出した内容のうち、 更に別の正規表現に一致するもののみ置換する サンプル # タグの中の文字列がbeforeだった場合のみ置換する sub…
パンくず 書籍 Regular Expressions Cookbook Find a Match Within Another Match 概要 マッチした内容を、更に別の条件にマッチさせる サンプル list = [] subject = "【124\n212\nhige】【hohoho 999】" # mは複数行モード subject.scan(/【(.*?)】/m) {|o…
パンくず 書籍 Regular Expressions Cookbook Retrieve a List of All Matches 概要 マッチした内容全体をリストで取得 構文 # 戻り値はヒットした件数分のリストになる ret = "reg".scan(/regexp/) サンプル subject = "hoge hige hage" result = subject.s…
パンくず 書籍 Regular Expressions Cookbook Insert the Regex Match into the Replacement Text 概要 マッチングした内容を後方参照で置換文字列内に埋め込む方法について 構文 マッチ内容をグループのindexを指定して取得 string.gsub(/(pattern1)(patter…
パンくず 書籍 Regular Expressions Cookbook Insert the Regex Match into the Replacement Text 概要 マッチングした内容を後方参照で置換文字列内に埋め込む方法について 構文 マッチ内容全体を参照 \0 サンプル URLをはてな記法のリンクにします。 # \S…