📄 regexp.hpp
字号:
/// @param match_flags /// Flags to match. /// @return /// Return TRUE if a string corresponding to the match to pattern or /// subpattern. /// @sa /// CRegexp, CRegexp::GetMatch() bool Exists( const string& pattern, CRegexp::TCompile compile_flags = CRegexp::eCompile_default, CRegexp::TMatch match_flags = CRegexp::eMatch_default ); /// Get matching pattern/subpattern from string. /// /// @param pattern /// Perl regular expression to search. /// @param compile_flags /// Regular expression compilation flags. /// @param match_flags /// Flags to match. /// @param pattern_idx /// Index of pattern/subpattern to extract. /// Use pattern_idx = 0 for pattern, pattern_idx > 0 for sub patterns. /// @return /// Return the substring at location of pattern/subpatter match with /// index pattern_idx. Return empty string when no match. /// @sa /// CRegexp, CRegexp::GetMatch() string Extract( const string& pattern, CRegexp::TCompile compile_flags = CRegexp::eCompile_default, CRegexp::TMatch match_flags = CRegexp::eMatch_default, size_t pattern_idx = 0 ); /// Replace occurrences of a substring within a string by pattern. /// /// @param search /// Reqular expression to match a substring value that is replaced. /// @param replace /// Replace "search" substring with this value. The matched subpatterns /// (if any) can be found and inserted into replace string using /// variables $1, $2, $3, and so forth. The variable can be enclosed /// in the curly brackets {}, that will be deleted on substitution. /// @param compile_flags /// Regular expression compilation flags. /// @param match_flags /// Flags to match. /// @param max_replace /// Replace no more than "max_replace" occurrences of substring "search". /// If "max_replace" is zero (default), then replace all occurrences with /// "replace". /// @return /// Return the count of replacements. /// @sa /// CRegexp, ReplaceRange() size_t Replace( const string& search, const string& replace, CRegexp::TCompile compile_flags = CRegexp::eCompile_default, CRegexp::TMatch match_flags = CRegexp::eMatch_default, size_t max_replace = 0 ); // // Range functions. // /// Range processing type. /// Defines which part of the specified range should be processed. enum ERange { eInside, ///< Process substrings inside range. eOutside ///< Process substrings outside range. }; /// Set new range for range-dependent functions. /// /// The mached string will be splitted up by "delimeter". /// And then in range-dependent functions every part (substring) is checked /// to fall into the range, specified by start and end adresses. /// /// The addresses works similare the Unix utility SED, except that regular /// expressions is Perl-compatible: /// - empty address in the range correspond to any substring. /// - command with one address correspond to any substring that matches /// the address. /// - command with two addresses correspond to inclusive range from the /// start address to through the next pattern space that maches the /// end address. /// /// Specified range have effect only for range-dependent functions. /// Otherwise range is ignored. /// @param addr_start /// Regular expression which assign a starting address of range. /// @param addr_end /// Regular expression which assign an ending address of range. /// Should be empty if the start address is empty. /// @param delimiter /// Split a source string by "delimiter. /// @sa /// ClearRange, ReplaceRange() void SetRange( const string& addr_start = kEmptyStr, const string& addr_end = kEmptyStr, const string& delimiter = "\n" ); /// Clear range for range-dependent functions. /// /// Have the same effect as SetRange() without parameters. /// @sa /// SetRange() void ClearRange(void); /// Replace all occurrences of a substring within a string by pattern. /// /// Use range specified by SetRange() method. Work like SED command s/. /// @param search /// Reqular expression to match a substring value that is replaced. /// @param replace /// Replace "search" substring with this value. The matched subpatterns /// (if any) can be found and inserted into replace string using /// variables $1, $2, $3, and so forth. The variable can be enclosed /// in the curly brackets {}, that will be deleted on substitution. /// @param compile_flags /// Regular expression compilation flags. /// @param match_flags /// Flags to match. /// @param process_within /// Define which part of the range should be processed. /// @param max_replace /// Replace no more than "max_replace" occurrences of substring "search" /// in the every substring. If "max_replace" is zero (default), /// then replace all occurrences with "replace". /// @return /// Return the count of replacements. /// @sa /// ERange, SetRange(), ClearRange() size_t ReplaceRange( const string& search, const string& replace, CRegexp::TCompile compile_flags = CRegexp::eCompile_default, CRegexp::TMatch match_flags = CRegexp::eMatch_default, CRegexpUtil::ERange process_within = eInside, size_t max_replace = 0 );private: /// Divide source string to substrings by delimiter. /// If delimiter is empty string that use early defined delimiter. void x_Divide(const string& delimiter = kEmptyStr); /// Join substrings back to entire string. void x_Join(void);private: string m_Content; ///< Content string. list<string> m_ContentList; ///< Content list. bool m_IsDivided; ///< TRUE if m_ContentList is newer than ///< m_Content, and FALSE otherwise. string m_RangeStart; ///< Regexp to determine start of range. string m_RangeEnd; ///< Regexp to determine end of range. string m_Delimiter; ///< Delimiter used to split string.};////////////////////////////////////////////////////////////////////////////////// Inline////// CRegexp//inlineint CRegexp::NumFound() const{ return m_NumFound;}inlineconst int* CRegexp::GetResults(size_t idx) const{ if ((int)idx >= m_NumFound) { throw runtime_error("idx >= NumFound()"); } return m_Results + 2 * idx;}//// CRegexpUtil//inlinestring CRegexpUtil::GetResult(void){ if ( m_IsDivided ) { x_Join(); } return m_Content;}inlinevoid CRegexpUtil::Reset(const string& str){ m_Content = str; m_IsDivided = false; m_ContentList.clear();}inlineCRegexpUtil::operator string(void){ return GetResult();}inlinevoid CRegexpUtil::operator= (const string& str){ Reset(str);}inlinevoid CRegexpUtil::ClearRange(void){ SetRange();}inlinebool CRegexpUtil::Exists( const string& pattern, CRegexp::TCompile compile_flags, CRegexp::TMatch match_flags){ // Fill shure that string is not divided x_Join(); // Check the pattern existence CRegexp re(pattern, compile_flags); re.GetMatch(m_Content.c_str(), 0, match_flags, 0, true); return re.NumFound() > 0;}inlinestring CRegexpUtil::Extract( const string& pattern, CRegexp::TCompile compile_flags, CRegexp::TMatch match_flags, size_t pattern_idx){ // Fill shure that string is not divided x_Join(); // Get the pattern/subpattern CRegexp re(pattern, compile_flags); return re.GetMatch(m_Content.c_str(), 0, pattern_idx, match_flags);}END_NCBI_SCOPE/* * =========================================================================== * $Log: regexp.hpp,v $ * Revision 1000.2 2004/06/01 19:38:40 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.10 * * Revision 1.10 2004/04/05 16:54:26 ucko * Include the internal pcre.h when using MSVC until its build system * catches up. * * Revision 1.9 2004/04/05 15:54:12 ucko * Default to using external versions of zlib, bzlib, and libpcre if available. * * Revision 1.8 2004/03/12 13:57:13 dicuccio * Renamed NCBI_REGEXP_EXPORT -> NXBI_XREGEXP_EXPORT to match library name * * Revision 1.7 2004/03/11 22:54:48 gorelenk * Changed export prefix of all classes to NCBI_REGEXP_EXPORT. * * Revision 1.6 2003/11/07 13:38:51 ivanov * Comments changes * * Revision 1.5 2003/11/06 16:12:10 ivanov * Added CRegexpUtil class. * Added some new CRegExp::ECompile flags. * Added more comments; Some formal code rearrangement. * * Revision 1.4 2003/07/16 19:15:05 clausen * Added TCompile and TMatch and fixed comments * * Revision 1.3 2003/07/07 13:50:59 kuznets * Added DLL export/import instruction * * Revision 1.2 2003/06/20 18:32:42 clausen * Changed to native interface for regexp * * Revision 1.1 2003/06/03 14:47:46 clausen * Initial version * * =========================================================================== */#endif /* UTIL___REGEXP__HPP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -