ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
regexmatcher.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_strings of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#if (ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR) ) || DOXYGEN
9
10ALIB_EXPORT namespace alib { namespace strings { namespace util {
11
12//==================================================================================================
13/// This utility class wraps \https{boost::regex library,www.boost.org} and interfaces
14/// \alib_strings with it.
15///
16/// The availability of the class is dependent on the compiler-symbol \ref ALIB_FEAT_BOOST_REGEX
17/// which has to be \c true, and in parallel either \ref ALIB_CHARACTERS_WIDE is \c false or
18/// \ref ALIB_CHARACTERS_NATIVE_WCHAR equals \c true.
19///
20/// Method #Compile accepts the pattern string and compiles it to \b boost::regex.
21/// Subsequent invocations of #Match will then use the compiled regular expression for testing
22/// a given string.
23///
24/// The syntax of the regular expressions is compatible to
25/// \https{Perl Regular Expressions,perldoc.perl.org/perlretut.html}.
26///
27/// \note
28/// This is a very basic wrapper that supports just the <b>bare minimum of the features</b>
29/// available in the original \b boost::regex library.
30/// I.e, the expression syntax is fixed to be \e Perl-compatible and no string replacement
31/// features or even match positioning are available.
32//==================================================================================================
34{
35 protected:
36 /// This is the internal regex matcher.
37 boost::basic_regex<character, boost::regex_traits<character> > boostRegex;
38
39 public:
40 /// A simple struct that determines a range in a string.
41 /// A range is not bound to a certain string object and its validty has to be checked
42 /// when used.
43 struct SRange {
44 integer Position; ///< The starting index of this range in a string.
45 integer Length; ///< The length of this range in a string.
46 };
47
48 /// Constructs a RegexMatcher to work on a given string.
49 /// Passes the optional parameters to method #Compile.
50 /// @param pattern The string pattern to match.
51 /// Defaults to \b NULL_STRING to allow parameterless construction, with
52 /// later invocation of #Compile.
53 RegexMatcher( const String& pattern= NULL_STRING ) { Compile( pattern ); }
54
55 /// Resets this object to use the given pattern.
56 /// @param pattern The string pattern to match.
58 void Compile( const String& pattern );
59
60 /// Tests if the given \p{haystack} matches the actual pattern.
61 /// If #Compile was not invoked or an empty pattern string was given, \c true is returned.
62 ///
63 /// @param haystack The string to test.
64 /// @return \c true if given \p{haystack} matches the actual pattern, \c false otherwise.
66 bool Match( const String& haystack );
67
68 /// Searches for the first match of the actual pattern in the given \p{haystack}
69 /// If #Compile was not invoked or an empty pattern string was given, an
70 /// \alib_assertion is raised.
71 /// @param haystack The string to search in..
72 /// @return The (first) range in \b haystack that matches the compiled pattern.
73 /// In case the pattern was not found, <c>{-1,-1}</c> is returned.
75 SRange SearchIn( const String& haystack );
76}; // class RegexMatcher
77
78}} // namespace alib[::strings::util]
79
80/// Type alias in namespace \b alib.
82
83} // namespace [alib]
84
85#endif // ALIB_FEAT_BOOST_REGEX && (!ALIB_CHARACTERS_WIDE || ALIB_CHARACTERS_NATIVE_WCHAR)
boost::basic_regex< character, boost::regex_traits< character > > boostRegex
This is the internal regex matcher.
ALIB_DLL SRange SearchIn(const String &haystack)
ALIB_DLL void Compile(const String &pattern)
RegexMatcher(const String &pattern=NULL_STRING)
ALIB_DLL bool Match(const String &haystack)
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2271
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::util::RegexMatcher RegexMatcher
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
integer Position
The starting index of this range in a string.
integer Length
The length of this range in a string.