📄 pattern.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>class Pattern</TITLE>
<META NAME="GENERATOR" CONTENT="DOC++ 3.4.10">
</HEAD>
<BODY BGCOLOR="#ffffff">
<TABLE BORDER=0><TR>
<TD VALIGN=TOP><H2>class <A HREF="#DOC.DOCU">Pattern</A></H2></TD></H2></TD></TR></TABLE>
<BLOCKQUOTE>A class used to represent "PERL 5"-ish regular expressions</BLOCKQUOTE>
<HR>
<DL>
<P><TABLE>
<DT><H3>Public Fields</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.33"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const unsigned long </TD><TD><B>CASE_INSENSITIVE</B> <BR>
<I>We should match regardless of case</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.35"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const unsigned long </TD><TD><B>DOT_MATCHES_ALL</B> <BR>
<I>We should treat a <code><b>.</b></code> as [\x00-\x7F]</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.34"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const unsigned long </TD><TD><B>LITERAL</B> <BR>
<I>We are implicitly quoted</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.39"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const int </TD><TD><B>MAX_QMATCH</B> <BR>
<I>The absolute maximum number of matches a quantifier can match (0x7FFFFFFF)</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.38"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const int </TD><TD><B>MIN_QMATCH</B> <BR>
<I>The absolute minimum number of matches a quantifier can match (0)</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.36"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const unsigned long </TD><TD><B>MULTILINE_MATCHING</B> <BR>
<I><code>^</code> and <code>$</code> should anchor to the beginning and ending of lines, not all input</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.37"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const unsigned long </TD><TD><B>UNIX_LINE_MODE</B> <BR>
<I>When enabled, only instances of <code>\n</codes> are recognized as line terminators</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Public Methods</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.48"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static void </TD><TD><B>clearPatternCache</B> ()<BR>
<I> Don't use</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.40"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static <!1><A HREF="Pattern.html">Pattern</A>* </TD><TD><B>compile</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const unsigned long mode = 0)<BR>
<I> Call this function to compile a regular expression into a <code>Pattern</code> object.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.41"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static <!1><A HREF="Pattern.html">Pattern</A>* </TD><TD><B>compileAndKeep</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const unsigned long mode = 0)<BR>
<I> Dont use this function.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.53"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A><!1><A HREF="Matcher.html">Matcher</A>* </TD><TD><B>createMatcher</B> (const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>)<BR>
<I> Creates a matcher object using the specified string and this pattern.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.44"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::vector<std::string> </TD><TD><B>findAll</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>, const unsigned long mode = 0)<BR>
<I> Finds all the instances of the specified pattern within the string.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.49"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::pair<std::string, int> </TD><TD><B>findNthMatch</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>, const int matchNum, const unsigned long mode = 0)<BR>
<I> Searches through a string for the <code>n<sup>th</sup></code> match of the given pattern in the string.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.51"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>unsigned long </TD><TD><B>getFlags</B> () const <BR>
<I> Returns the flags used during compilation of this pattern </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.52"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>getPattern</B> () const <BR>
<I> Returns the regular expression this pattern represents </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.45"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static bool </TD><TD><B>matches</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>, const unsigned long mode = 0)<BR>
<I> Determines if an entire string matches the specified pattern</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.46"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static bool </TD><TD><B>registerPattern</B> (const std::string & name, const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const unsigned long mode = 0)<BR>
<I> Registers a pattern under a specific name for use in later compilations.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.42"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::string </TD><TD><B>replace</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const std::string & <!1><A HREF="Pattern.html#DOC.3.42">replace</A>, const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>, const unsigned long mode = 0)<BR>
<I> Searches through <code>replace</code> and replaces all substrings matched by <code>pattern</code> with <code>str</code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.43"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::vector<std::string> </TD><TD><B>split</B> (const std::string & <!1><A HREF="Pattern.html#DOC.3.7">pattern</A>, const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>, const bool keepEmptys = 0, const unsigned long limit = 0, const unsigned long mode = 0)<BR>
<I> Splits the specified string over occurrences of the specified pattern.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.47"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static void </TD><TD><B>unregisterPatterns</B> ()<BR>
<I> Clears the pattern registry</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.50"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A> </TD><TD><B>~Pattern</B> ()<BR>
<I> Deletes all NFA nodes allocated during compilation</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Protected Fields</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.2"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::map<std::string, <!1><A HREF="Pattern.html">Pattern</A> *> </TD><TD><B>compiledPatterns</B> <BR>
<I>Holds all the compiled patterns for quick access.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.9"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>curInd</B> <BR>
<I> Used during compilation to keep track of the current index into <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A><code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.8"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>bool </TD><TD><B>error</B> <BR>
<I> Flag used during compilation.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.12"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>unsigned long </TD><TD><B>flags</B> <BR>
<I> The flags specified when this was compiled</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.10"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>groupCount</B> <BR>
<I> The number of capture groups this contains</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.6"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>head</B> <BR>
<I> The front node of the NFA</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.5"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A><!1><A HREF="Matcher.html">Matcher</A>* </TD><TD><B>matcher</B> <BR>
<I> Used when methods like split are called.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.4"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::map<NFANode*, bool> </TD><TD><B>nodes</B> <BR>
<I> Holds all the NFA nodes used.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.11"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>nonCapGroupCount</B> <BR>
<I> The number of non-capture groups this contains</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.7"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>pattern</B> <BR>
<I> The actual regular expression we rerpesent</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.3"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static std::map<std::string, std::pair<std::string, unsigned long> > </TD><TD><B>registeredPatterns</B> <BR>
<I> Holds all of the registered patterns as strings.</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Protected Methods</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.18"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>classCreateRange</B> (char low, char hi) const <BR>
<I> Creates a new "class" representing the range from <code>low</code> thru <code>hi</code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.16"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>classIntersect</B> (std::string s1, std::string s2) const <BR>
<I> Calculates the intersection of two strings.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.17"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>classNegate</B> (std::string s1) const <BR>
<I> Calculates the negation of a string.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.15"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>classUnion</B> (std::string s1, std::string s2) const <BR>
<I> Calculates the union of two strings.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.19"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>getInt</B> (int <!1><A HREF="Matcher.html#DOC.2.4">start</A>, int end)<BR>
<I> Extracts a decimal number from the substring of member-variable <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A><code> starting at <code>start</code> and ending at <code>end</code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.32"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>parse</B> (const bool inParen = 0, const bool inOr = 0, NFANode** end = NULL)<BR>
<I> Parses <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A></code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.27"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>parseBackref</B> ()<BR>
<I> Returns a new node representing the back reference being parsed </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.30"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>parseBehind</B> (const bool pos, NFANode** end)<BR>
<I> Parses a lookbehind expression.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.23"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>parseClass</B> ()<BR>
<I> Parses the current class being examined in <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A></code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.28"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>parseEscape</B> (bool & inv, bool & quo)<BR>
<I> Parses the escape sequence currently being examined.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.26"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>parseHex</B> ()<BR>
<I> Returns a string containing the hex character being parsed </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.25"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>parseOctal</B> ()<BR>
<I> Returns a string containing the octal character being parsed </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.24"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>parsePosix</B> ()<BR>
<I> Parses the current POSIX class being examined in <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A></code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.31"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>parseQuote</B> ()<BR>
<I> Parses the current expression and tacks on nodes until a \E is found.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.29"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>parseRegisteredPattern</B> (NFANode** end)<BR>
<I> Parses a supposed registered pattern currently under compilation.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.22"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>quantify</B> (NFANode* newNode)<BR>
<I> Tries to quantify the last parsed expression.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.20"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>bool </TD><TD><B>quantifyCurly</B> (int & sNum, int & eNum)<BR>
<I> Parses a <code>{n,m}</code> string out of the member-variable <code><!1><A HREF="Pattern.html#DOC.3.7">pattern</A><code> stores the result in <code>sNum</code> and <code>eNum</code>.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.21"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>quantifyGroup</B> (NFANode* <!1><A HREF="Matcher.html#DOC.2.4">start</A>, NFANode* stop, const int gn)<BR>
<I> Tries to quantify the currently parsed group.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.13"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>void </TD><TD><B>raiseError</B> ()<BR>
<I> Raises an error during compilation.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.3.14"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>NFANode* </TD><TD><B>registerNode</B> (NFANode* node)<BR>
<I> Convenience function for registering a node in <code>nodes</code>.</I>
</TD></TR></TABLE></P>
</DL>
<A NAME="DOC.DOCU"></A>
<HR>
<H2>Documentation</H2>
<BLOCKQUOTE>
This pattern class is very similar in functionality to Java's
javautilregexPattern class. The pattern class represents an immutable
regular expression object. Instead of having a single object contain both the
regular expression object and the matching object, instead the two objects are
split apart. The <!1><A HREF="Matcher.html">Matcher</A> class represents the maching
object.
<P>The Pattern class works primarily off of "compiled" patterns. A typical
instantiation of a regular expression looks like:
<P><PRE>
Pattern * p = Pattern::compile("a*b");
Matcher * m = p->createMatcher("aaaaaab");
if (m->matches()) ...
</PRE>
<P>However, if you do not need to use a pattern more than once, it is often times
okay to use the Pattern's static methods insteads. An example looks like this:
<P><PRE>
if (Pattern::matches("a*b", "aaaab")) { ... }
</PRE>
<P>This class does not currently support unicode. The unicode update for this
class is coming soon.
<P>This class is partially immutable. It is completely safe to call createMatcher
concurrently in different threads, but the other functions (e.g. split) should
not be called concurrently on the same <code>Pattern</code>.
<P><table border="0" cellpadding="1" cellspacing="0">
<tr align="left" bgcolor="#CCCCFF">
<td>
<b>Construct</b>
</td>
<td>
<b>Matches</b>
</th>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<b>Characters</b>
</td>
</tr>
<tr>
<td>
<code><i>x</i></code>
</td>
<td>
The character <code><i>x</i></code>
</td>
</tr>
<tr>
<td>
<code>\\</code>
</td>
<td>
The character <code>\</code>
</td>
</tr>
<tr>
<td>
<code>\0<i>nn</i></code>
</td>
<td>
The character with octal ASCII value <code><i>nn</i></code>
</td>
</tr>
<tr>
<td>
<code>\0<i>nnn</i></code>
</td>
<td>
The character with octal ASCII value <code><i>nnn</i></code>
</td>
</tr>
<tr>
<td>
<code>\x<i>hh</i></code>
</td>
<td>
The character with hexadecimal ASCII value <code><i>hh</i></code>
</td>
</tr>
<tr>
<td>
<code>\t</code>
</td>
<td>
A tab character
</td>
</tr>
<tr>
<td>
<code>\r</code>
</td>
<td>
A carriage return character
</td>
</tr>
<tr>
<td>
<code>\n</code>
</td>
<td>
A new-line character
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td>
<b>Character Classes</b>
</td>
</tr>
<tr>
<td>
<code>[abc]</code>
</td>
<td>
Either <code>a</code>, <code>b</code>, or <code>c</code>
</td>
</tr>
<tr>
<td>
<code>[^abc]</code>
</td>
<td>
Any character but <code>a</code>, <code>b</code>, or <code>c</code>
</td>
</tr>
<tr>
<td>
<code>[a-zA-Z]</code>
</td>
<td>
Any character ranging from <code>a</code> thru <code>z</code>, or
<code>A</code> thru <code>Z</code>
</td>
</tr>
<tr>
<td>
<code>[^a-zA-Z]</code>
</td>
<td>
Any character except those ranging from <code>a</code> thru
<code>z</code>, or <code>A</code> thru <code>Z</code>
</td>
</tr>
<tr>
<td>
<code>[a\-z]</code>
</td>
<td>
Either <code>a</code>, <code>-</code>, or <code>z</code>
</td>
</tr>
<tr>
<td>
<code>[a-z[A-Z]]</code>
</td>
<td>
Same as <code>[a-zA-Z]</code>
</td>
</tr>
<tr>
<td>
<code>[a-z&&[g-i]]</code>
</td>
<td>
Any character in the intersection of <code>a-z</code> and
<code>g-i</code>
</td>
</tr>
<tr>
<td>
<code>[a-z&&[^g-i]]</code>
</td>
<td>
Any character in <code>a-z</code> and not in <code>g-i</code>
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<b>Prefefined character classes</b>
</td>
</tr>
<tr>
<td>
<code><b>.</b></code>
</td>
<td>
Any character. Multiline matching must be compiled into the pattern for
<code><b>.</b></code> to match a <code>\r</code> or a <code>\n</code>.
Even if multiline matching is enabled, <code><b>.</b></code> will not
match a <code>\r\n</code>, only a <code>\r</code> or a <code>\n</code>.
</td>
</tr>
<tr>
<td>
<code>\d</code>
</td>
<td>
<code>[0-9]</code>
</td>
</tr>
<tr>
<td>
<code>\D</code>
</td>
<td>
<code>[^\d]</code>
</td>
</tr>
<tr>
<td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -