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

非包含オペレータが正規表現中の先頭もしくは末尾に来ていたら怒るCop #34

Open
pocke opened this issue Jun 4, 2020 · 0 comments

Comments

@pocke
Copy link
Owner

pocke commented Jun 4, 2020

非包含オペレータが正規表現中の先頭もしくは末尾に来ている場合、それはおそらく意図した挙動とは違う挙動をするのではないか。

# (?~def) は de にマッチするので、これは "abcde" にマッチする。
p "abcdef".match?(/abc(?~def)/) # => true

# 後ろに \z がついていれば、abcde のあとは文字列の終端ではないのでマッチしなくなる。
p "abcdef".match?(/abc(?~def)\z/) # => false

Onigmoの中の人から「非包含演算子を単独で使うのはあんまりよくないかも」と教えていただきました。

確かに、実用上も他の正規表現の間に非包含演算子をサンドイッチするのが普通だと思います。本記事の上の例ではたまたま単独利用ばかりでしたので、実用的な例も貼っておきます。

https://techracho.bpsinc.jp/hachi8833/2017_03_23/37689

この元発言は見つけられず。
ただ、このように言われているし、非包含オペレータが正規表現中の先頭もしくは末尾に来ても嬉しいことはあんまりないのではないだろうか。

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