C++ Boost

Regex++, Format String Reference.

Copyright (c) 1998-2001

Dr John Maddock

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Dr John Maddock makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.


Format String Syntax

Format strings are used by the algorithms regex_format and regex_merge, and are used to transform one string into another.

There are three kind of format string: sed, perl and extended, the extended syntax is the default 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.


Copyright Dr John Maddock 1998-2000 all rights reserved.