old_regex.qbk 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. [/
  2. Copyright 2006-2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:old_regex High Level Class RegEx (Deprecated)]
  8. The high level wrapper class RegEx is now deprecated and does not form
  9. part of the regular expression standardization proposal. This type still
  10. exists, and existing code will continue to compile, however the following
  11. documentation is unlikely to be further updated.
  12. #include <boost/cregex.hpp>
  13. The class RegEx provides a high level simplified interface to the regular
  14. expression library, this class only handles narrow character strings, and
  15. regular expressions always follow the "normal" syntax - that is the
  16. same as the perl / ECMAScript syntax.
  17. typedef bool (*GrepCallback)(const RegEx& expression);
  18. typedef bool (*GrepFileCallback)(const char* file, const RegEx& expression);
  19. typedef bool (*FindFilesCallback)(const char* file);
  20. class RegEx
  21. {
  22. public:
  23. RegEx();
  24. RegEx(const RegEx& o);
  25. ~RegEx();
  26. RegEx(const char* c, bool icase = false);
  27. explicit RegEx(const std::string& s, bool icase = false);
  28. RegEx& operator=(const RegEx& o);
  29. RegEx& operator=(const char* p);
  30. RegEx& operator=(const std::string& s);
  31. unsigned int SetExpression(const char* p, bool icase = false);
  32. unsigned int SetExpression(const std::string& s, bool icase = false);
  33. std::string Expression()const;
  34. //
  35. // now matching operators:
  36. //
  37. bool Match(const char* p, boost::match_flag_type flags = match_default);
  38. bool Match(const std::string& s, boost::match_flag_type flags = match_default);
  39. bool Search(const char* p, boost::match_flag_type flags = match_default);
  40. bool Search(const std::string& s, boost::match_flag_type flags = match_default);
  41. unsigned int Grep(GrepCallback cb, const char* p,
  42. boost::match_flag_type flags = match_default);
  43. unsigned int Grep(GrepCallback cb, const std::string& s,
  44. boost::match_flag_type flags = match_default);
  45. unsigned int Grep(std::vector<std::string>& v, const char* p,
  46. boost::match_flag_type flags = match_default);
  47. unsigned int Grep(std::vector<std::string>& v, const std::string& s,
  48. boost::match_flag_type flags = match_default);
  49. unsigned int Grep(std::vector<unsigned int>& v, const char* p,
  50. boost::match_flag_type flags = match_default);
  51. unsigned int Grep(std::vector<unsigned int>& v, const std::string& s,
  52. boost::match_flag_type flags = match_default);
  53. unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false,
  54. boost::match_flag_type flags = match_default);
  55. unsigned int GrepFiles(GrepFileCallback cb, const std::string& files,
  56. bool recurse = false,
  57. boost::match_flag_type flags = match_default);
  58. unsigned int FindFiles(FindFilesCallback cb, const char* files,
  59. bool recurse = false,
  60. boost::match_flag_type flags = match_default);
  61. unsigned int FindFiles(FindFilesCallback cb, const std::string& files,
  62. bool recurse = false,
  63. boost::match_flag_type flags = match_default);
  64. std::string Merge(const std::string& in, const std::string& fmt,
  65. bool copy = true, boost::match_flag_type flags = match_default);
  66. std::string Merge(const char* in, const char* fmt, bool copy = true,
  67. boost::match_flag_type flags = match_default);
  68. unsigned Split(std::vector<std::string>& v, std::string& s,
  69. boost::match_flag_type flags = match_default,
  70. unsigned max_count = ~0);
  71. //
  72. // now operators for returning what matched in more detail:
  73. //
  74. unsigned int Position(int i = 0)const;
  75. unsigned int Length(int i = 0)const;
  76. bool Matched(int i = 0)const;
  77. unsigned int Line()const;
  78. unsigned int Marks() const;
  79. std::string What(int i)const;
  80. std::string operator[](int i)const ;
  81. static const unsigned int npos;
  82. };
  83. Member functions for class RegEx are defined as follows:
  84. [table
  85. [[Member][Description]]
  86. [[`RegEx();`][Default constructor, constructs an instance of RegEx without any valid expression. ]]
  87. [[`RegEx(const RegEx& o);`][Copy constructor, all the properties of parameter /o/
  88. are copied. ]]
  89. [[`RegEx(const char* c, bool icase = false);`][Constructs an instance of RegEx,
  90. setting the expression to /c/, if /icase/ is true then matching is
  91. insensitive to case, otherwise it is sensitive to case. Throws
  92. [bad_expression] on failure. ]]
  93. [[`RegEx(const std::string& s, bool icase = false);`][Constructs an instance of
  94. RegEx, setting the expression to /s/, if /icase/ is true then matching
  95. is insensitive to case, otherwise it is sensitive to case. Throws
  96. [bad_expression] on failure. ]]
  97. [[`RegEx& operator=(const RegEx& o);`][Default assignment operator. ]]
  98. [[`RegEx& operator=(const char* p);`][Assignment operator, equivalent to calling
  99. `SetExpression(p, false)`. Throws [bad_expression] on failure. ]]
  100. [[`RegEx& operator=(const std::string& s);`][Assignment operator, equivalent to
  101. calling `SetExpression(s, false)`. Throws [bad_expression] on failure. ]]
  102. [[`unsigned int SetExpression(const char* p, bool icase = false);`][Sets the
  103. current expression to /p/, if /icase/ is true then matching is
  104. insensitive to case, otherwise it is sensitive to case.
  105. Throws [bad_expression] on failure. ]]
  106. [[`unsigned int SetExpression(const std::string& s, bool icase = false);`]
  107. [Sets the current expression to /s/, if /icase/ is true then matching is
  108. insensitive to case, otherwise it is sensitive to case. Throws
  109. [bad_expression] on failure. ]]
  110. [[`std::string Expression()const;`][Returns a copy of the current regular expression. ]]
  111. [[`bool Match(const char* p, boost::match_flag_type flags = match_default);`]
  112. [Attempts to match the current expression against the text /p/ using
  113. the match flags /flags/ - see [match_flag_type]. Returns /true/ if the
  114. expression matches the whole of the input string. ]]
  115. [[`bool Match(const std::string& s, boost::match_flag_type flags = match_default);`]
  116. [Attempts to match the current expression against the text /s/ using
  117. the [match_flag_type] /flags/. Returns /true/ if the expression matches
  118. the whole of the input string. ]]
  119. [[`bool Search(const char* p, boost::match_flag_type flags = match_default);`]
  120. [Attempts to find a match for the current expression somewhere in
  121. the text /p/ using the [match_flag_type] /flags/. Returns /true/
  122. if the match succeeds. ]]
  123. [[`bool Search(const std::string& s, boost::match_flag_type flags = match_default);`]
  124. [Attempts to find a match for the current expression somewhere in the
  125. text /s/ using the [match_flag_type] flags. Returns /true/ if the
  126. match succeeds. ]]
  127. [[`unsigned int Grep(GrepCallback cb, const char* p, boost::match_flag_type flags = match_default);`]
  128. [Finds all matches of the current expression in the text /p/ using the
  129. [match_flag_type] /flags/. For each match found calls the call-back
  130. function cb as: `cb(*this);`
  131. If at any stage the call-back function returns /false/ then the grep
  132. operation terminates, otherwise continues until no further matches
  133. are found. Returns the number of matches found.]]
  134. [[`unsigned int Grep(GrepCallback cb, const std::string& s, boost::match_flag_type flags = match_default);`]
  135. [Finds all matches of the current expression in the text /s/ using the
  136. [match_flag_type] flags. For each match found calls the call-back
  137. function cb as: `cb(*this);`
  138. If at any stage the call-back function returns false then the grep operation
  139. terminates, otherwise continues until no further matches are found.
  140. Returns the number of matches found.]]
  141. [[`unsigned int Grep(std::vector<std::string>& v, const char* p, boost::match_flag_type flags = match_default);`]
  142. [Finds all matches of the current expression in the text /p/ using the
  143. [match_flag_type] flags. For each match pushes a copy of what matched
  144. onto /v/. Returns the number of matches found. ]]
  145. [[`unsigned int Grep(std::vector<std::string>& v, const std::string& s, boost::match_flag_type flags = match_default);`]
  146. [Finds all matches of the current expression in the text /s/ using the
  147. [match_flag_type] /flags/. For each match pushes a copy of what
  148. matched onto /v/. Returns the number of matches found. ]]
  149. [[`unsigned int Grep(std::vector<unsigned int>& v, const char* p, boost::match_flag_type flags = match_default);`]
  150. [Finds all matches of the current expression in the text /p/ using the
  151. [match_flag_type] /flags/. For each match pushes the starting index of
  152. what matched onto /v/. Returns the number of matches found. ]]
  153. [[`unsigned int Grep(std::vector<unsigned int>& v, const std::string& s, boost::match_flag_type flags = match_default);`]
  154. [Finds all matches of the current expression in the text /s/ using the
  155. [match_flag_type] /flags/. For each match pushes the starting index of what
  156. matched onto /v/. Returns the number of matches found. ]]
  157. [[`unsigned int GrepFiles(GrepFileCallback cb, const char* files, bool recurse = false, boost::match_flag_type flags = match_default);`]
  158. [Finds all matches of the current expression in the files /files/ using
  159. the [match_flag_type] /flags/. For each match calls the call-back function cb.
  160. If the call-back returns false then the algorithm returns without
  161. considering further matches in the current file, or any further files.
  162. The parameter /files/ can include wild card characters '\*' and '\?', if
  163. the parameter recurse is true then searches sub-directories for matching
  164. file names.
  165. Returns the total number of matches found.
  166. May throw an exception derived from `std::runtime_error` if file io fails.]]
  167. [[`unsigned int GrepFiles(GrepFileCallback cb, const std::string& files, bool recurse = false, boost::match_flag_type flags = match_default);`]
  168. [Finds all matches of the current expression in the files /files/ using the
  169. [match_flag_type] /flags/. For each match calls the call-back function cb.
  170. If the call-back returns false then the algorithm returns without
  171. considering further matches in the current file, or any further files.
  172. The parameter /files/ can include wild card characters '\*' and '\?', if
  173. the parameter recurse is true then searches sub-directories for
  174. matching file names.
  175. Returns the total number of matches found.
  176. May throw an exception derived from `std::runtime_error` if file io fails.]]
  177. [[`unsigned int FindFiles(FindFilesCallback cb, const char* files, bool recurse = false, boost::match_flag_type flags = match_default);`]
  178. [Searches files to find all those which contain at least one match of
  179. the current expression using the [match_flag_type] /flags/. For each
  180. matching file calls the call-back function cb.
  181. If the call-back returns false then the algorithm returns without
  182. considering any further files.
  183. The parameter /files/ can include wild card characters '\*' and '\?', if
  184. the parameter /recurse/ is true then searches sub-directories for
  185. matching file names.
  186. Returns the total number of files found.
  187. May throw an exception derived from `std::runtime_error` if file io fails.]]
  188. [[`unsigned int FindFiles(FindFilesCallback cb, const std::string& files, bool recurse = false, boost::match_flag_type flags = match_default);`]
  189. [Searches files to find all those which contain at least one
  190. match of the current expression using the [match_flag_type] /flags/.
  191. For each matching file calls the call-back function cb.
  192. If the call-back returns false then the algorithm returns without
  193. considering any further files.
  194. The parameter /files/ can include wild card characters '\*' and '\?', if
  195. the parameter /recurse/ is true then searches sub-directories for
  196. matching file names.
  197. Returns the total number of files found.
  198. May throw an exception derived from `std::runtime_error` if file io fails.]]
  199. [[`std::string Merge(const std::string& in, const std::string& fmt, bool copy = true, boost::match_flag_type flags = match_default);`]
  200. [Performs a search and replace operation: searches through the
  201. string /in/ for all occurrences of the current expression, for each
  202. occurrence replaces the match with the format string /fmt/. Uses /flags/
  203. to determine what gets matched, and how the format string should be
  204. treated. If /copy/ is true then all unmatched sections of input are
  205. copied unchanged to output, if the flag /format_first_only/ is set then
  206. only the first occurrence of the pattern found is replaced.
  207. Returns the new string. See also
  208. [link boost_regex.format format string syntax], and [match_flag_type].]]
  209. [[`std::string Merge(const char* in, const char* fmt, bool copy = true, boost::match_flag_type flags = match_default);`]
  210. [Performs a search and replace operation: searches through the string /in/
  211. for all occurrences of the current expression, for each occurrence
  212. replaces the match with the format string /fmt/. Uses /flags/ to determine
  213. what gets matched, and how the format string should be treated.
  214. If /copy/ is true then all unmatched sections of input are copied
  215. unchanged to output, if the flag /format_first_only/ is set then only
  216. the first occurrence of the pattern found is replaced. Returns
  217. the new string. See also [link boost_regex.format format string syntax], and [match_flag_type].]]
  218. [[`unsigned Split(std::vector<std::string>& v, std::string& s, boost::match_flag_type flags = match_default, unsigned max_count = ~0);`]
  219. [Splits the input string and pushes each one onto the vector.
  220. If the expression contains no marked sub-expressions, then one
  221. string is outputted for each section of the input that does not
  222. match the expression. If the expression does contain marked
  223. sub-expressions, then outputs one string for each marked
  224. sub-expression each time a match occurs. Outputs no more than
  225. /max_count/ strings. Before returning, deletes from the input string
  226. /s/ all of the input that has been processed (all of the string if
  227. /max_count/ was not reached). Returns the number of strings pushed
  228. onto the vector. ]]
  229. [[`unsigned int Position(int i = 0)const;`]
  230. [Returns the position of what matched sub-expression /i/. If `i = 0`
  231. then returns the position of the whole match. Returns `RegEx::npos` if
  232. the supplied index is invalid, or if the specified sub-expression
  233. did not participate in the match. ]]
  234. [[`unsigned int Length(int i = 0)const;`]
  235. [Returns the length of what matched sub-expression i. If `i = 0` then
  236. returns the length of the whole match. Returns `RegEx::npos` if the
  237. supplied index is invalid, or if the specified sub-expression did not
  238. participate in the match. ]]
  239. [[`bool Matched(int i = 0)const;`]
  240. [Returns true if sub-expression /i/ was matched, false otherwise. ]]
  241. [[`unsigned int Line()const;`][Returns the line on which the match occurred,
  242. indexes start from 1 not zero, if no match occurred then returns `RegEx::npos`. ]]
  243. [[`unsigned int Marks() const;`][Returns the number of marked sub-expressions
  244. contained in the expression. Note that this includes the whole
  245. match (sub-expression zero), so the value returned is always >= 1. ]]
  246. [[`std::string What(int i)const;`]
  247. [Returns a copy of what matched sub-expression /i/. If `i = 0` then
  248. returns a copy of the whole match. Returns a null string if the
  249. index is invalid or if the specified sub-expression did not
  250. participate in a match. ]]
  251. [[`std::string operator[](int i)const ;`][Returns `what(i);`
  252. Can be used to simplify access to sub-expression matches, and make
  253. usage more perl-like.]]
  254. ]
  255. [endsect]