Tbpgr Blog

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

Regular Expressions Cookbook

正規表現サンプル | **二文字で始まる行以外の全行を削除

正規表現サンプル 自分の作業用に使った正規表現のサンプルレシピ 概要 **(アスタリスク)二文字で始まる行以外の全行を削除 ※要ははてな記法の見出し以外の行を削除したい時に利用する正規表現検索条件 (?!^\*\*)(^.*\n?)置換条件 ※空文字 出力 置換前 **…

書籍 Regular Expressions Cookbook | Matching IPv4 Addresses

パンくず 書籍 Regular Expressions Cookbook Matching IPv4 Addresses 概要 IPv4のバリデーションについて サンプル subject = Array.new subject << "192.168.0.1" # => match subject << "255.255.255.255" # => match subject << "1.1.1.1" # => match s…

書籍 Regular Expressions Cookbook | Validating URLs

パンくず 書籍 Regular Expressions Cookbook Validating URLs 概要 URLのValidationについて サンプル subject = Array.new subject << "http://test.com" # => match subject << "https://test.cpm" # => match subject << "ftp://hoge" # => match subjec…

書籍 Regular Expressions Cookbook | Escape Regular Expression Metacharacters

パンくず 書籍 Regular Expressions Cookbook Escape Regular Expression Metacharacters 概要 正規表現のエスケープについて サンプル str = '\n[]|^!?+' puts str puts "-------------------------" puts Regexp.escape(str) 出力 \n[]|^!?+ -------------…

書籍 Regular Expressions Cookbook | Match Complete Lines That Do Not Contain a Word

パンくず 書籍 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 | Match Complete Lines That Contain a Word

パンくず 書籍 Regular Expressions Cookbook Match Complete Lines That Contain a Word 概要 ある単語を含む行について サンプル # encoding: Windows-31J require "pp" str =<<"EOS" line1 kuroko line2 EOS puts str puts "----------------------------…

書籍 Regular Expressions Cookbook | Remove Duplicate Lines

パンくず 書籍 Regular Expressions Cookbook Remove Duplicate Lines 概要 重複行の削除について サンプル str =<<"EOS" line1 line1 line2 EOS puts str puts "------------------------------------" print str.gsub(/^(.*)(?:(?:\r?\n|\n)\1)+/,"") 出力…

書籍 Regular Expressions Cookbook | Find Repeated Words

パンくず 書籍 Regular Expressions Cookbook Find Repeated Words 概要 単語の繰り返しのマッチングについて サンプル print "オラ オラ 無駄 無駄".scan(/\b(オラ+)\s+\1\b/) 出力 [["オラ"]]

書籍 Regular Expressions Cookbook | Find All Except a Specific Word

パンくず 書籍 Regular Expressions Cookbook Find All Except a Specific Word 概要 特定の単語を除く単語のマッチングについて サンプル print "dog cat tiger catfish".scan(/\b(?!cat\b)\w+/) 出力 ["dog", "tiger", "catfish"]

書籍 Regular Expressions Cookbook | Find Similar Words

パンくず 書籍 Regular Expressions Cookbook Find Similar Words 概要 似た単語のマッチングについて サンプル print "ユーザー ユーザ".scan(/\bユーザー?\b/) puts "" print "hage hige hoge".scan(/\bh[aio]ge?\b/) puts "" print "ベジータ セニョリー…

書籍 Regular Expressions Cookbook | Find a Specific Word

パンくず 書籍 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 | Reformat Names From “FirstName LastName” to “LastName FirstName”

パンくず 書籍 Regular Expressions Cookbook Reformat Names From “FirstName LastName” to “LastName FirstName” 概要 姓名の入れ替えについて。 書籍では結構複雑な英名に対応してたけど、 日本人なので日本語名の単純入れ替えのサンプルで例示。 サンプ…

書籍 Regular Expressions Cookbook | Limit the Number of Lines in Text

パンくず 書籍 Regular Expressions Cookbook Limit the Number of Lines in Text 概要 行数のバリデーション。 サンプル subject = Array.new subject << "1\n" # => \n=Unix,Linux subject << "1\n2\n" subject << "1\n2\n3\n" subject << "1\r\n" # => \n…

書籍 Regular Expressions Cookbook | Limit the Length of Text

パンくず 書籍 Regular Expressions Cookbook Limit the Length of Text 概要 文字数のバリデーション。 サンプル # encoding: Windows-31J require "pp" subject = Array.new subject << "azA0" subject << "azAZ0" subject << "azAZ01234あ" subject << "a…

書籍 Regular Expressions Cookbook | Limit Input to Alphanumeric Characters

パンくず 書籍 Regular Expressions Cookbook Limit Input to Alphanumeric Characters 概要 半角英数字のバリデーション。 サンプル subject = Array.new subject << "azAZ09" # =>match subject << "azAZあ09" subject << "あ" subject << "ア" # =>match …

書籍 Regular Expressions Cookbook | Validate Traditional Time Formats

パンくず 書籍 Regular Expressions Cookbook Validate Traditional Time Formats 概要 時間のバリデーション。 hh:mi:ss hh:24mi:ss それぞれについて。 サンプル subject = Array.new subject << "11:11:11" # =>match subject << "1:1:1" # =>match subje…

書籍 Regular Expressions Cookbook | Validate Traditional Date Formats

パンくず 書籍 Regular Expressions Cookbook Validate Traditional Date Formats 概要 日付のバリデーション。 YYYY-MM-DD もしくは YYYY/MM/DD かつ MMは01-12で一桁の場合は0を省略可能。 DDは01-31で一桁の場合は0を省略可能。この条件にマッチする正規…

書籍 Regular Expressions Cookbook | Validate Handy Phone Number

パンくず 書籍 Regular Expressions Cookbook Validate Handy Phone Number 概要 携帯電話番号のバリデーション サンプル subject = Array.new subject << "08012345678" subject << "090-1234-5678" subject << "070-1234-5678" subject << " 090-1234-5678…

書籍 Regular Expressions Cookbook | Search Line by Line

パンくず 書籍 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 start or end index

パンくず 書籍 Regular Expressions Cookbook get start or end index 概要 正規表現のマッチング結果の位置情報の取得 構文 利用する構文 ・マッチした内容を取得 $~ ・マッチした文字列の開始位置 $~.begin(0) ・マッチした文字列の終了位置 $~.end(0) サ…

書籍 Regular Expressions Cookbook | get left or right or match String

パンくず 書籍 Regular Expressions Cookbook get left or right or match String 概要 正規表現のマッチング結果の取得 構文 利用する構文 ・マッチした内容より前方を取得 $` ・マッチした内容を取得 $& ・マッチした内容より後方を取得 $' サンプル "Hoge…

書籍 Regular Expressions Cookbook | Eliminate Needless Backtracking

パンくず 書籍 Regular Expressions Cookbook Eliminate Needless Backtracking 概要 (独占的|絶対最大)量指定子について。 後戻りしてのマッチングをしない。 構文 /(target++)/ サンプル DragonQuestに対して、1文字以上の任意の文字(\w+)と(Quest)でマッ…

書籍 Regular Expressions Cookbook | Capture and Name Parts of the Match

パンくず 書籍 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

パンくず 書籍 Regular Expressions Cookbook Replace All Matches Within the Matches of Another Regex 概要 正規表現で抽出した内容のうち、 更に別の正規表現に一致するもののみ置換する サンプル # タグの中の文字列がbeforeだった場合のみ置換する sub…

書籍 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) {|o…

書籍 Regular Expressions Cookbook | Retrieve a List of All Matches

パンくず 書籍 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

パンくず 書籍 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

パンくず 書籍 Regular Expressions Cookbook Insert the Regex Match into the Replacement Text 概要 マッチングした内容を後方参照で置換文字列内に埋め込む方法について 構文 マッチ内容全体を参照 \0 サンプル URLをはてな記法のリンクにします。 # \S…

書籍 Regular Expressions Cookbook | Add Comments to a Regular Expression

パンくず 書籍 Regular Expressions Cookbook Add Comments to a Regular Expression 概要 正規表現でのコメントについて 構文 (?#comment) サンプル reg = /(?#family name)([A-Z][a-z]+)\s(?#first name)([A-Z][a-z]+)/ puts "Tanaka Hideo".scan(reg) #=>…

書籍 Regular Expressions Cookbook | Match Previously Matched Text Again

パンくず 書籍 Regular Expressions Cookbook Match Previously Matched Text Again 概要 前回一致した内容を再びマッチさせる方法について 構文 \b(\w)\1\b サンプル # 2文字の数値の繰り返しが出てきたら@に置換 pp "12age1212age1234age".gsub(/([\d]{2})…