regex.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: regex.h
  3. // Purpose: interface of wxRegEx
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @anchor wxRE_FLAGS
  9. Flags for regex compilation to be used with wxRegEx::Compile().
  10. */
  11. enum
  12. {
  13. /** Use extended regex syntax. */
  14. wxRE_EXTENDED = 0,
  15. /** Use advanced RE syntax (built-in regex only). */
  16. wxRE_ADVANCED = 1,
  17. /** Use basic RE syntax. */
  18. wxRE_BASIC = 2,
  19. /** Ignore case in match. */
  20. wxRE_ICASE = 4,
  21. /** Only check match, don't set back references. */
  22. wxRE_NOSUB = 8,
  23. /**
  24. If not set, treat '\n' as an ordinary character, otherwise it is
  25. special: it is not matched by '.' and '^' and '$' always match
  26. after/before it regardless of the setting of wxRE_NOT[BE]OL.
  27. */
  28. wxRE_NEWLINE = 16,
  29. /** Default flags.*/
  30. wxRE_DEFAULT = wxRE_EXTENDED
  31. };
  32. /**
  33. @anchor wxRE_NOT_FLAGS
  34. Flags for regex matching to be used with wxRegEx::Matches().
  35. These flags are mainly useful when doing several matches in a long string
  36. to prevent erroneous matches for '^' and '$':
  37. */
  38. enum
  39. {
  40. /** '^' doesn't match at the start of line. */
  41. wxRE_NOTBOL = 32,
  42. /** '$' doesn't match at the end of line. */
  43. wxRE_NOTEOL = 64
  44. };
  45. /**
  46. @class wxRegEx
  47. wxRegEx represents a regular expression. This class provides support
  48. for regular expressions matching and also replacement.
  49. It is built on top of either the system library (if it has support
  50. for POSIX regular expressions - which is the case of the most modern
  51. Unices) or uses the built in Henry Spencer's library. Henry Spencer
  52. would appreciate being given credit in the documentation of software
  53. which uses his library, but that is not a requirement.
  54. Regular expressions, as defined by POSIX, come in two flavours: @e extended
  55. and @e basic. The builtin library also adds a third flavour
  56. of expression @ref overview_resyntax "advanced", which is not available
  57. when using the system library.
  58. Unicode is fully supported only when using the builtin library.
  59. When using the system library in Unicode mode, the expressions and data
  60. are translated to the default 8-bit encoding before being passed to
  61. the library.
  62. On platforms where a system library is available, the default is to use
  63. the builtin library for Unicode builds, and the system library otherwise.
  64. It is possible to use the other if preferred by selecting it when building
  65. the wxWidgets.
  66. @library{wxbase}
  67. @category{data}
  68. Examples:
  69. A bad example of processing some text containing email addresses (the example
  70. is bad because the real email addresses can have more complicated form than
  71. @c user@host.net):
  72. @code
  73. wxString text;
  74. ...
  75. wxRegEx reEmail = "([^@]+)@([[:alnum:].-_].)+([[:alnum:]]+)";
  76. if ( reEmail.Matches(text) )
  77. {
  78. wxString text = reEmail.GetMatch(email);
  79. wxString username = reEmail.GetMatch(email, 1);
  80. if ( reEmail.GetMatch(email, 3) == "com" ) // .com TLD?
  81. {
  82. ...
  83. }
  84. }
  85. // or we could do this to hide the email address
  86. size_t count = reEmail.ReplaceAll(text, "HIDDEN@\\2\\3");
  87. printf("text now contains %u hidden addresses", count);
  88. @endcode
  89. */
  90. class wxRegEx
  91. {
  92. public:
  93. /**
  94. Default constructor: use Compile() later.
  95. */
  96. wxRegEx();
  97. /**
  98. Create and compile the regular expression, use
  99. IsValid() to test for compilation errors.
  100. As for the flags, please see @ref wxRE_FLAGS.
  101. */
  102. wxRegEx(const wxString& expr, int flags = wxRE_DEFAULT);
  103. /**
  104. Destructor. It's not virtual, don't derive from this class.
  105. */
  106. ~wxRegEx();
  107. /**
  108. Compile the string into regular expression, return @true if ok or @false
  109. if string has a syntax error.
  110. As for the flags, please see @ref wxRE_FLAGS.
  111. */
  112. bool Compile(const wxString& pattern, int flags = wxRE_DEFAULT);
  113. /**
  114. Get the start index and the length of the match of the expression
  115. (if @a index is 0) or a bracketed subexpression (@a index different from 0).
  116. May only be called after successful call to Matches() and only if @c wxRE_NOSUB
  117. was @b not used in Compile().
  118. Returns @false if no match or if an error occurred.
  119. */
  120. bool GetMatch(size_t* start, size_t* len, size_t index = 0) const;
  121. /**
  122. Returns the part of string corresponding to the match where index is interpreted
  123. as above. Empty string is returned if match failed.
  124. May only be called after successful call to Matches() and only if @c wxRE_NOSUB
  125. was @b not used in Compile().
  126. */
  127. wxString GetMatch(const wxString& text, size_t index = 0) const;
  128. /**
  129. Returns the size of the array of matches, i.e.\ the number of bracketed
  130. subexpressions plus one for the expression itself, or 0 on error.
  131. May only be called after successful call to Compile().
  132. and only if @c wxRE_NOSUB was @b not used.
  133. */
  134. size_t GetMatchCount() const;
  135. /**
  136. Return @true if this is a valid compiled regular expression, @false
  137. otherwise.
  138. */
  139. bool IsValid() const;
  140. //@{
  141. /**
  142. Matches the precompiled regular expression against the string @a text,
  143. returns @true if matches and @false otherwise.
  144. @e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see
  145. @ref wxRE_NOT_FLAGS.
  146. Some regex libraries assume that the text given is null terminated, while
  147. others require the length be given as a separate parameter. Therefore for
  148. maximum portability assume that @a text cannot contain embedded nulls.
  149. When the <b>Matches(const wxChar *text, int flags = 0)</b> form is used,
  150. a wxStrlen() will be done internally if the regex library requires the
  151. length. When using Matches() in a loop the <b>Matches(text, flags, len)</b>
  152. form can be used instead, making it possible to avoid a wxStrlen() inside
  153. the loop.
  154. May only be called after successful call to Compile().
  155. */
  156. bool Matches(const wxChar* text, int flags = 0) const;
  157. bool Matches(const wxChar* text, int flags, size_t len) const;
  158. //@}
  159. /**
  160. Matches the precompiled regular expression against the string @a text,
  161. returns @true if matches and @false otherwise.
  162. @e Flags may be combination of @c wxRE_NOTBOL and @c wxRE_NOTEOL, see
  163. @ref wxRE_NOT_FLAGS.
  164. May only be called after successful call to Compile().
  165. */
  166. bool Matches(const wxString& text, int flags = 0) const;
  167. /**
  168. Replaces the current regular expression in the string pointed to by
  169. @a text, with the text in @a replacement and return number of matches
  170. replaced (maybe 0 if none found) or -1 on error.
  171. The replacement text may contain back references @c \\number which will be
  172. replaced with the value of the corresponding subexpression in the
  173. pattern match. @c \\0 corresponds to the entire match and @c \& is a
  174. synonym for it. Backslash may be used to quote itself or @c \& character.
  175. @a maxMatches may be used to limit the number of replacements made, setting
  176. it to 1, for example, will only replace first occurrence (if any) of the
  177. pattern in the text while default value of 0 means replace all.
  178. */
  179. int Replace(wxString* text, const wxString& replacement,
  180. size_t maxMatches = 0) const;
  181. /**
  182. Replace all occurrences: this is actually a synonym for
  183. Replace().
  184. @see ReplaceFirst()
  185. */
  186. int ReplaceAll(wxString* text, const wxString& replacement) const;
  187. /**
  188. Replace the first occurrence.
  189. */
  190. int ReplaceFirst(wxString* text, const wxString& replacement) const;
  191. };