Skip to content

Instantly share code, notes, and snippets.

@hackugyo
Created January 6, 2016 18:19
Show Gist options
  • Select an option

  • Save hackugyo/529ea8dc64e2403b92fd to your computer and use it in GitHub Desktop.

Select an option

Save hackugyo/529ea8dc64e2403b92fd to your computer and use it in GitHub Desktop.
与えられたカッコ(なんでも.開きと閉じとの定義を引数にくれればいい)内を抜き出します. ruby extract_between_parentheses.rb "「」" YOUR_FILE_NAME
# coding: utf-8
open_paren = Regexp.quote(ARGV[0][0])
close_paren = Regexp.quote(ARGV[0][1])
open(ARGV[1]).read.lines
.select {|x| x.include?(ARGV[0][0]) && x.include?(ARGV[0][1])}
.map{|x|
x
.gsub(/^(.*?)#{open_paren}/, "#{open_paren}")
.gsub(/#{close_paren}[^#{open_paren}]*$/, "#{close_paren}")
.gsub(/#{close_paren}.*?#{open_paren}/,"#{close_paren}#{open_paren}")
}.join('').split(ARGV[0][1]).each{|x| puts "#{x + ARGV[0][1]}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment