os_substitute_copy |
Copies a collection of strings, while searching and replacing strings matching a regular expression pattern.
Copies all strings in the range [
begin , end ) to out .
Searches for any string that matches the regular expression pattern
, and replaces it with the string replace . If global
is true , replaces all occurrences; otherwise,
replaces only the first match. Returns the number of substitutions performed.
#include <ospace/helper/regalgo.h>
template< class ForwardIterator >
size_t os_substitute
(
ForwardIterator begin,
ForwardIterator end,
OutputIterator out,
const string& pattern,
const string& replace,
bool global
);
#include <iostream>
#include <algorithm>
#include <vector>
#include <ospace/helper.h>
void
fill( vector< string >& v )
{
v.push_back( "Web<Component> and Web<Component> are cool." );
v.push_back( "STL<ToolKit> and Helper<Component> are cool." );
v.push_back( "JGL is cool." );
}
void
substitute
(
const vector< string >& v,
const string& pattern,
const string& replace_with,
bool replace_global = false
)
{
os_substitute_copy
(
v.begin(),
v.end(),
os_ostream_iterator< string >( cout, "\n" ),
pattern,
replace_with,
replace_global,
false
);
cout << endl;
}
void
print( const string& s )
{
cout << s << endl;
}
void
main()
{
vector< string > v;
fill( v );
for_each( v.begin(), v.end(), print );
cout << "\nReplace the first occurrence of Web by systems:" << endl;
substitute( v, "Web", "Systems" );
cout << "Replace all occurrences of Component by ToolKit:" << endl;
substitute( v, "Component", "ToolKit", true );
cout << "Replace JGL by JGL, The Java Generic Library:" << endl;
substitute( v, "JGL", "JGL, The Java Generic Library" );
}
Web<Component> and Web<Component> are cool.
STL<ToolKit> and Helper<Component> are cool.
JGL is cool.
Replace the first occurrence of Web by systems:
Systems<Component> and Web<Component> are cool.
STL<ToolKit> and Helper<Component> are cool.
JGL is cool.
Replace all occurrences of Component by ToolKit:
Web<ToolKit> and Web<ToolKit> are cool.
STL<ToolKit> and Helper<ToolKit> are cool.
JGL is cool.
Replace JGL by JGL, The Java Generic Library:
Web<Component> and Web<Component> are cool.
STL<ToolKit> and Helper<Component> are cool.
JGL, The Java Generic Library is cool.
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.