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
reference/string/gsub.txt · 最終更新: 2011/07/04 15:16 by sukai
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0