「{test}」が「test」になるように中かっこを削除したい。私は試した
"{test}".gsub(/\{(.*)\}/,$1)
while "{test}".gsub!(/(\{).*(\})/,""); end
"{test}".gsub(/\{.*\}/,"")
何も機能していないようです。
を使用する必要があります\1
。$1
"{test}".gsub /\{(.*)\}/, '\1'
または、すべての中括弧を削除したいだけの場合:
"{test}".delete "{}"
特定のステートメントからすべての中括弧を削除したいと考えています。その場合はお試しください
"{test}".gsub(/\{|\}/, '') => "test"
一方、最初または手から中括弧を削除したい場合は、おそらく非正規表現ベースの方法を使用して行うことができます
削除機能を使用できます。
static VALUE
rb_str_delete(int argc, VALUE *argv, VALUE str)
{
str = rb_str_dup(str);
rb_str_delete_bang(argc, argv, str);
return str;
}
example:
"hello".delete "l" =>"heo"
String#trで試してください
> "{test}".tr '{}', ''
#=> "test"