Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#Ruby の正規表現置換でマッチ結果に対してメソッド実行する ( 例: 小文字を大文字にする例 ) #3169

Open
YumaInaura opened this issue May 26, 2020 · 0 comments

Comments

@YumaInaura
Copy link
Owner

  • カッコを使ってキャプチャする
  • gsub + ブロック記法で マッチ結果に対して好きなメソッドを実行できる
"ab cd ef".gsub(/(ab|ef)/) { "#{$1.upcase}" }
# => "AB cd EF"

Bad Case

ブロックでないと、複数個のマッチ結果がある場合に、置換結果が全て同じものになってしまう

"ab cd ef".gsub(/(ab|ef)/, "#{$1.upcase}")
# "EF cd EF"

参考

How to change case of letters in string using RegEx in Ruby - Stack Overflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant