Skip to content

Instantly share code, notes, and snippets.

@tbpgr
Last active August 29, 2015 14:05
Show Gist options
  • Save tbpgr/e697a792da552f6c2e55 to your computer and use it in GitHub Desktop.
Save tbpgr/e697a792da552f6c2e55 to your computer and use it in GitHub Desktop.
PerlBackrefsのサンプルコード
str = <<-EOS
abc hoge abc
abc hige abc
abc hage abc
EOS
str.match(/(.*)(hoge)(.*)(hige)(.*)(hage)/m)
print "$1= #{$1}", "\n"
print "$2= #{$2}", "\n"
print "$3= #{$3}", "\n"
print "$4= #{$4}", "\n"
print "$5= #{$5}", "\n"
print "$6= #{$6}", "\n"
__END__
# PerlBackrefs は --auto-correct. に対応しているので、
# rubocop -a で自動修正すると、以下のように修正されます。
str = <<-EOS
abc hoge abc
abc hige abc
abc hage abc
EOS
str.match(/(.*)(hoge)(.*)(hige)(.*)(hage)/m)
print "$1= #{Regexp.last_match[1]}", "\n"
print "$2= #{Regexp.last_match[2]}", "\n"
print "$3= #{Regexp.last_match[3]}", "\n"
print "$4= #{Regexp.last_match[4]}", "\n"
print "$5= #{Regexp.last_match[5]}", "\n"
print "$6= #{Regexp.last_match[6]}", "\n"
# RuboCopのこの規約が気に入らなければ
# .rubocop.yml に下記の設定を追加する
PerlBackrefs:
Enabled: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment