📄 pattern.h
字号:
wanted from this <code>Pattern</code> object
@return If successful, <code>compileAndKeep</code> returns a
<code>Pattern</code> pointer. Upon failure, <code>compile</code>
returns <code>NULL</code>.
*/
static Pattern * compileAndKeep (const std::string & pattern,
const unsigned long mode = 0);
/**
Searches through <code>replace</code> and replaces all substrings matched
by <code>pattern</code> with <code>str</code>. <code>str</code> may
contain backreferences (e.g. <code>\1</code>) to capture groups. A typical
invocation looks like:
<p>
<code>
Pattern::replace("(a+)b(c+)", "abcccbbabcbabc", "\\2b\\1");
</code>
<p>
which would replace <code>abcccbbabcbabc</code> with
<code>cccbabbcbabcba</code>.
@param pattern The regular expression
@param str The replacement text
@param replacementText The string in which to perform replacements
@param mode The special mode requested of the <code>Pattern</code>
during the replacement process
@return The text with the replacement string substituted where necessary
*/
static std::string replace (const std::string & pattern,
const std::string & str,
const std::string & replacementText,
const unsigned long mode = 0);
/**
Splits the specified string over occurrences of the specified pattern.
Empty strings can be optionally ignored. The number of strings returned is
configurable. A typical invocation looks like:
<p>
<code>
std::string str(strSize, '\0');<br>
FILE * fp = fopen(fileName, "r");<br>
fread((char*)str.data(), strSize, 1, fp);<br>
fclose(fp);<br>
<br>
std::vector<std::string> lines = Pattern::split("[\r\n]+", str, true);<br>
<br>
</code>
@param pattern The regular expression
@param replace The string to split
@param keepEmptys Whether or not to keep empty strings
@param limit The maximum number of splits to make
@param mode The special mode requested of the <code>Pattern</code>
during the split process
@return All substrings of <code>str</code> split across <code>pattern</code>.
*/
static std::vector<std::string> split (const std::string & pattern,
const std::string & str,
const bool keepEmptys = 0,
const unsigned long limit = 0,
const unsigned long mode = 0);
/**
Finds all the instances of the specified pattern within the string. You
should be careful to only pass patterns with a minimum length of one. For
example, the pattern <code>a*</code> can be matched by an empty string, so
instead you should pass <code>a+</code> since at least one character must
be matched. A typical invocation of <code>findAll</code> looks like:
<p>
<code>
std::vector<td::string> numbers = Pattern::findAll("\\d+", string);
</code>
<p>
@param pattern The pattern for which to search
@param str The string to search
@param mode The special mode requested of the <code>Pattern</code>
during the find process
@return All instances of <code>pattern</code> in <code>str</code>
*/
static std::vector<std::string> findAll (const std::string & pattern,
const std::string & str,
const unsigned long mode = 0);
/**
Determines if an entire string matches the specified pattern
@param pattern The pattern for to match
@param str The string to match
@param mode The special mode requested of the <code>Pattern</code>
during the replacement process
@return True if <code>str</code> is recognized by <code>pattern</code>
*/
static bool matches (const std::string & pattern,
const std::string & str,
const unsigned long mode = 0);
/**
Registers a pattern under a specific name for use in later compilations.
A typical invocation and later use looks like:
<p>
<code>
Pattern::registerPattern("ip", "(?:\\d{1,3}\\.){3}\\d{1,3}");<br>
Pattern * p1 = Pattern::compile("{ip}:\\d+");<br>
Pattern * p2 = Pattern::compile("Connection from ({ip}) on port \\d+");<br>
</code>
<p>
Multiple calls to <code>registerPattern</code> with the same
<code>name</code> will result in the pattern getting overwritten.
@param name The name to give to the pattern
@param pattern The pattern to register
@param mode Any special flags to use when compiling pattern
@return Success/Failure. Fails only if <code>pattern</code> has invalid
syntax
*/
static bool registerPattern(const std::string & name,
const std::string & pattern,
const unsigned long mode = 0);
/**
Clears the pattern registry
*/
static void unregisterPatterns();
/**
Don't use
*/
static void clearPatternCache();
/**
Searches through a string for the <code>n<sup>th</sup></code> match of the
given pattern in the string. Match indeces start at zero, not one.
A typical invocation looks like this:
<p>
<code>
std::pair<std::string, int> match = Pattern::findNthMatch("\\d{1,3}", "192.168.1.101:22", 1);<br>
printf("%s %i\n", match.first.c_str(), match.second);<br>
<br>
Output: 168 4<br>
<br>
@param pattern The pattern for which to search
@param str The string to search
@param matchNum Which match to find
@param mode Any special flags to use during the matching process
@return A string and an integer. The string is the string matched. The
integer is the starting location of the matched string in
<code>str</code>. You can check for success/failure by making sure
that the integer returned is greater than or equal to zero.
*/
static std::pair<std::string, int> findNthMatch (const std::string & pattern,
const std::string & str,
const int matchNum,
const unsigned long mode = 0);
public:
/**
Deletes all NFA nodes allocated during compilation
*/
~Pattern();
std::string replace (const std::string & str,
const std::string & replacementText);
std::vector<std::string> split (const std::string & str, const bool keepEmptys = 0,
const unsigned long limit = 0);
std::vector<std::string> findAll (const std::string & str);
bool matches (const std::string & str);
/**
Returns the flags used during compilation of this pattern
@return The flags used during compilation of this pattern
*/
unsigned long getFlags () const;
/**
Returns the regular expression this pattern represents
@return The regular expression this pattern represents
*/
std::string getPattern () const;
/**
Creates a matcher object using the specified string and this pattern.
@param str The string to match against
@return A new matcher using object using this pattern and the specified
string
*/
Matcher * createMatcher (const std::string & str);
};
class NFANode
{
friend class Matcher;
public:
NFANode * next;
NFANode();
virtual ~NFANode();
virtual void findAllNodes(std::map<NFANode*, bool> & soFar);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const = 0;
inline virtual bool isGroupHeadNode() const { return false; }
inline virtual bool isStartOfInputNode() const { return false; }
};
class NFACharNode : public NFANode
{
protected:
char ch;
public:
NFACharNode(const char c);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFACICharNode : public NFANode
{
protected:
char ch;
public:
NFACICharNode(const char c);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAStartNode : public NFANode
{
public:
NFAStartNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAEndNode : public NFANode
{
public:
NFAEndNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAQuantifierNode : public NFANode
{
public:
int min, max;
NFANode * inner;
virtual void findAllNodes(std::map<NFANode*, bool> & soFar);
NFAQuantifierNode(Pattern * pat, NFANode * internal,
const int minMatch = Pattern::MIN_QMATCH,
const int maxMatch = Pattern::MAX_QMATCH);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAGreedyQuantifierNode : public NFAQuantifierNode
{
public:
NFAGreedyQuantifierNode(Pattern * pat, NFANode * internal,
const int minMatch = Pattern::MIN_QMATCH,
const int maxMatch = Pattern::MAX_QMATCH);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
virtual int matchInternal(const std::string & str, Matcher * matcher, const int curInd, const int soFar) const;
};
class NFALazyQuantifierNode : public NFAQuantifierNode
{
public:
NFALazyQuantifierNode(Pattern * pat, NFANode * internal,
const int minMatch = Pattern::MIN_QMATCH,
const int maxMatch = Pattern::MAX_QMATCH);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAPossessiveQuantifierNode : public NFAQuantifierNode
{
public:
NFAPossessiveQuantifierNode(Pattern * pat, NFANode * internal,
const int minMatch = Pattern::MIN_QMATCH,
const int maxMatch = Pattern::MAX_QMATCH);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAAcceptNode : public NFANode
{
public:
NFAAcceptNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAClassNode : public NFANode
{
public:
bool inv;
std::map<char, bool> vals;
NFAClassNode(const bool invert = 0);
NFAClassNode(const std::string & clazz, const bool invert);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFACIClassNode : public NFANode
{
public:
bool inv;
std::map<char, bool> vals;
NFACIClassNode(const bool invert = 0);
NFACIClassNode(const std::string & clazz, const bool invert);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFASubStartNode : public NFANode
{
public:
NFASubStartNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAOrNode : public NFANode
{
public:
NFANode * one;
NFANode * two;
NFAOrNode(NFANode * first, NFANode * second);
virtual void findAllNodes(std::map<NFANode*, bool> & soFar);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAQuoteNode : public NFANode
{
public:
std::string qStr;
NFAQuoteNode(const std::string & quoted);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFACIQuoteNode : public NFANode
{
public:
std::string qStr;
NFACIQuoteNode(const std::string & quoted);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFALookAheadNode : public NFANode
{
public:
bool pos;
NFANode * inner;
NFALookAheadNode(NFANode * internal, const bool positive);
virtual void findAllNodes(std::map<NFANode*, bool> & soFar);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFALookBehindNode : public NFANode
{
public:
bool pos;
std::string mStr;
NFALookBehindNode(const std::string & str, const bool positive);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAStartOfLineNode : public NFANode
{
public:
NFAStartOfLineNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAEndOfLineNode : public NFANode
{
public:
NFAEndOfLineNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAReferenceNode : public NFANode
{
public:
int gi;
NFAReferenceNode(const int groupIndex);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAStartOfInputNode : public NFANode
{
public:
NFAStartOfInputNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
inline virtual bool isStartOfInputNode() const { return true; }
};
class NFAEndOfInputNode : public NFANode
{
public:
bool term;
NFAEndOfInputNode(const bool lookForTerm);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAWordBoundaryNode : public NFANode
{
public:
bool pos;
NFAWordBoundaryNode(const bool positive);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAEndOfMatchNode : public NFANode
{
public:
NFAEndOfMatchNode();
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAGroupHeadNode : public NFANode
{
public:
int gi;
NFAGroupHeadNode(const int groupIndex);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
inline virtual bool isGroupHeadNode() const { return true; }
};
class NFAGroupTailNode : public NFANode
{
public:
int gi;
NFAGroupTailNode(const int groupIndex);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAGroupLoopPrologueNode : public NFANode
{
public:
int gi;
NFAGroupLoopPrologueNode(const int groupIndex);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
class NFAGroupLoopNode : public NFANode
{
public:
int gi, min, max, type;
NFANode * inner;
NFAGroupLoopNode(NFANode * internal, const int minMatch,
const int maxMatch, const int groupIndex, const int matchType);
virtual void findAllNodes(std::map<NFANode*, bool> & soFar);
virtual int match(const std::string & str, Matcher * matcher, const int curInd = 0) const;
int matchGreedy(const std::string & str, Matcher * matcher, const int curInd = 0) const;
int matchLazy(const std::string & str, Matcher * matcher, const int curInd = 0) const;
int matchPossessive(const std::string & str, Matcher * matcher, const int curInd = 0) const;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -