regex.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/regex.h
  3. // Purpose: regular expression matching
  4. // Author: Karsten Ballueder
  5. // Modified by: VZ at 13.07.01 (integrated to wxWin)
  6. // Created: 05.02.2000
  7. // Copyright: (c) 2000 Karsten Ballueder <ballueder@gmx.net>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_REGEX_H_
  11. #define _WX_REGEX_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_REGEX
  14. #include "wx/string.h"
  15. // ----------------------------------------------------------------------------
  16. // constants
  17. // ----------------------------------------------------------------------------
  18. // flags for regex compilation: these can be used with Compile()
  19. enum
  20. {
  21. // use extended regex syntax
  22. wxRE_EXTENDED = 0,
  23. // use advanced RE syntax (built-in regex only)
  24. #ifdef wxHAS_REGEX_ADVANCED
  25. wxRE_ADVANCED = 1,
  26. #endif
  27. // use basic RE syntax
  28. wxRE_BASIC = 2,
  29. // ignore case in match
  30. wxRE_ICASE = 4,
  31. // only check match, don't set back references
  32. wxRE_NOSUB = 8,
  33. // if not set, treat '\n' as an ordinary character, otherwise it is
  34. // special: it is not matched by '.' and '^' and '$' always match
  35. // after/before it regardless of the setting of wxRE_NOT[BE]OL
  36. wxRE_NEWLINE = 16,
  37. // default flags
  38. wxRE_DEFAULT = wxRE_EXTENDED
  39. };
  40. // flags for regex matching: these can be used with Matches()
  41. //
  42. // these flags are mainly useful when doing several matches in a long string,
  43. // they can be used to prevent erroneous matches for '^' and '$'
  44. enum
  45. {
  46. // '^' doesn't match at the start of line
  47. wxRE_NOTBOL = 32,
  48. // '$' doesn't match at the end of line
  49. wxRE_NOTEOL = 64
  50. };
  51. // ----------------------------------------------------------------------------
  52. // wxRegEx: a regular expression
  53. // ----------------------------------------------------------------------------
  54. class WXDLLIMPEXP_FWD_BASE wxRegExImpl;
  55. class WXDLLIMPEXP_BASE wxRegEx
  56. {
  57. public:
  58. // default ctor: use Compile() later
  59. wxRegEx() { Init(); }
  60. // create and compile
  61. wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT)
  62. {
  63. Init();
  64. (void)Compile(expr, flags);
  65. }
  66. // return true if this is a valid compiled regular expression
  67. bool IsValid() const { return m_impl != NULL; }
  68. // compile the string into regular expression, return true if ok or false
  69. // if string has a syntax error
  70. bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT);
  71. // matches the precompiled regular expression against a string, return
  72. // true if matches and false otherwise
  73. //
  74. // flags may be combination of wxRE_NOTBOL and wxRE_NOTEOL
  75. // len may be the length of text (ignored by most system regex libs)
  76. //
  77. // may only be called after successful call to Compile()
  78. bool Matches(const wxString& text, int flags = 0) const;
  79. bool Matches(const wxChar *text, int flags, size_t len) const
  80. { return Matches(wxString(text, len), flags); }
  81. // get the start index and the length of the match of the expression
  82. // (index 0) or a bracketed subexpression (index != 0)
  83. //
  84. // may only be called after successful call to Matches()
  85. //
  86. // return false if no match or on error
  87. bool GetMatch(size_t *start, size_t *len, size_t index = 0) const;
  88. // return the part of string corresponding to the match, empty string is
  89. // returned if match failed
  90. //
  91. // may only be called after successful call to Matches()
  92. wxString GetMatch(const wxString& text, size_t index = 0) const;
  93. // return the size of the array of matches, i.e. the number of bracketed
  94. // subexpressions plus one for the expression itself, or 0 on error.
  95. //
  96. // may only be called after successful call to Compile()
  97. size_t GetMatchCount() const;
  98. // replaces the current regular expression in the string pointed to by
  99. // pattern, with the text in replacement and return number of matches
  100. // replaced (maybe 0 if none found) or -1 on error
  101. //
  102. // the replacement text may contain backreferences (\number) which will be
  103. // replaced with the value of the corresponding subexpression in the
  104. // pattern match
  105. //
  106. // maxMatches may be used to limit the number of replacements made, setting
  107. // it to 1, for example, will only replace first occurrence (if any) of the
  108. // pattern in the text while default value of 0 means replace all
  109. int Replace(wxString *text, const wxString& replacement,
  110. size_t maxMatches = 0) const;
  111. // replace the first occurrence
  112. int ReplaceFirst(wxString *text, const wxString& replacement) const
  113. { return Replace(text, replacement, 1); }
  114. // replace all occurrences: this is actually a synonym for Replace()
  115. int ReplaceAll(wxString *text, const wxString& replacement) const
  116. { return Replace(text, replacement, 0); }
  117. // dtor not virtual, don't derive from this class
  118. ~wxRegEx();
  119. private:
  120. // common part of all ctors
  121. void Init();
  122. // the real guts of this class
  123. wxRegExImpl *m_impl;
  124. // as long as the class wxRegExImpl is not ref-counted,
  125. // instances of the handle wxRegEx must not be copied.
  126. wxRegEx(const wxRegEx&);
  127. wxRegEx &operator=(const wxRegEx&);
  128. };
  129. #endif // wxUSE_REGEX
  130. #endif // _WX_REGEX_H_