📄 regularexpression.h
字号:
//
// RegularExpression.h
//
// $Id: //poco/Main/Foundation/include/Foundation/RegularExpression.h#5 $
//
// Definitions of class RegularExpression.
//
// A wrapper class for Philip Hazel's PCRE - Perl Compatible Regular Expressions
// library (http://www.pcre.org).
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_RegularExpression_INCLUDED
#define Foundation_RegularExpression_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
//
// Copy these definitions from pcre.h
// to avoid pulling in the entire header file
//
extern "C"
{
struct real_pcre;
typedef struct real_pcre pcre;
struct pcre_extra;
}
Foundation_BEGIN
class Foundation_API RegularExpression
/// A class for working with regular expressions.
/// Implemented using PCRE, the Perl Compatible
/// Regular Expressions library by Philip Hazel
/// (see http://www.pcre.org).
///
/// An overload of operator ^ is provided for
/// simple matching.
{
public:
enum Options // These must match the corresponsing options in pcre.h!
/// See the PCRE documentation for more information.
{
RE_CASELESS = 0x00001, /// case insensitive matching (/i)
RE_MULTILINE = 0x00002, /// enable multi-line mode; affects ^ and $ (/m)
RE_DOTALL = 0x00004, /// dot matches all characters, including newline (/s)
RE_EXTENDED = 0x00004, /// totally ignore whitespace (/x)
RE_ANCHORED = 0x00010, /// treat pattern as if it starts with a ^
RE_DOLLAR_ENDONLY = 0x00020, /// dollar matches end-of-string only, not last newline in string
RE_EXTRA = 0x00040, /// enable optional PCRE functionality
RE_NOTBOL = 0x00080, /// circumflex does not match beginning of string
RE_NOTEOL = 0x00100, /// $ does not match end of string
RE_UNGREEDY = 0x00200, /// make quantifiers ungreedy
RE_NOTEMPTY = 0x00400, /// empty string never matches
RE_UTF8 = 0x00800, /// assume pattern and subject is UTF-8 encoded
RE_NO_AUTO_CAPTURE = 0x01000, /// disable numbered capturing parentheses
RE_NO_UTF8_CHECK = 0x02000, /// do not check validity of UTF-8 code sequences
RE_GLOBAL = 0x10000, /// replace all occurences (/g)
RE_NO_VARS = 0x20000 /// treat dollar in replacement string as ordinary character
};
struct Match
{
std::string::size_type offset; /// zero based offset (std::string::npos if subexpr does not match)
std::string::size_type length; /// length of substring
};
typedef std::vector<Match> MatchVec;
RegularExpression(const std::string& pattern, int options = 0, bool study = true);
/// Creates a regular expression and parses the given pattern.
/// If study is true, the pattern is analyzed and optimized. This
/// is mainly useful if the pattern is used more than once.
/// For a description of the options, please see the PCRE documentation.
/// Throws a RegularExpressionException if the patter cannot be compiled.
~RegularExpression();
/// Destroys the regular expression.
int match(const std::string& subject, Match& mtch, int options = 0) const;
/// Matches the given subject string against the pattern. Returns the position
/// of the first captured substring in mtch.
/// If no part of the subject matches the pattern, mtch.offset is std::string::npos and
/// mtch.length is 0.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int match(const std::string& subject, std::string::size_type offset, Match& mtch, int options = 0) const;
/// Matches the given subject string, starting at offset, against the pattern.
/// Returns the position of the captured substring in mtch.
/// If no part of the subject matches the pattern, mtch.offset is std::string::npos and
/// mtch.length is 0.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int match(const std::string& subject, std::string::size_type offset, MatchVec& matches, int options = 0) const;
/// Matches the given subject string against the pattern.
/// The first entry in matches contains the position of the captured substring.
/// The following entries identify matching subpatterns. See the PCRE documentation
/// for a more detailed explanation.
/// If no part of the subject matches the pattern, matches is empty.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
bool match(const std::string& subject, std::string::size_type offset = 0) const;
/// Returns true if and only if the subject matches the regular expression.
int extract(const std::string& subject, std::string& str, int options = 0) const;
/// Matches the given subject string against the pattern.
/// Returns the captured string.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int extract(const std::string& subject, std::string::size_type offset, std::string& str, int options = 0) const;
/// Matches the given subject string, starting at offset, against the pattern.
/// Returns the captured string.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int split(const std::string& subject, std::vector<std::string>& strings, int options = 0) const;
/// Matches the given subject string against the pattern.
/// The first entry in captured is the captured substring.
/// The following entries contain substrings matching subpatterns. See the PCRE documentation
/// for a more detailed explanation.
/// If no part of the subject matches the pattern, captured is empty.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int split(const std::string& subject, std::string::size_type offset, std::vector<std::string>& strings, int options = 0) const;
/// Matches the given subject string against the pattern.
/// The first entry in captured is the captured substring.
/// The following entries contain substrings matching subpatterns. See the PCRE documentation
/// for a more detailed explanation.
/// If no part of the subject matches the pattern, captured is empty.
/// Throws a RegularExpressionException in case of an error.
/// Returns the number of matches.
int subst(std::string& subject, const std::string& replacement, int options = 0) const;
/// Substitute in subject all matches of the pattern with replacement.
/// If RE_GLOBAL is specified as option, all matches are replaced. Otherwise,
/// only the first match is replaced.
/// Occurences of $<n> (for example, $1, $2, ...) in replacement are replaced
/// with the corresponding captured string. $0 is the original subject string.
/// Returns the number of replaced occurences.
int subst(std::string& subject, std::string::size_type offset, const std::string& replacement, int options = 0) const;
/// Substitute in subject all matches of the pattern with replacement,
/// starting at offset.
/// If RE_GLOBAL is specified as option, all matches are replaced. Otherwise,
/// only the first match is replaced.
/// Unless RE_NO_VARS is specified, occurences of $<n> (for example, $0, $1, $2, ... $9)
/// in replacement are replaced with the corresponding captured string.
/// $0 is the captured substring. $1 ... $n are the substrings maching the subpatterns.
/// Returns the number of replaced occurences.
static bool match(const std::string& subject, const std::string& pattern, int options = 0);
/// Matches the given subject string against the regular expression given in pattern,
/// using the given options.
protected:
std::string::size_type substOne(std::string& subject, std::string::size_type offset, const std::string& replacement, int options) const;
private:
pcre* _pcre;
pcre_extra* _extra;
static const int OVEC_SIZE;
RegularExpression();
RegularExpression(const RegularExpression&);
RegularExpression& operator = (const RegularExpression&);
};
//
// inlines
//
inline int RegularExpression::match(const std::string& subject, Match& mtch, int options) const
{
return match(subject, 0, mtch, options);
}
inline int RegularExpression::split(const std::string& subject, std::vector<std::string>& strings, int options) const
{
return split(subject, 0, strings, options);
}
inline int RegularExpression::subst(std::string& subject, const std::string& replacement, int options) const
{
return subst(subject, 0, replacement, options);
}
Foundation_END
#endif // Foundation_RegularExpression_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -