os_substitute


Searches and replaces strings that match a regular expression pattern.

Finds all strings in the range [ begin , end ) that match the regular expression pattern , and replaces them with the string replace . If global is true , then replaces all occurrences; otherwise, only replaces the first match. Returns the number of substitutions performed.

Library

Helper<ToolKit>

Declaration

#include <ospace/helper/regalgo.h>

template< class ForwardIterator >
size_t os_substitute
  (
  ForwardIterator begin,
  ForwardIterator end,
  const string& pattern,
  const string& replace,
  bool global
  );
  
Example <ospace/helper/examples/regexpr6.cpp>
#include <iostream>
#include <algorithm>
#include <vector>
#include <ospace/helper.h>

void
print( const string& s )
  {
  cout << '\t' << s << endl;
  }

void
main()
  {
  // Build a vector of strings.
  vector< string > v;
  v.push_back( "Web<Component> and Web<Component> are C++ libraries." );
  v.push_back( "STL<ToolKit> and Helper<Component> are cool." );
  v.push_back( "JGL is cool." );

  // Display the vector contents.
  cout << "The original vector:" << endl;
  for_each( v.begin(), v.end(), print );
  // Perform regular expression replacements.
  cout << "\nReplace the first occurrence of \"Web\" with \"Systems\":"
       << endl;
  os_substitute
    (
    v.begin(),
    v.end(),
    string( "Web" ),
    string( "Systems" ),
    false
    );
  for_each( v.begin(), v.end(), print );
  cout << "\nReplace all occurrences of \"Component\" with \"ToolKit\":"
       <<endl;
  os_substitute
    (
    v.begin(),
    v.end(),
    string( "Component" ),
    string( "ToolKit" ),
    true
    );
  for_each( v.begin(), v.end(), print );
  cout << "\nReplace \"JGL\" with \"JGL, the Java Generic Library,\":"
       << endl;
  os_substitute
    (
    v.begin(),
    v.end(),
    string( "JGL" ),
    string( "JGL, the Java Generic Library," ),
    false
    );
  for_each( v.begin(), v.end(), print );
  cout << "\nReplace the pattern \" [cC][^.]*\" with \" portable\":"
       << endl;
  os_substitute
    (
    v.begin(),
    v.end(),
    string( " [cC][^.]*" ),
    string( " portable" ),
    true
    );
  for_each( v.begin(), v.end(), print );
  }

The original vector:
        Web<Component> and Web<Component> are C++ libraries.
        STL<ToolKit> and Helper<Component> are cool.
        JGL is cool.

Replace the first occurrence of "Web" with "Systems":
        Systems<Component> and Web<Component> are C++ libraries.
        STL<ToolKit> and Helper<Component> are cool.
        JGL is cool.

Replace all occurrences of "Component" with "ToolKit":
        Systems<ToolKit> and Web<ToolKit> are C++ libraries.
        STL<ToolKit> and Helper<ToolKit> are cool.
        JGL is cool.

Replace "JGL" with "JGL, the Java Generic Library,":
        Systems<ToolKit> and Web<ToolKit> are C++ libraries.
        STL<ToolKit> and Helper<ToolKit> are cool.
        JGL, the Java Generic Library, is cool.

Replace the pattern " [cC][^.]*" with " portable":
        Systems<ToolKit> and Web<ToolKit> are portable.
        STL<ToolKit> and Helper<ToolKit> are portable.
        JGL, the Java Generic Library, is portable.
	

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.