text = "This [is] a [fill]-in-the-[blank]"
私は私のためにいくつかの魔法を行うための正規表現を探しています:
new_text = text.gsub(/[magic happens]/, "")
=> "This [] a []-in-the-[]"
私のコードは Ruby ですが、それは大した問題ではないと思います。
次のようなものが機能します。
text = "This [is] a [fill]-in-the-[blank]"
text.gsub(/\[.+?\]/, '[]')
#=> "This [] a []-in-the-[]"
text = "This [is] a [fill]-in-the-[blank]"
text.gsub(/(?<=\[).+?(?=\])/, "")
また
text.gsub(/(?<=\[)[^\]]+?(?=\])/, "")
テストケースを考慮して、 Rubularを使用してこれをプロトタイプしました-> http://rubular.com/r/TgdjOtc4Ruここから、一致または類似のものを削除できます。
[5] pry(main)> text = "This [is] a [fill]-in-the[blank]"
=> "This [is] a [fill]-in-the[blank]"
[6] pry(main)> text.gsub(/\[(\w+)\]/) { |match| "[]" }
=> "This [] a []-in-the[]"
おそらくもっときれいな方法があります:-)