diff options
Diffstat (limited to 'src/boost/libs/xpressive/example')
-rw-r--r-- | src/boost/libs/xpressive/example/Jamfile.v2 | 24 | ||||
-rw-r--r-- | src/boost/libs/xpressive/example/example.vcproj | 738 | ||||
-rw-r--r-- | src/boost/libs/xpressive/example/main.cpp | 257 | ||||
-rw-r--r-- | src/boost/libs/xpressive/example/numbers.cpp | 167 |
4 files changed, 1186 insertions, 0 deletions
diff --git a/src/boost/libs/xpressive/example/Jamfile.v2 b/src/boost/libs/xpressive/example/Jamfile.v2 new file mode 100644 index 00000000..ab9cd5d2 --- /dev/null +++ b/src/boost/libs/xpressive/example/Jamfile.v2 @@ -0,0 +1,24 @@ +# (C) Copyright 2004: Eric Niebler +# Distributed under 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) + +project + : requirements + <toolset>msvc-8.0:<define>_SCL_SECURE_NO_DEPRECATE + <toolset>msvc-9.0:<define>_SCL_SECURE_NO_DEPRECATE + <toolset>msvc,<stdlib>stlport:<define>_STLP_EXPOSE_GLOBALS_IMPLEMENTATION + ; + +exe examples + : + main.cpp + : + <include>$(BOOST_ROOT) + ; + +exe numbers + : + numbers.cpp + : + <include>$(BOOST_ROOT) + ; diff --git a/src/boost/libs/xpressive/example/example.vcproj b/src/boost/libs/xpressive/example/example.vcproj new file mode 100644 index 00000000..0e54599d --- /dev/null +++ b/src/boost/libs/xpressive/example/example.vcproj @@ -0,0 +1,738 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9.00" + Name="example-vc9" + ProjectGUID="{5B83CA94-A2A6-43A6-8363-E5F4731443F7}" + Keyword="Win32Proj" + TargetFrameworkVersion="131072" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="Debug" + IntermediateDirectory="Debug" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalOptions="/EHa" + Optimization="0" + AdditionalIncludeDirectories="..\..\.." + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_SCL_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_DEPRECATE" + MinimalRebuild="false" + ExceptionHandling="0" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + DisableLanguageExtensions="false" + TreatWChar_tAsBuiltInType="true" + ForceConformanceInForLoopScope="true" + RuntimeTypeInfo="true" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/example.exe" + LinkIncremental="2" + GenerateDebugInformation="true" + ProgramDatabaseFile="$(OutDir)/example.pdb" + SubSystem="1" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="Release" + IntermediateDirectory="Release" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="..\..\.." + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_SCL_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_DEPRECATE" + RuntimeLibrary="0" + UsePrecompiledHeader="0" + WarningLevel="3" + Detect64BitPortabilityProblems="false" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="$(OutDir)/example.exe" + LinkIncremental="1" + GenerateDebugInformation="true" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + RandomizedBaseAddress="1" + DataExecutionPrevention="0" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="Source Files" + Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx" + UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}" + > + <File + RelativePath=".\main.cpp" + > + </File> + </Filter> + <Filter + Name="Header Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd;ipp" + > + </Filter> + <Filter + Name="xpressive Files" + Filter="h;hpp;hxx;hm;inl;inc;xsd;ipp" + UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" + > + <File + RelativePath="..\..\..\boost\xpressive\basic_regex.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\match_results.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_actions.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_algorithms.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_compiler.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_constants.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_error.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_iterator.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_primitives.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_token_iterator.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\regex_traits.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\sub_match.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\xpressive.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\xpressive_dynamic.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\xpressive_fwd.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\xpressive_static.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\xpressive_typeof.hpp" + > + </File> + <Filter + Name="detail" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\detail_fwd.hpp" + > + </File> + <Filter + Name="core" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\core\access.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\action.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\adaptor.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\finder.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\flow_control.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\icase.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\linker.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\list.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matchers.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\optimize.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\peeker.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\quant_style.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\regex_impl.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\results_cache.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\state.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\sub_match_impl.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\sub_match_vector.hpp" + > + </File> + <Filter + Name="matcher" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\action_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\alternate_end_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\alternate_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\any_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_bol_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_bos_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_eol_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_eos_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_line_base.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\assert_word_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\attr_begin_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\attr_end_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\attr_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\charset_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\end_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\epsilon_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\keeper_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\literal_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\logical_newline_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\lookahead_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\lookbehind_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\mark_begin_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\mark_end_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\mark_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\optional_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\posix_charset_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\predicate_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\range_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\regex_byref_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\regex_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\repeat_begin_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\repeat_end_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\set_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\simple_repeat_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\string_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\core\matcher\true_matcher.hpp" + > + </File> + </Filter> + </Filter> + <Filter + Name="static" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\static\compile.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\grammar.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\is_pure.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\modifier.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\placeholders.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\static.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transmogrify.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\type_traits.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\visitor.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\width_of.hpp" + > + </File> + <Filter + Name="transforms" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_action.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_alternate.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_independent.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_inverse.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_marker.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_matcher.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_modifier.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_quantifier.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_sequence.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\static\transforms\as_set.hpp" + > + </File> + </Filter> + </Filter> + <Filter + Name="utility" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\algorithm.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\any.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\boyer_moore.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\cons.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\counted_base.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\dont_care.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\hash_peek_bitset.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\ignore_unused.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\literals.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\never_true.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\save_restore.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\sequence_stack.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\symbols.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\tracking_ptr.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\traits_utils.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\width.hpp" + > + </File> + <Filter + Name="chset" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\chset\basic_chset.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\chset\basic_chset.ipp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\chset\chset.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\chset\range_run.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\utility\chset\range_run.ipp" + > + </File> + </Filter> + </Filter> + <Filter + Name="dynamic" + > + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\dynamic.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\matchable.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\parse_charset.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\parser.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\parser_enum.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\parser_traits.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\detail\dynamic\sequence.hpp" + > + </File> + </Filter> + </Filter> + <Filter + Name="traits" + > + <File + RelativePath="..\..\..\boost\xpressive\traits\c_regex_traits.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\traits\cpp_regex_traits.hpp" + > + </File> + <File + RelativePath="..\..\..\boost\xpressive\traits\null_regex_traits.hpp" + > + </File> + <Filter + Name="detail" + > + <File + RelativePath="..\..\..\boost\xpressive\traits\detail\c_ctype.hpp" + > + </File> + </Filter> + </Filter> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/src/boost/libs/xpressive/example/main.cpp b/src/boost/libs/xpressive/example/main.cpp new file mode 100644 index 00000000..8992f0fc --- /dev/null +++ b/src/boost/libs/xpressive/example/main.cpp @@ -0,0 +1,257 @@ +/////////////////////////////////////////////////////////////////////////////// +// main.hpp +// +// Copyright 2004 Eric Niebler. Distributed under 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) + +#include <iostream> +#include <iomanip> +#include <boost/xpressive/xpressive.hpp> + +using namespace boost::xpressive; + +/////////////////////////////////////////////////////////////////////////////// +// Displays nested results to std::cout with indenting +// +// Display a tree of nested results +// +// Here is a helper class to demonstrate how you might display a tree of nested results: +struct output_nested_results +{ + int tabs_; + + output_nested_results(int tabs = 0) + : tabs_(tabs) + { + } + + template< typename BidiIterT > + void operator ()( match_results< BidiIterT > const &what ) const + { + // first, do some indenting + typedef typename std::iterator_traits< BidiIterT >::value_type char_type; + char_type space_ch = char_type(' '); + std::fill_n( std::ostream_iterator<char_type>( std::cout ), tabs_ * 4, space_ch ); + + // output the match + std::cout << what[0] << '\n'; + + // output any nested matches + std::for_each( + what.nested_results().begin(), + what.nested_results().end(), + output_nested_results( tabs_ + 1 ) ); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// See if a whole string matches a regex +// +// This program outputs the following: +// +// hello world! +// hello +// world + +void example1() +{ + std::string hello( "hello world!" ); + + sregex rex = sregex::compile( "(\\w+) (\\w+)!" ); + smatch what; + + if( regex_match( hello, what, rex ) ) + { + std::cout << what[0] << '\n'; // whole match + std::cout << what[1] << '\n'; // first capture + std::cout << what[2] << '\n'; // second capture + } +} + +/////////////////////////////////////////////////////////////////////////////// +// See if a string contains a sub-string that matches a regex +// +// Notice in this example how we use custom mark_tags to make the pattern +// more readable. We can use the mark_tags later to index into the match_results<>. +// +// This program outputs the following: +// +// 5/30/1973 +// 30 +// 5 +// 1973 +// / + +void example2() +{ + char const *str = "I was born on 5/30/1973 at 7am."; + + // define some custom mark_tags with names more meaningful than s1, s2, etc. + mark_tag day(1), month(2), year(3), delim(4); + + // this regex finds a date + cregex date = (month= repeat<1,2>(_d)) // find the month ... + >> (delim= (set= '/','-')) // followed by a delimiter ... + >> (day= repeat<1,2>(_d)) >> delim // and a day followed by the same delimiter ... + >> (year= repeat<1,2>(_d >> _d)); // and the year. + + cmatch what; + + if( regex_search( str, what, date ) ) + { + std::cout << what[0] << '\n'; // whole match + std::cout << what[day] << '\n'; // the day + std::cout << what[month] << '\n'; // the month + std::cout << what[year] << '\n'; // the year + std::cout << what[delim] << '\n'; // the delimiter + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Replace all sub-strings that match a regex +// +// The following program finds dates in a string and marks them up with pseudo-HTML. +// +// This program outputs the following: +// +// I was born on <date>5/30/1973</date> at 7am. + +void example3() +{ + std::string str( "I was born on 5/30/1973 at 7am." ); + + // essentially the same regex as in the previous example, but using a dynamic regex + sregex date = sregex::compile( "(\\d{1,2})([/-])(\\d{1,2})\\2((?:\\d{2}){1,2})" ); + + // As in Perl, $& is a reference to the sub-string that matched the regex + std::string format( "<date>$&</date>" ); + + str = regex_replace( str, date, format ); + std::cout << str << '\n'; +} + +/////////////////////////////////////////////////////////////////////////////// +// Find all the sub-strings that match a regex and step through them one at a time +// +// The following program finds the words in a wide-character string. It uses wsregex_iterator. +// Notice that dereferencing a wsregex_iterator yields a wsmatch object. +// +// This program outputs the following: +// +// This +// is +// his +// face + +void example4() +{ + #ifndef BOOST_XPRESSIVE_NO_WREGEX + std::wstring str( L"This is his face." ); + + // find a whole word + wsregex token = +alnum; + + wsregex_iterator cur( str.begin(), str.end(), token ); + wsregex_iterator end; + + for( ; cur != end; ++cur ) + { + wsmatch const &what = *cur; + std::wcout << what[0] << L'\n'; + } + #endif +} + +/////////////////////////////////////////////////////////////////////////////// +// Split a string into tokens that each match a regex +// +// The following program finds race times in a string and displays first the minutes +// and then the seconds. It uses regex_token_iterator<>. +// +// This program outputs the following: +// +// 4 +// 40 +// 3 +// 35 +// 2 +// 32 + +void example5() +{ + std::string str( "Eric: 4:40, Karl: 3:35, Francesca: 2:32" ); + + // find a race time + sregex time = sregex::compile( "(\\d):(\\d\\d)" ); + + // for each match, the token iterator should first take the value of + // the first marked sub-expression followed by the value of the second + // marked sub-expression + int const subs[] = { 1, 2 }; + + sregex_token_iterator cur( str.begin(), str.end(), time, subs ); + sregex_token_iterator end; + + for( ; cur != end; ++cur ) + { + std::cout << *cur << '\n'; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// Split a string using a regex as a delimiter +// +// The following program takes some text that has been marked up with html and strips +// out the mark-up. It uses a regex that matches an HTML tag and a regex_token_iterator<> +// that returns the parts of the string that do not match the regex. +// +// This program outputs the following: +// +// {Now }{is the time }{for all good men}{ to come to the aid of their}{ country.} + +void example6() +{ + std::string str( "Now <bold>is the time <i>for all good men</i> to come to the aid of their</bold> country." ); + + // find an HTML tag + sregex html = '<' >> optional('/') >> +_w >> '>'; + + // the -1 below directs the token iterator to display the parts of + // the string that did NOT match the regular expression. + sregex_token_iterator cur( str.begin(), str.end(), html, -1 ); + sregex_token_iterator end; + + for( ; cur != end; ++cur ) + { + std::cout << '{' << *cur << '}'; + } + std::cout << '\n'; +} + +/////////////////////////////////////////////////////////////////////////////// +// main +int main() +{ + std::cout << "\n\nExample 1:\n\n"; + example1(); + + std::cout << "\n\nExample 2:\n\n"; + example2(); + + std::cout << "\n\nExample 3:\n\n"; + example3(); + + std::cout << "\n\nExample 4:\n\n"; + example4(); + + std::cout << "\n\nExample 5:\n\n"; + example5(); + + std::cout << "\n\nExample 6:\n\n"; + example6(); + + std::cout << "\n\n" << std::flush; + + return 0; +} diff --git a/src/boost/libs/xpressive/example/numbers.cpp b/src/boost/libs/xpressive/example/numbers.cpp new file mode 100644 index 00000000..4e591bbb --- /dev/null +++ b/src/boost/libs/xpressive/example/numbers.cpp @@ -0,0 +1,167 @@ +/////////////////////////////////////////////////////////////////////////////// +// numbers.cpp +// +// Copyright 2008 David Jenkins. Distributed under 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) + +#if defined(_MSC_VER) +//disbale warning C4996: 'std::xxx' was declared deprecated +# pragma warning(disable:4996) +#endif + +#include <iostream> +#include <string> +#include <map> +#include <boost/assign/list_of.hpp> // for 'map_list_of()' +#include <boost/xpressive/xpressive.hpp> +#include <boost/xpressive/regex_actions.hpp> + +/////////////////////////////////////////////////////////////////////////////// +// Match all named numbers in a string and return their integer values +// +// For example, given the input string: +// "one two sixty three thousand ninety five eleven" +// the program will output: +// "one = 1" +// "two = 2" +// "sixty three thousand ninety five = 63095" +// "eleven = 11" + +void example1() +{ + using namespace boost::xpressive; + using namespace boost::assign; + + // initialize the maps for named numbers + std::map< std::string, int > ones_map = + map_list_of("one",1)("two",2)("three",3)("four",4)("five",5) + ("six",6)("seven",7)("eight",8)("nine",9); + + std::map< std::string, int > teens_map = + map_list_of("ten",10)("eleven",11)("twelve",12)("thirteen",13) + ("fourteen",14)("fifteen",15)("sixteen",16)("seventeen",17) + ("eighteen",18)("nineteen",19); + + std::map< std::string, int > tens_map = + map_list_of("twenty",20)("thirty",30)("fourty",40) + ("fifty",50)("sixty",60)("seventy",70)("eighty",80)("ninety",90); + + std::map< std::string, int > specials_map = + map_list_of("zero",0)("dozen",12)("score",20); + + // n is the integer result + local<long> n(0); + // temp stores intermediate values + local<long> temp(0); + + // initialize the regular expressions for named numbers + sregex tens_rx = + // use skip directive to skip whitespace between words + skip(_s) + ( + ( a3 = teens_map ) + | + ( a2 = tens_map ) >> !( a1 = ones_map ) + | + ( a1 = ones_map ) + ) + [ n += (a3|0) + (a2|0) + (a1|0) ]; + + sregex hundreds_rx = + skip(_s) + ( + tens_rx >> + !( + as_xpr("hundred") [ n *= 100 ] + >> !tens_rx + ) + ) + ; + + sregex specials_rx = // regex for special number names like dozen + skip(_s) + ( + // Note: this uses two attribues, a1 and a2, and it uses + // a default attribute value of 1 for a1. + ( !( a1 = ones_map ) >> ( a2 = specials_map ) ) + [ n = (a1|1) * a2 ] + >> !( "and" >> tens_rx ) + ) + ; + + sregex number_rx = + bow + >> + skip(_s|punct) + ( + specials_rx // special numbers + | + ( // normal numbers + !( hundreds_rx >> "million" ) [ temp += n * 1000000, n = 0 ] + >> + !( hundreds_rx >> "thousand" ) [ temp += n * 1000, n = 0 ] + >> + !hundreds_rx + ) + [n += temp, temp = 0 ] + ); + + // this is the input string + std::string str( "one two three eighteen twenty two " + "nine hundred ninety nine twelve " + "eight hundred sixty three thousand ninety five " + "sixty five hundred ten " + "two million eight hundred sixty three thousand ninety five " + "zero sixty five hundred thousand " + "extra stuff " + "two dozen " + "four score and seven"); + + // the MATCHING results of iterating through the string are: + // one = 1 + // two = 2 + // three = 3 + // eighteen = 18 + // twenty two = 22 + // nine hundred ninety nine = 999 + // twelve = 12 + // eight hundred sixty three thousand ninety five = 863095 + // sixty five hundred ten = 6510 + // two million eight hundred sixty three thousand ninety five = 2863095 + // zero = 0 + // sixty five hundred thousand = 6500000 + // two dozen = 24 + // four score and seven = 87 + sregex_token_iterator cur( str.begin(), str.end(), number_rx ); + sregex_token_iterator end; + + for( ; cur != end; ++cur ) + { + if ((*cur).length() > 0) + std::cout << *cur << " = " << n.get() << '\n'; + n.get() = 0; + } + std::cout << '\n'; + // the NON-MATCHING results of iterating through the string are: + // extra = unmatched + // stuff = unmatched + sregex_token_iterator cur2( str.begin(), str.end(), number_rx, -1 ); + for( ; cur2 != end; ++cur2 ) + { + if ((*cur2).length() > 0) + std::cout << *cur2 << " = unmatched" << '\n'; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// main +int main() +{ + std::cout << "\n\nExample 1:\n\n"; + example1(); + + std::cout << "\n\n" << std::flush; + + return 0; +} |