Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.

C++ Boost

Boost.Regex

Format String Syntax

Boost.Regex Index




Format strings are used by the algorithm regex_replace and by match_results::format, and are used to transform one string into another.

There are three kind of format string: sed, Perl and extended, the extended syntax is a superset of the others so this is covered first.

Extended format syntax

In format strings, all characters are treated as literals except: ()$\?:

To use any of these as literals you must prefix them with the escape character \

The following special sequences are recognized: 
 
Grouping:

Use the parenthesis characters ( and ) to group sub-expressions within the format string, use \( and \) to represent literal '(' and ')'. 
 
Sub-expression expansions:

The following Perl like expressions expand to a particular matched sub-expression:
 

  $` Expands to all the text from the end of the previous match to the start of the current match, if there was no previous match in the current operation, then everything from the start of the input string to the start of the match.  
  $' Expands to all the text from the end of the match to the end of the input string.  
  $& Expands to all of the current match.  
  $0 Expands to all of the current match.  
  $N Expands to the text that matched sub-expression N.  


Conditional expressions:

Conditional expressions allow two different format strings to be selected dependent upon whether a sub-expression participated in the match or not:

?Ntrue_expression:false_expression

Executes true_expression if sub-expression N participated in the match, otherwise executes false_expression.

Example: suppose we search for "(while)|(for)" then the format string "?1WHILE:FOR" would output what matched, but in upper case. 
 
Escape sequences:

The following escape sequences are also allowed:

  \a The bell character.  
  \f The form feed character.  
  \n The newline character.  
  \r The carriage return character.  
  \t The tab character.  
  \v A vertical tab character.  
  \x A hexadecimal character - for example \x0D.  
  \x{} A possible Unicode hexadecimal character - for example \x{1A0}  
  \cx The ASCII escape character x, for example \c@ is equivalent to escape-@.  
  \e The ASCII escape character.  
  \dd An octal character constant, for example \10.  


Perl format strings

Perl format strings are the same as the default syntax except that the characters ()?: have no special meaning.

Sed format strings

Sed format strings use only the characters \ and & as special characters.

\n where n is a digit, is expanded to the nth sub-expression.

& is expanded to the whole of the match (equivalent to \0).

Other escape sequences are expanded as per the default syntax.


Revised 24 Oct 2003

Copyright John Maddock 1998- 2003

Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)