📄 matcher.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>class Matcher</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">Matcher</A></H2></TD></H2></TD></TR></TABLE>
<BLOCKQUOTE>Mutable object used on instances of a Pattern class</BLOCKQUOTE>
<HR>
<DL>
<P><TABLE>
<DT><H3>Public Fields</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.16"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>static const int </TD><TD><B>MATCH_ENTIRE_STRING</B> <BR>
<I>Used internally by match to signify we want the entire string matched</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Public Methods</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.24"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::vector<std::string> </TD><TD><B>findAll</B> ()<BR>
<I> Returns a vector of every substring in order which matches the given pattern.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.22"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>bool </TD><TD><B>findFirstMatch</B> ()<BR>
<I> Scans the string for the first substring matching the pattern.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.23"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>bool </TD><TD><B>findNextMatch</B> ()<BR>
<I> Scans the string for the next substring matching the pattern.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.29"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>getEndingIndex</B> (const int groupNum = 0) const <BR>
<I> Returns the ending index of the specified group.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.19"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>unsigned long </TD><TD><B>getFlags</B> () const <BR>
<I> The flags currently being used by the matcher.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.30"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>getGroup</B> (const int groupNum = 0) const <BR>
<I> Returns the specified group.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.31"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::vector<std::string> </TD><TD><B>getGroups</B> (const bool includeGroupZero = 0) const <BR>
<I> Returns every capture group in a vector</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.28"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>getStartingIndex</B> (const int groupNum = 0) const <BR>
<I> Returns the starting index of the specified group.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.26"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>inline std::string </TD><TD><B>getString</B> () const <BR>
<I> Same as getText.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.20"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>getText</B> () const <BR>
<I> The text being searched by the matcher.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.21"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>bool </TD><TD><B>matches</B> ()<BR>
<I> Scans the string from start to finish for a match.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.18"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>replaceWithGroups</B> (const std::string & <!1><A HREF="Matcher.html#DOC.2.3">str</A>)<BR>
<I> Replaces the contents of <code>str</code> with the appropriate captured text.</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.25"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>void </TD><TD><B>reset</B> ()<BR>
<I> Resets the internal state of the matcher</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.27"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>inline void </TD><TD><B>setString</B> (const std::string & newStr)<BR>
<I> Sets the string to scan </I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.17"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A> </TD><TD><B>~Matcher</B> ()<BR>
<I>Cleans up the dynamic memory used by this matcher</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Protected Fields</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.6"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int* </TD><TD><B>ends</B> <BR>
<I>An array of the ending positions for each group</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.14"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>unsigned long </TD><TD><B>flags</B> <BR>
<I>The flags with which we were made</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.11"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>gc</B> <BR>
<I>The number of capturing groups we have</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.8"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int* </TD><TD><B>groupIndeces</B> <BR>
<I>An array of private data used by NFANodes during matching</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.9"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int* </TD><TD><B>groupPos</B> <BR>
<I>An array of private data used by NFANodes during matching</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.7"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int* </TD><TD><B>groups</B> <BR>
<I>An array of private data used by NFANodes during matching</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.10"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>lm</B> <BR>
<I>The ending index of the last match</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.13"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>matchedSomething</B> <BR>
<I>Whether or not we have matched something (used only by findFirstMatch and findNextMatch)</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.12"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>ncgc</B> <BR>
<I>The number of non-capturing groups we havew</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.2"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A><!1><A HREF="Pattern.html">Pattern</A>* </TD><TD><B>pat</B> <BR>
<I>The pattern we use to match</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.4"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int </TD><TD><B>start</B> <BR>
<I>The starting point of our match</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.5"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>int* </TD><TD><B>starts</B> <BR>
<I>An array of the starting positions for each group</I>
</TD></TR><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.3"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>std::string </TD><TD><B>str</B> <BR>
<I>The string in which we are matching</I>
</TD></TR></TABLE></P>
<P><TABLE>
<DT><H3>Protected Methods</H3><DD><TR>
<TD VALIGN=TOP><A HREF="#DOC.2.15"><IMG ALT="[more]" BORDER=0 SRC=icon1.gif></A>void </TD><TD><B>clearGroups</B> ()<BR>
<I>Called by reset to clear the group arrays</I>
</TD></TR></TABLE></P>
</DL>
<A NAME="DOC.DOCU"></A>
<HR>
<H2>Documentation</H2>
<BLOCKQUOTE>
A matcher is a non thread-safe object used to scan strings using a given
<!1><A HREF="Pattern.html">Pattern</A> object. Using a <code>Matcher</code> is the preferred
method for scanning strings. Matchers are not thread-safe. Matchers require
very little dynamic memory, hence one is encouraged to create several
instances of a matcher when necessary as opposed to sharing a single instance
of a matcher.
<p>
The most common methods needed by the matcher are <code>matches</code>,
<code>findNextMatch</code>, and <code>getGroup</code>. <code>matches</code>
and <code>findNextMatch</code> both return success or failure, and further
details can be gathered from their documentation.
<p>
Unlike Java's <code>Matcher</code>, this class allows you to change the string
you are matching against. This provides a small optimization, since you no
longer need multiple matchers for a single pattern in a single thread.
<p>
This class also provides an extremely handy method for replacing text with
captured data via the <code>replaceWithGroups</code> method. A typical
invocation looks like:
<PRE>
char buf[10000];
std::string str = "\\5 (user name \\1) uses \\7 for his/her shell and \\6 is their home directory";
FILE * fp = fopen("/etc/passwd", "r");
Pattern::registerPattern("entry", "[^:]+");
Pattern * p = Pattern::compile("^({entry}):({entry}):({entry}):({entry}):({entry}):({entry}):({entry})$",
Pattern::MULTILINE_MATCHING | Pattern::UNIX_LINE_MODE);
Matcher * m = p->createMatcher("");
while (fgets(buf, 9999, fp))
{
m->setString(buf);
if (m->matches())
{
printf("%s\n", m->replaceWithGroups(str).c_str());
}
}
fclose(fp);
<P> </PRE>
Calling any of the following functions before first calling
<code>matches</code>, <code>findFirstMatch</code>, or
<code>findNextMatch</code> results in undefined behavior and may cause your
program to crash.
<code>
<ul>
<li>replaceWithGroups</code>
<li>getStartingIndex</li>
<li>getEndingIndex</li>
<li>getGroup</li>
<li>getGroups</li>
</ul>
</code>
<p>
The function <code>findFirstMatch</code> will attempt to find the first match
in the input string. The same results can be obtained by first calling
<code>reset</code> followed by <code>findNextMatch</code>.
<p>
To eliminate the necessity of looping through a string to find all the
matching substrings, <code>findAll</code> was created. The function will find
all matching substrings and return them in a <code>vector</code>. If you need
to examine specific capture groups within the substrings, then this method
should not be used.
<P></BLOCKQUOTE>
<DL>
<A NAME="pat"></A>
<A NAME="DOC.2.2"></A>
<DT><IMG ALT="o" BORDER=0 SRC=icon2.gif><TT><B><!1><A HREF="Pattern.html">Pattern</A>* pat</B></TT>
<DD>The pattern we use to match
<DL><DT><DD></DL><P>
<A NAME="str"></A>
<A NAME="DOC.2.3"></A>
<DT><IMG ALT="o" BORDER=0 SRC=icon2.gif><TT><B>std::string str</B></TT>
<DD>The string in which we are matching
<DL><DT><DD></DL><P>
<A NAME="start"></A>
<A NAME="DOC.2.4"></A>
<DT><IMG ALT="o" BORDER=0 SRC=icon2.gif><TT><B>int start</B></TT>
<DD>The starting point of our match
<DL><DT><DD></DL><P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -