====== gsub ====== ===== C++ ===== StringPtr String::gsub(const AnyPtr& pattern, const AnyPtr& fn) const; ===== Xtal ===== // param1 type : String, ... // param2 type : Stringを返すop_callが定義されている型 // return type : String String::gsub : method(pattern, fn); ===== Description ===== //pattern//に渡したパターンに一致する文字列全てを//fn//(exec)の戻り値で置換します(execはxpeg::Executor)。 最も単純には、文字列//pattern//に一致する文字列全てを文字列//fn//で置き換える、という使い方をします。 置換された文字列がその操作範囲内で再び置換対象になることはありません。 ===== Example1 ===== "foo"を"bar"で置換する ==== C++ ==== ==== Xtal ==== str : "foobar piyo fuga foo"; ptn : "foo"; fn : "bar"; str.gsub(ptn, fn).p; ==== Output ==== barbar piyo fuga bar ===== Example2 ===== "hoge"を"foo"に置換した回数をカウントする ==== C++ ==== - ==== Xtal ==== str : "hogehhhoogehoggehogehohgeohgeogehoegheoghohogeeghhogeeghogeogheohge"; ptn : "hoge"; counter : 0; fn : fun(e){counter++; return "foo";}; str.gsub(ptn, fn).p; counter.p; ==== Output ==== foohhhoogehoggefoohohgeohgeogehoegheoghofooeghfooegfooogheohge 5