⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wcpattern.h

📁 C++正则表达式解析
💻 H
📖 第 1 页 / 共 4 页
字号:
      @param pattern The regular expression to compile
      @param mode    A bitwise or of flags signalling what special behaviors are
                     wanted from this <code>WCPattern</code> object
      @return If successful, <code>compileAndKeep</code> returns a
              <code>WCPattern</code> pointer. Upon failure, <code>compile</code>
              returns <code>NULL</code>.
     */
    static WCPattern                    * compileAndKeep (const std::wstring & 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>
      WCPattern::replace(L"(a+)b(c+)", L"abcccbbabcbabc", L"\\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>WCPattern</code>
                              during the replacement process
      @return The text with the replacement string substituted where necessary
     */
    static std::wstring                  replace       (const std::wstring & pattern,
                                                        const std::wstring & str,
                                                        const std::wstring & 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::wstring str(strSize, 0);<br>
      FILE * fp = fopen(fileName, "r");<br>
      fread((char*)str.data(), strSize * 2, 1, fp);<br>
      fclose(fp);<br>
      <br>
      std::vector&lt;std::wstring&gt; lines = WCPattern::split(L"[\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>WCPattern</code>
                        during the split process
      @return All substrings of <code>str</code> split across <code>pattern</code>.
     */
    static std::vector<std::wstring>     split         (const std::wstring & pattern,
                                                        const std::wstring & 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&lt;td::string&gt; numbers = WCPattern::findAll(L"\\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>WCPattern</code>
                      during the find process
      @return All instances of <code>pattern</code> in <code>str</code>
     */
    static std::vector<std::wstring>     findAll       (const std::wstring & pattern,
                                                        const std::wstring & 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>WCPattern</code>
                      during the replacement process
      @return True if <code>str</code> is recognized by <code>pattern</code>
     */
    static bool                         matches        (const std::wstring & pattern,
                                                        const std::wstring & 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>
      WCPattern::registerWCPattern(L"ip", L"(?:\\d{1,3}\\.){3}\\d{1,3}");<br>
      WCPattern * p1 = WCPattern::compile(L"{ip}:\\d+");<br>
      WCPattern * p2 = WCPattern::compile(L"Connection from ({ip}) on port \\d+");<br>
      </code>
      <p>
      Multiple calls to <code>registerWCPattern</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                         registerWCPattern(const std::wstring & name,
                                                        const std::wstring & pattern,
                                                        const unsigned long mode = 0);

    /**
      Clears the pattern registry
      */
    static void                         unregisterWCPatterns();
    /**
      Don't use
     */
    static void                         clearWCPatternCache();

    /**
      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&lt;std::wstring, int&gt; match = WCPattern::findNthMatch(L"\\d{1,3}", L"192.168.1.101:22", 1);<br>
      wprintf(L"%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::wstring, int>  findNthMatch   (const std::wstring & pattern,
                                                        const std::wstring & str,
                                                        const int matchNum,
                                                        const unsigned long mode = 0);
  public:
    /**
      Deletes all NFA nodes allocated during compilation
     */
    ~WCPattern();

    std::wstring               replace       (const std::wstring & str,
                                              const std::wstring & replacementText);
    std::vector<std::wstring>  split         (const std::wstring & str, const bool keepEmptys = 0,
                                              const unsigned long limit = 0);
    std::vector<std::wstring>  findAll       (const std::wstring & str);
    bool                       matches       (const std::wstring & 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::wstring               getWCPattern     () 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
     */
    WCMatcher                 * createWCMatcher  (const std::wstring & str);
};

class NFAUNode
{
  friend class WCMatcher;
  public:
    NFAUNode * next;
    NFAUNode();
    virtual ~NFAUNode();
    virtual void findAllNodes(std::map<NFAUNode*, bool> & soFar);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const = 0;
    inline virtual bool isGroupHeadNode()     const { return false; }
    inline virtual bool isStartOfInputNode()  const { return false; }
};
class NFACharUNode : public NFAUNode
{
  protected:
    wchar_t ch;
  public:
    NFACharUNode(const wchar_t c);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFACICharUNode : public NFAUNode
{
  protected:
    wchar_t ch;
  public:
    NFACICharUNode(const wchar_t c);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAStartUNode : public NFAUNode
{
  public:
    NFAStartUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAEndUNode : public NFAUNode
{
  public:
    NFAEndUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAQuantifierUNode : public NFAUNode
{
  public:
    int min, max;
    NFAUNode * inner;
    virtual void findAllNodes(std::map<NFAUNode*, bool> & soFar);
    NFAQuantifierUNode(WCPattern * pat, NFAUNode * internal,
                      const int minMatch = WCPattern::MIN_QMATCH,
                      const int maxMatch = WCPattern::MAX_QMATCH);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAGreedyQuantifierUNode : public NFAQuantifierUNode
{
  public:
    NFAGreedyQuantifierUNode(WCPattern * pat, NFAUNode * internal,
                            const int minMatch = WCPattern::MIN_QMATCH,
                            const int maxMatch = WCPattern::MAX_QMATCH);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    virtual int matchInternal(const std::wstring & str, WCMatcher * matcher, const int curInd, const int soFar) const;
};
class NFALazyQuantifierUNode : public NFAQuantifierUNode
{
  public:
    NFALazyQuantifierUNode(WCPattern * pat, NFAUNode * internal,
                          const int minMatch = WCPattern::MIN_QMATCH,
                          const int maxMatch = WCPattern::MAX_QMATCH);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAPossessiveQuantifierUNode : public NFAQuantifierUNode
{
  public:
    NFAPossessiveQuantifierUNode(WCPattern * pat, NFAUNode * internal,
                                const int minMatch = WCPattern::MIN_QMATCH,
                                const int maxMatch = WCPattern::MAX_QMATCH);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAAcceptUNode : public NFAUNode
{
  public:
    NFAAcceptUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAClassUNode : public NFAUNode
{
  public:
    bool inv;
    std::map<wchar_t, bool> vals;
    NFAClassUNode(const bool invert = 0);
    NFAClassUNode(const std::wstring & clazz, const bool invert);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFACIClassUNode : public NFAUNode
{
  public:
    bool inv;
    std::map<wchar_t, bool> vals;
    NFACIClassUNode(const bool invert = 0);
    NFACIClassUNode(const std::wstring & clazz, const bool invert);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFASubStartUNode : public NFAUNode
{
  public:
    NFASubStartUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAOrUNode : public NFAUNode
{
  public:
    NFAUNode * one;
    NFAUNode * two;
    NFAOrUNode(NFAUNode * first, NFAUNode * second);
    virtual void findAllNodes(std::map<NFAUNode*, bool> & soFar);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAQuoteUNode : public NFAUNode
{
  public:
    std::wstring qStr;
    NFAQuoteUNode(const std::wstring & quoted);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFACIQuoteUNode : public NFAUNode
{
  public:
    std::wstring qStr;
    NFACIQuoteUNode(const std::wstring & quoted);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFALookAheadUNode : public NFAUNode
{
  public:
    bool pos;
    NFAUNode * inner;
    NFALookAheadUNode(NFAUNode * internal, const bool positive);
    virtual void findAllNodes(std::map<NFAUNode*, bool> & soFar);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFALookBehindUNode : public NFAUNode
{
  public:
    bool pos;
    std::wstring mStr;
    NFALookBehindUNode(const std::wstring & str, const bool positive);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAStartOfLineUNode : public NFAUNode
{
  public:
    NFAStartOfLineUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAEndOfLineUNode : public NFAUNode
{
  public:
    NFAEndOfLineUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAReferenceUNode : public NFAUNode
{
  public:
    int gi;
    NFAReferenceUNode(const int groupIndex);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAStartOfInputUNode : public NFAUNode
{
  public:
    NFAStartOfInputUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    inline virtual bool isStartOfInputNode()  const { return false; }
};
class NFAEndOfInputUNode : public NFAUNode
{
  public:
    bool term;
    NFAEndOfInputUNode(const bool lookForTerm);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAWordBoundaryUNode : public NFAUNode
{
  public:
    bool pos;
    NFAWordBoundaryUNode(const bool positive);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAEndOfMatchUNode : public NFAUNode
{
  public:
    NFAEndOfMatchUNode();
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAGroupHeadUNode : public NFAUNode
{
  public:
    int gi;
    NFAGroupHeadUNode(const int groupIndex);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    inline virtual bool isGroupHeadNode()     const { return false; }
};
class NFAGroupTailUNode : public NFAUNode
{
  public:
    int gi;
    NFAGroupTailUNode(const int groupIndex);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAGroupLoopPrologueUNode : public NFAUNode
{
  public:
    int gi;
    NFAGroupLoopPrologueUNode(const int groupIndex);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};
class NFAGroupLoopUNode : public NFAUNode
{
  public:
    int gi, min, max, type;
    NFAUNode * inner;
    NFAGroupLoopUNode(NFAUNode * internal, const int minMatch,
                     const int maxMatch, const int groupIndex, const int matchType);
    virtual void findAllNodes(std::map<NFAUNode*, bool> & soFar);
    virtual int match(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    int matchGreedy(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    int matchLazy(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
    int matchPossessive(const std::wstring & str, WCMatcher * matcher, const int curInd = 0) const;
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -