<P>This is the first port of regex++ to the boost library, and is based on regex++ 2.x, see changes.txt for a full list of changes from the previous version. There are no known functionality bugs except that POSIX style equivalence classes are only guaranteed correct if the Win32 localization model is used (the default for Win32 builds of the library). </P>
<P>There are some aspects of the code that C++ puritans will consider to be poor style, in particular the use of goto in some of the algorithms. The code could be cleaned up, by changing to a recursive implementation, although it is likely to be slower in that case. </P>
<P>The performance of the algorithms should be satisfactory in most cases. For example the times taken to match the ftp response expression "^([0-9]+)(\-| |$)(.*)$" against the string "100- this is a line of ftp response which contains a message string" are: BSD implementation 450 micro seconds, GNU implementation 271 micro seconds, regex++ 127 micro seconds (Pentium P90, Win32 console app under MS Windows 95). </P>
<P>However it should be noted that there are some "pathological" expressions which may require exponential time for matching; these all involve nested repetition operators, for example attempting to match the expression "(a*a)*b" against <I>N</I> letter a's requires time proportional to <I>2<SUP>N</I></SUP>. These expressions can (almost) always be rewritten in such a way as to avoid the problem, for example "(a*a)*b" could be rewritten as "a*b" which requires only time linearly proportional to <I>N</I> to solve. In the general case, non-nested repeat expressions require time proportional to <I>N<SUP>2</I></SUP>, however if the clauses are mutually exclusive then they can be matched in linear time - this is the case with "a*b", for each character the matcher will either match an "a" or a "b" or fail, where as with "a*a" the matcher can't tell which branch to take (the first "a" or the second) and so has to try both. <I>Be careful how you write your regular expressions and avoid nested repeats if you can! New to this version, some previously pathological cases have been fixed - in particular searching for expressions which contain leading repeats and/or leading literal strings should be much faster than before. Literal strings are now searched for using the Knuth/Morris/Pratt algorithm (this is used in preference to the Boyer/More algorithm because it allows the tracking of newline characters).</I></P>
<I><P>Some aspects of the POSIX regular expression syntax are implementation defined:</I></P>
<UL>
<LI>The "leftmost-longest" rule for determining what matches is ambiguous, this library takes the "obvious" interpretation: find the leftmost match, then maximize the length of each sub-expression in turn with lower indexed sub-expressions taking priority over higher indexed sub-expression. </LI>
<LI>The behavior of multi-character collating elements is ambiguous in the standard, in particular expressions such as [a[.ae.]] may have subtle inconsistencies lurking in them. This implementation matches bracket expressions as follows: all bracket expressions match a single character only, unless the expression contains a multi-character collating element, either on its own, or as the endpoint to a range, in which case the expression may match more than one character. </LI>
<LI>Repeated null expressions are repeated only once, they are treated "as if" they were matched the maximum number of times allowed by the expression. </LI>
<LI>The behavior of back references is ambiguous in the standard, in particular it is unclear whether expressions of the form "((ab*)\2)+" should be allowed. This implementation allows such expressions and the back reference matches whatever the last sub-expression match was. This means that at the end of the match, the back references may have matched strings different from the final value of the sub-expression to which they refer. </LI></UL>
<P>Class reg_expression<> and its typedefs regex and wregex are thread safe, in that compiled regular expressions can safely be shared between threads. The matching algorithms regex_match, regex_search, regex_grep, regex_format and regex_merge are all re-entrant and thread safe. Class match_results is now thread safe, in that the results of a match can be safely copied from one thread to another (for example one thread may find matches and push match_results instances onto a queue, while another thread pops them off the other end), otherwise use a separate instance of match_results per thread. </P>
<P>The POSIX API functions are all re-entrant and thread safe, regular expressions compiled with <I>regcomp</I> can also be shared between threads. </P>
<P>The class RegEx is only thread safe if each thread gets its own RegEx instance (apartment threading) - this is a consequence of RegEx handling both compiling and matching regular expressions. </P>
<P>Finally note that changing the global locale invalidates all compiled regular expressions, therefore calling <I>set_locale</I> from one thread while another uses regular expressions <I>will</I> produce unpredictable results. </P>
<P>There is also a requirement that there is only one thread executing prior to the start of main(). </P>
<P> Regex++ provides extensive support for run-time localization, the localization model used can be split into two parts: front-end and back-end. </P>
<P>Front-end localization deals with everything which the user sees - error messages, and the regular expression syntax itself. For example a French application could change [[:word:]] to [[:mot:]] and \w to \m. Modifying the front end locale requires active support from the developer, by providing the library with a message catalogue to load, containing the localized strings. Front-end locale is affected by the LC_MESSAGES category only. </P>
<P>Back-end localization deals with everything that occurs after the expression has been parsed - in other words everything that the user does not see or interact with directly. It deals with case conversion, collation, and character class membership. The back-end locale does not require any intervention from the developer - the library will acquire all the information it requires for the current locale from the underlying operating system / run time library. This means that if the program user does not interact with regular expressions directly - for example if the expressions are embedded in your C++ code - then no explicit localization is required, as the library will take care of everything for you. For example embedding the expression [[:word:]]+ in your code will always match a whole word, if the program is run on a machine with, for example, a Greek locale, then it will still match a whole word, but in Greek characters rather than Latin ones. The back-end locale is affected by the LC_TYPE and LC_COLLATE categories. </P>
<P>There are three separate localization mechanisms supported by regex++: </P>
<I><P>Win32 localization model.</I></P>
<P>This is the default model when the library is compiled under Win32, and is encapsulated by the traits class <AHREF="template_class_ref.htm#regex_char_traits">w32_regex_traits</A>. When this model is in effect there is a single global locale as defined by the user's control panel settings, and returned by GetUserDefaultLCID. All the settings used by regex++ are acquired directly from the operating system bypassing the C run time library. Front-end localization requires a resource dll, containing a string table with the user-defined strings. The traits class exports the function: </P>
<P>which needs to be called with a string identifying the name of the resource dll, <I>before</I> your code compiles any regular expressions (but not necessarily before you construct any <I>reg_expression</I> instances): </P>
<P>Note that this API sets the dll name for <I>both</I> the narrow and wide character specializations of w32_regex_traits. </P>
<P>This model does not currently support thread specific locales (via SetThreadLocale under Windows NT), the library provides full Unicode support under NT, under Windows 9x the library degrades gracefully - characters 0 to 255 are supported, the remainder are treated as "unknown" graphic characters. </P>
<I><P>C localization model.</I></P>
<P>This is the default model when the library is compiled under an operating system other than Win32, and is encapsulated by the traits class <AHREF="template_class_ref.htm#regex_char_traits"><I>c_regex_traits</I></A>, Win32 users can force this model to take effect by defining the pre-processor symbol BOOST_RE_LOCALE_C. When this model is in effect there is a single global locale, as set by <I>setlocale</I>. All settings are acquired from your run time library, consequently Unicode support is dependent upon your run time library implementation. Front end localization requires a POSIX message catalogue. The traits class exports the function: </P>
<P>which needs to be called with a string identifying the name of the message catalogue, <I>before</I> your code compiles any regular expressions (but not necessarily before you construct any <I>reg_expression</I> instances): </P>
<P>Note that this API sets the dll name for <I>both</I> the narrow and wide character specializations of c_regex_traits. If your run time library does not support POSIX message catalogues, then you can either provide your own implementation of <nl_types.h> or define BOOST_RE_NO_CAT to disable front-end localization via message catalogues. </P>
<P>Note that calling <I>setlocale</I> invalidates all compiled regular expressions, calling <TT>setlocale(LC_ALL, "C")</TT> will make this library behave equivalent to most traditional regular expression libraries including version 1 of this library. </P>
<P>This model is only in effect if the library is built with the pre-processor symbol BOOST_RE_LOCALE_CPP defined. When this model is in effect each instance of reg_expression<> has its own instance of std::locale, class reg_expression<> also has a member function <I>imbue</I> which allows the locale for the expression to be set on a per-instance basis. Front end localization requires a POSIX message catalogue, which will be loaded via the std::messages facet of the expression's locale, the traits class exports the symbol: </P>
<P>which needs to be called with a string identifying the name of the message catalogue, <I>before</I> your code compiles any regular expressions (but not necessarily before you construct any <I>reg_expression</I> instances): </P>
<P>Note that calling reg_expression<>::imbue will invalidate any expression currently compiled in that instance of reg_expression<>. This model is the one which closest fits the ethos of the C++ standard library, however it is the model which will produce the slowest code, and which is the least well supported by current standard library implementations, for example I have yet to find an implementation of std::locale which supports either message catalogues, or locales other than "C" or "POSIX". </P>
<P>Finally note that if you build the library with a non-default localization model, then the appropriate pre-processor symbol (BOOST_RE_LOCALE_C or BOOST_RE_LOCALE_CPP) must be defined both when you build the support library, and when you include <boost/regex.hpp> or <boost/cregex.hpp> in your code. The best way to ensure this is to add the #define to <boost/regex/detail/regex_options.hpp>. </P>
<I><P>Providing a message catalogue:</I></P>
<P>In order to localize the front end of the library, you need to provide the library with the appropriate message strings contained either in a resource dll's string table (Win32 model), or a POSIX message catalogue (C or C++ models). In the latter case the messages must appear in message set zero of the catalogue. The messages and their id's are as follows: <BR>
<P>The character class name for alphanumeric characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"alnum" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>301 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for alphabetic characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"alpha" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>302 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for control characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"cntrl" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>303 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for digit characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"digit" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>304 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for graphics characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"graph" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>305 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for lower case characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"lower" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>306 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for printable characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"print" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>307 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for punctuation characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"punct" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>308 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for space characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"space" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>309 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for upper case characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"upper" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>310 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for hexadecimal characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"xdigit" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>311 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for blank characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"blank" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>312 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for word characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"word" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
<TR><TDWIDTH="8%"VALIGN="TOP">
<P> </TD>
<TDWIDTH="22%"VALIGN="TOP">
<P>313 </TD>
<TDWIDTH="32%"VALIGN="TOP">
<P>The character class name for Unicode characters. </TD>
<TDWIDTH="31%"VALIGN="TOP">
<P>"unicode" </TD>
<TDWIDTH="7%"VALIGN="TOP">
<P> </TD>
</TR>
</TABLE>
<P><BR>
</P>
<P>Finally, custom collating element names are loaded starting from message id 400, and terminating when the first load thereafter fails. Each message looks something like: "tagname string" where <I>tagname</I> is the name used inside [[.tagname.]] and <I>string</I> is the actual text of the collating element. Note that the value of collating element [[.zero.]] is used for the conversion of strings to numbers - if you replace this with another value then that will be used for string parsing - for example use the Unicode character 0x0660 for [[.zero.]] if you want to use Unicode Arabic-Indic digits in your regular expressions in place of Latin digits. </P>
<P>Note that the POSIX defined names for character classes and collating elements are always available - even if custom names are defined, in contrast, custom error messages, and custom syntax messages replace the default ones. </P>
<P><HR></P>
<H3><ANAME="demos"></A>Appendix 4: Example Applications</H3>
<P>There are three demo applications that ship with this library, they all come with makefiles for Borland, Microsoft and gcc compilers, otherwise you will have to create your own makefiles. </P>
<H5>regress.exe: </H5>
<P>A regression test application that gives the matching/searching algorithms a full workout. The presence of this program is your guarantee that the library will behave as claimed - at least as far as those items tested are concerned - if anyone spots anything that isn't being tested I'd be glad to hear about it. </P>
<P>A simple grep implementation, run with no command line options to find out its usage. Look at <AHREF="src/fileiter.cpp">fileiter.cpp</A>/fileiter.hpp and the mapfile class to see an example of a "smart" bidirectional iterator that can be used with regex++ or any other STL algorithm. </P>
<P>A simple interactive expression matching application, the results of all matches are timed, allowing the programmer to optimize their regular expressions where performance is critical. </P>
<P>The snippets examples contain the code examples used in the documentation:</P>
<P><AHREF="example/snippets/regex_match_example.cpp">regex_match_example.cpp</A>: ftp based regex_match example.</P>
<P><AHREF="example/snippets/regex_search_example.cpp">regex_search_example.cpp</A>: regex_search example: searches a cpp file for class definitions.</P>
<P><AHREF="example/snippets/regex_grep_example_1.cpp">regex_grep_example_1.cpp</A>: regex_grep example 1: searches a cpp file for class definitions.</P>
<P><AHREF="example/snippets/regex_merge_example.cpp">regex_merge_example.cpp</A>: regex_merge example: converts a C++ file to syntax highlighted HTML.</P>
<P><AHREF="example/snippets/regex_grep_example_2.cpp">regex_grep_example_2.cpp</A>: regex_grep example 2: searches a cpp file for class definitions, using a global callback function. </P>
<P><AHREF="example/snippets/regex_grep_example_3.cpp">regex_grep_example_3.cpp</A>: regex_grep example 2: searches a cpp file for class definitions, using a bound member function callback.</P>
<P><AHREF="example/snippets/regex_grep_example_4.cpp">regex_grep_example_4.cpp</A>: regex_grep example 2: searches a cpp file for class definitions, using a C++ Builder closure as a callback.</P>
<P><AHREF="example/snippets/regex_split_example_1.cpp">regex_split_example_1.cpp</A>: regex_split example: split a string into tokens.</P>
<P><AHREF="example/snippets/regex_split_example_2.cpp">regex_split_example_2.cpp</A>: regex_split example: spit out linked URL's.</P>
<P>There are two main headers used by this library: <boost/regex.hpp> provides full access to the entire library, while <boost/cregex.hpp> provides access to just the high level class RegEx, and the POSIX API functions. </P>
<P> If you are using Microsoft or Borland C++ and link to a dll version of the run time library, then you will also link to one of the dll versions of regex++. While these dll's are redistributable, there are no "standard" versions, so when installing on the users PC, you should place these in a directory private to your application, and not in the PC's directory path. Note that if you link to a static version of your run time library, then you will also link to a static version of regex++ and no dll's will need to be distributed. The possible regex++ dll's are as follows: <BR>
<P>Note: you can disable automatic library selection by defining the symbol BOOST_RE_NO_LIB when compiling, this is useful if you want to statically link even though you're using the dll version of your run time library, or if you need to debug regex++. </P>
<P><HR></P>
<H3><ANAME="upgrade"></A>Notes for upgraders</H3>
<P>This version of regex++ is the first to be ported to the <AHREF="http://www.boost.org/">boost</A> project, and as a result has a number of changes to comply with the boost coding guidelines. </P>
<P>Headers have been changed from <header> or <header.h> to <boost/header.hpp></P>
<P>The library namespace has changed from "jm", to "boost". </P>
<P>The reg_xxx algorithms have been renamed regex_xxx (to improve naming consistency). </P>
<P>Algorithm query_match has been renamed regex_match, and only returns true if the expression matches the whole of the input string (think input data validation). </P>
<I><P>Compiling existing code:</I></P>
<P>The directory, libs/regex/old_include contains a set of headers that make this version of regex++ compatible with previous ones, either add this directory to your include path, or copy these headers to the root directory of your boost installation. The contents of these headers are deprecated and undocumented - really these are just here for existing code - for new projects use the new header forms. </P>
<P><HR></P>
<H3><ANAME="furtherInfo"></A>Further Information (Contacts and Acknowledgements)</H3>
<P>The author can be contacted at <AHREF="mailto:John_Maddock@compuserve.com">John_Maddock@compuserve.com</A>, the home page for this library is at <AHREF="http://ourworld.compuserve.com/homepages/John_Maddock/regexpp.htm">http://ourworld.compuserve.com/homepages/John_Maddock/regexpp.htm</A>, and the official boost version can be obtained from <AHREF="file:///c:/boost/site/libs/libraries.htm">www.boost.org/libraries.htm</A>. </P>
<P>I am indebted to Robert Sedgewick's "Algorithms in C++" for forcing me to think about algorithms and their performance, and to the folks at boost for forcing me to <I>think</I>, period. The following people have all contributed useful comments or fixes: Dave Abrahams, Mike Allison, Edan Ayal, Jayashree Balasubramanian, Beman Dawes, Paul Baxter, David Dennerline, Edward Diener, Robert Dunn, , Fabio Forno, Tobias Gabrielsson, Rob Gillen, Marc Gregoire, Chris Hecker, Nick Hodapp, Jesse Jones, Martin Jost, Boris Krasnovskiy, Jan Hermelink, Max Leung, Wei-hao Lin, Jens Maurer, Heiko Schmidt, Scobie Smith, Alexander Sokolovsky, Hervé Poirier, Marc Recht, Bruno Voigt, Alexey Voinov, Jerry Waldorf, Rob Ward, Lealon Watts, Thomas Witt and Yuval Yosef. I am also grateful to the manuals supplied with the Henry Spencer, Perl and GNU regular expression libraries - wherever possible I have tried to maintain compatibility with these libraries and with the POSIX standard - the code however is entirely my own, including any bugs! I can absolutely guarantee that I will not fix any bugs I don't know about, so if you have any comments or spot any bugs, please get in touch. </P>
<P>The <AHREF="http://www.opengroup.org/onlinepubs/7908799/toc.htm">Open Unix Specification</A> contains a wealth of useful material, including the regular expression syntax, and specifications for <AHREF="http://www.opengroup.org/onlinepubs/7908799/xsh/regex.h.html"><regex.h></A> and <AHREF="http://www.opengroup.org/onlinepubs/7908799/xsh/nl_types.h.html"><nl_types.h></A>. </P>
<P>The <AHREF="http://www.cs.purdue.edu/homes/stelo/pattern.html">Pattern Matching Pointers</A> site is a "must visit" resource for anyone interested in pattern matching. </P>
<P><AHREF="http://glimpse.cs.arizona.edu/">Glimpse and Agrep</A>, use a simplified regular expression syntax to achieve faster search times. </P>
<P><AHREF="http://glimpse.cs.arizona.edu/udi.html">Udi Manber</A> and <AHREF="http://www.dcc.uchile.cl/~rbaeza/">Ricardo Baeza-Yates</A> both have a selection of useful pattern matching papers available from their respective web sites. </P>
<P><HR></P>
<I><P>Copyright </I><AHREF="mailto:John_Maddock@compuserve.com"><I>Dr John Maddock</I></A><I> 1998-2000 all rights reserved.</I></P></BODY>