1 /* 2 ********************************************************************** 3 * Copyright (C) 2002-2015, International Business Machines 4 * Corporation and others. All Rights Reserved. 5 ********************************************************************** 6 * file name: regex.h 7 * encoding: US-ASCII 8 * indentation:4 9 * 10 * created on: 2002oct22 11 * created by: Andy Heninger 12 * 13 * ICU Regular Expressions, API for C++ 14 */ 15 16 #ifndef REGEX_H 17 #define REGEX_H 18 19 //#define REGEX_DEBUG 20 21 /** 22 * \file 23 * \brief C++ API: Regular Expressions 24 * 25 * <h2>Regular Expression API</h2> 26 * 27 * <p>The ICU API for processing regular expressions consists of two classes, 28 * <code>RegexPattern</code> and <code>RegexMatcher</code>. 29 * <code>RegexPattern</code> objects represent a pre-processed, or compiled 30 * regular expression. They are created from a regular expression pattern string, 31 * and can be used to create <code>RegexMatcher</code> objects for the pattern.</p> 32 * 33 * <p>Class <code>RegexMatcher</code> bundles together a regular expression 34 * pattern and a target string to which the search pattern will be applied. 35 * <code>RegexMatcher</code> includes API for doing plain find or search 36 * operations, for search and replace operations, and for obtaining detailed 37 * information about bounds of a match. </p> 38 * 39 * <p>Note that by constructing <code>RegexMatcher</code> objects directly from regular 40 * expression pattern strings application code can be simplified and the explicit 41 * need for <code>RegexPattern</code> objects can usually be eliminated. 42 * </p> 43 */ 44 45 #include "unicode/utypes.h" 46 47 #if !UCONFIG_NO_REGULAR_EXPRESSIONS 48 49 #include "unicode/uobject.h" 50 #include "unicode/unistr.h" 51 #include "unicode/utext.h" 52 #include "unicode/parseerr.h" 53 54 #include "unicode/uregex.h" 55 56 // Forward Declarations 57 58 struct UHashtable; 59 60 U_NAMESPACE_BEGIN 61 62 struct Regex8BitSet; 63 class RegexCImpl; 64 class RegexMatcher; 65 class RegexPattern; 66 struct REStackFrame; 67 class RuleBasedBreakIterator; 68 class UnicodeSet; 69 class UVector; 70 class UVector32; 71 class UVector64; 72 73 74 /** 75 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes 76 * factory methods for creating a RegexPattern object from the source (string) form 77 * of a regular expression, methods for creating RegexMatchers that allow the pattern 78 * to be applied to input text, and a few convenience methods for simple common 79 * uses of regular expressions. 80 * 81 * <p>Class RegexPattern is not intended to be subclassed.</p> 82 * 83 * @stable ICU 2.4 84 */ 85 class U_I18N_API RegexPattern U_FINAL : public UObject { 86 public: 87 88 /** 89 * default constructor. Create a RegexPattern object that refers to no actual 90 * pattern. Not normally needed; RegexPattern objects are usually 91 * created using the factory method <code>compile()</code>. 92 * 93 * @stable ICU 2.4 94 */ 95 RegexPattern(); 96 97 /** 98 * Copy Constructor. Create a new RegexPattern object that is equivalent 99 * to the source object. 100 * @param source the pattern object to be copied. 101 * @stable ICU 2.4 102 */ 103 RegexPattern(const RegexPattern &source); 104 105 /** 106 * Destructor. Note that a RegexPattern object must persist so long as any 107 * RegexMatcher objects that were created from the RegexPattern are active. 108 * @stable ICU 2.4 109 */ 110 virtual ~RegexPattern(); 111 112 /** 113 * Comparison operator. Two RegexPattern objects are considered equal if they 114 * were constructed from identical source patterns using the same match flag 115 * settings. 116 * @param that a RegexPattern object to compare with "this". 117 * @return TRUE if the objects are equivalent. 118 * @stable ICU 2.4 119 */ 120 UBool operator==(const RegexPattern& that) const; 121 122 /** 123 * Comparison operator. Two RegexPattern objects are considered equal if they 124 * were constructed from identical source patterns using the same match flag 125 * settings. 126 * @param that a RegexPattern object to compare with "this". 127 * @return TRUE if the objects are different. 128 * @stable ICU 2.4 129 */ 130 inline UBool operator!=(const RegexPattern& that) const {return ! operator ==(that);} 131 132 /** 133 * Assignment operator. After assignment, this RegexPattern will behave identically 134 * to the source object. 135 * @stable ICU 2.4 136 */ 137 RegexPattern &operator =(const RegexPattern &source); 138 139 /** 140 * Create an exact copy of this RegexPattern object. Since RegexPattern is not 141 * intended to be subclassed, <code>clone()</code> and the copy construction are 142 * equivalent operations. 143 * @return the copy of this RegexPattern 144 * @stable ICU 2.4 145 */ 146 virtual RegexPattern *clone() const; 147 148 149 /** 150 * Compiles the regular expression in string form into a RegexPattern 151 * object. These compile methods, rather than the constructors, are the usual 152 * way that RegexPattern objects are created. 153 * 154 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 155 * objects created from the pattern are active. RegexMatchers keep a pointer 156 * back to their pattern, so premature deletion of the pattern is a 157 * catastrophic error.</p> 158 * 159 * <p>All pattern match mode flags are set to their default values.</p> 160 * 161 * <p>Note that it is often more convenient to construct a RegexMatcher directly 162 * from a pattern string rather than separately compiling the pattern and 163 * then creating a RegexMatcher object from the pattern.</p> 164 * 165 * @param regex The regular expression to be compiled. 166 * @param pe Receives the position (line and column nubers) of any error 167 * within the regular expression.) 168 * @param status A reference to a UErrorCode to receive any errors. 169 * @return A regexPattern object for the compiled pattern. 170 * 171 * @stable ICU 2.4 172 */ 173 static RegexPattern * U_EXPORT2 compile( const UnicodeString ®ex, 174 UParseError &pe, 175 UErrorCode &status); 176 177 /** 178 * Compiles the regular expression in string form into a RegexPattern 179 * object. These compile methods, rather than the constructors, are the usual 180 * way that RegexPattern objects are created. 181 * 182 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 183 * objects created from the pattern are active. RegexMatchers keep a pointer 184 * back to their pattern, so premature deletion of the pattern is a 185 * catastrophic error.</p> 186 * 187 * <p>All pattern match mode flags are set to their default values.</p> 188 * 189 * <p>Note that it is often more convenient to construct a RegexMatcher directly 190 * from a pattern string rather than separately compiling the pattern and 191 * then creating a RegexMatcher object from the pattern.</p> 192 * 193 * @param regex The regular expression to be compiled. Note, the text referred 194 * to by this UText must not be deleted during the lifetime of the 195 * RegexPattern object or any RegexMatcher object created from it. 196 * @param pe Receives the position (line and column nubers) of any error 197 * within the regular expression.) 198 * @param status A reference to a UErrorCode to receive any errors. 199 * @return A regexPattern object for the compiled pattern. 200 * 201 * @stable ICU 4.6 202 */ 203 static RegexPattern * U_EXPORT2 compile( UText *regex, 204 UParseError &pe, 205 UErrorCode &status); 206 207 /** 208 * Compiles the regular expression in string form into a RegexPattern 209 * object using the specified match mode flags. These compile methods, 210 * rather than the constructors, are the usual way that RegexPattern objects 211 * are created. 212 * 213 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 214 * objects created from the pattern are active. RegexMatchers keep a pointer 215 * back to their pattern, so premature deletion of the pattern is a 216 * catastrophic error.</p> 217 * 218 * <p>Note that it is often more convenient to construct a RegexMatcher directly 219 * from a pattern string instead of than separately compiling the pattern and 220 * then creating a RegexMatcher object from the pattern.</p> 221 * 222 * @param regex The regular expression to be compiled. 223 * @param flags The match mode flags to be used. 224 * @param pe Receives the position (line and column numbers) of any error 225 * within the regular expression.) 226 * @param status A reference to a UErrorCode to receive any errors. 227 * @return A regexPattern object for the compiled pattern. 228 * 229 * @stable ICU 2.4 230 */ 231 static RegexPattern * U_EXPORT2 compile( const UnicodeString ®ex, 232 uint32_t flags, 233 UParseError &pe, 234 UErrorCode &status); 235 236 /** 237 * Compiles the regular expression in string form into a RegexPattern 238 * object using the specified match mode flags. These compile methods, 239 * rather than the constructors, are the usual way that RegexPattern objects 240 * are created. 241 * 242 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 243 * objects created from the pattern are active. RegexMatchers keep a pointer 244 * back to their pattern, so premature deletion of the pattern is a 245 * catastrophic error.</p> 246 * 247 * <p>Note that it is often more convenient to construct a RegexMatcher directly 248 * from a pattern string instead of than separately compiling the pattern and 249 * then creating a RegexMatcher object from the pattern.</p> 250 * 251 * @param regex The regular expression to be compiled. Note, the text referred 252 * to by this UText must not be deleted during the lifetime of the 253 * RegexPattern object or any RegexMatcher object created from it. 254 * @param flags The match mode flags to be used. 255 * @param pe Receives the position (line and column numbers) of any error 256 * within the regular expression.) 257 * @param status A reference to a UErrorCode to receive any errors. 258 * @return A regexPattern object for the compiled pattern. 259 * 260 * @stable ICU 4.6 261 */ 262 static RegexPattern * U_EXPORT2 compile( UText *regex, 263 uint32_t flags, 264 UParseError &pe, 265 UErrorCode &status); 266 267 /** 268 * Compiles the regular expression in string form into a RegexPattern 269 * object using the specified match mode flags. These compile methods, 270 * rather than the constructors, are the usual way that RegexPattern objects 271 * are created. 272 * 273 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 274 * objects created from the pattern are active. RegexMatchers keep a pointer 275 * back to their pattern, so premature deletion of the pattern is a 276 * catastrophic error.</p> 277 * 278 * <p>Note that it is often more convenient to construct a RegexMatcher directly 279 * from a pattern string instead of than separately compiling the pattern and 280 * then creating a RegexMatcher object from the pattern.</p> 281 * 282 * @param regex The regular expression to be compiled. 283 * @param flags The match mode flags to be used. 284 * @param status A reference to a UErrorCode to receive any errors. 285 * @return A regexPattern object for the compiled pattern. 286 * 287 * @stable ICU 2.6 288 */ 289 static RegexPattern * U_EXPORT2 compile( const UnicodeString ®ex, 290 uint32_t flags, 291 UErrorCode &status); 292 293 /** 294 * Compiles the regular expression in string form into a RegexPattern 295 * object using the specified match mode flags. These compile methods, 296 * rather than the constructors, are the usual way that RegexPattern objects 297 * are created. 298 * 299 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher 300 * objects created from the pattern are active. RegexMatchers keep a pointer 301 * back to their pattern, so premature deletion of the pattern is a 302 * catastrophic error.</p> 303 * 304 * <p>Note that it is often more convenient to construct a RegexMatcher directly 305 * from a pattern string instead of than separately compiling the pattern and 306 * then creating a RegexMatcher object from the pattern.</p> 307 * 308 * @param regex The regular expression to be compiled. Note, the text referred 309 * to by this UText must not be deleted during the lifetime of the 310 * RegexPattern object or any RegexMatcher object created from it. 311 * @param flags The match mode flags to be used. 312 * @param status A reference to a UErrorCode to receive any errors. 313 * @return A regexPattern object for the compiled pattern. 314 * 315 * @stable ICU 4.6 316 */ 317 static RegexPattern * U_EXPORT2 compile( UText *regex, 318 uint32_t flags, 319 UErrorCode &status); 320 321 /** 322 * Get the match mode flags that were used when compiling this pattern. 323 * @return the match mode flags 324 * @stable ICU 2.4 325 */ 326 virtual uint32_t flags() const; 327 328 /** 329 * Creates a RegexMatcher that will match the given input against this pattern. The 330 * RegexMatcher can then be used to perform match, find or replace operations 331 * on the input. Note that a RegexPattern object must not be deleted while 332 * RegexMatchers created from it still exist and might possibly be used again. 333 * <p> 334 * The matcher will retain a reference to the supplied input string, and all regexp 335 * pattern matching operations happen directly on this original string. It is 336 * critical that the string not be altered or deleted before use by the regular 337 * expression operations is complete. 338 * 339 * @param input The input string to which the regular expression will be applied. 340 * @param status A reference to a UErrorCode to receive any errors. 341 * @return A RegexMatcher object for this pattern and input. 342 * 343 * @stable ICU 2.4 344 */ 345 virtual RegexMatcher *matcher(const UnicodeString &input, 346 UErrorCode &status) const; 347 348 private: 349 /** 350 * Cause a compilation error if an application accidentally attempts to 351 * create a matcher with a (UChar *) string as input rather than 352 * a UnicodeString. Avoids a dangling reference to a temporary string. 353 * <p> 354 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 355 * using one of the aliasing constructors, such as 356 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 357 * or in a UText, using 358 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 359 * 360 */ 361 RegexMatcher *matcher(const UChar *input, 362 UErrorCode &status) const; 363 public: 364 365 366 /** 367 * Creates a RegexMatcher that will match against this pattern. The 368 * RegexMatcher can be used to perform match, find or replace operations. 369 * Note that a RegexPattern object must not be deleted while 370 * RegexMatchers created from it still exist and might possibly be used again. 371 * 372 * @param status A reference to a UErrorCode to receive any errors. 373 * @return A RegexMatcher object for this pattern and input. 374 * 375 * @stable ICU 2.6 376 */ 377 virtual RegexMatcher *matcher(UErrorCode &status) const; 378 379 380 /** 381 * Test whether a string matches a regular expression. This convenience function 382 * both compiles the regular expression and applies it in a single operation. 383 * Note that if the same pattern needs to be applied repeatedly, this method will be 384 * less efficient than creating and reusing a RegexMatcher object. 385 * 386 * @param regex The regular expression 387 * @param input The string data to be matched 388 * @param pe Receives the position of any syntax errors within the regular expression 389 * @param status A reference to a UErrorCode to receive any errors. 390 * @return True if the regular expression exactly matches the full input string. 391 * 392 * @stable ICU 2.4 393 */ 394 static UBool U_EXPORT2 matches(const UnicodeString ®ex, 395 const UnicodeString &input, 396 UParseError &pe, 397 UErrorCode &status); 398 399 /** 400 * Test whether a string matches a regular expression. This convenience function 401 * both compiles the regular expression and applies it in a single operation. 402 * Note that if the same pattern needs to be applied repeatedly, this method will be 403 * less efficient than creating and reusing a RegexMatcher object. 404 * 405 * @param regex The regular expression 406 * @param input The string data to be matched 407 * @param pe Receives the position of any syntax errors within the regular expression 408 * @param status A reference to a UErrorCode to receive any errors. 409 * @return True if the regular expression exactly matches the full input string. 410 * 411 * @stable ICU 4.6 412 */ 413 static UBool U_EXPORT2 matches(UText *regex, 414 UText *input, 415 UParseError &pe, 416 UErrorCode &status); 417 418 /** 419 * Returns the regular expression from which this pattern was compiled. This method will work 420 * even if the pattern was compiled from a UText. 421 * 422 * Note: If the pattern was originally compiled from a UText, and that UText was modified, 423 * the returned string may no longer reflect the RegexPattern object. 424 * @stable ICU 2.4 425 */ 426 virtual UnicodeString pattern() const; 427 428 429 /** 430 * Returns the regular expression from which this pattern was compiled. This method will work 431 * even if the pattern was compiled from a UnicodeString. 432 * 433 * Note: This is the original input, not a clone. If the pattern was originally compiled from a 434 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern 435 * object. 436 * 437 * @stable ICU 4.6 438 */ 439 virtual UText *patternText(UErrorCode &status) const; 440 441 442 /** 443 * Get the group number corresponding to a named capture group. 444 * The returned number can be used with any function that access 445 * capture groups by number. 446 * 447 * The function returns an error status if the specified name does not 448 * appear in the pattern. 449 * 450 * @param groupName The capture group name. 451 * @param status A UErrorCode to receive any errors. 452 * 453 * @draft ICU 55 454 */ 455 virtual int32_t groupNumberFromName(const UnicodeString &groupName, UErrorCode &status) const; 456 457 458 /** 459 * Get the group number corresponding to a named capture group. 460 * The returned number can be used with any function that access 461 * capture groups by number. 462 * 463 * The function returns an error status if the specified name does not 464 * appear in the pattern. 465 * 466 * @param groupName The capture group name, 467 * platform invariant characters only. 468 * @param nameLength The length of the name, or -1 if the name is 469 * nul-terminated. 470 * @param status A UErrorCode to receive any errors. 471 * 472 * @draft ICU 55 473 */ 474 virtual int32_t groupNumberFromName(const char *groupName, int32_t nameLength, UErrorCode &status) const; 475 476 477 /** 478 * Split a string into fields. Somewhat like split() from Perl or Java. 479 * Pattern matches identify delimiters that separate the input 480 * into fields. The input data between the delimiters becomes the 481 * fields themselves. 482 * 483 * If the delimiter pattern includes capture groups, the captured text will 484 * also appear in the destination array of output strings, interspersed 485 * with the fields. This is similar to Perl, but differs from Java, 486 * which ignores the presence of capture groups in the pattern. 487 * 488 * Trailing empty fields will always be returned, assuming sufficient 489 * destination capacity. This differs from the default behavior for Java 490 * and Perl where trailing empty fields are not returned. 491 * 492 * The number of strings produced by the split operation is returned. 493 * This count includes the strings from capture groups in the delimiter pattern. 494 * This behavior differs from Java, which ignores capture groups. 495 * 496 * For the best performance on split() operations, 497 * <code>RegexMatcher::split</code> is preferable to this function 498 * 499 * @param input The string to be split into fields. The field delimiters 500 * match the pattern (in the "this" object) 501 * @param dest An array of UnicodeStrings to receive the results of the split. 502 * This is an array of actual UnicodeString objects, not an 503 * array of pointers to strings. Local (stack based) arrays can 504 * work well here. 505 * @param destCapacity The number of elements in the destination array. 506 * If the number of fields found is less than destCapacity, the 507 * extra strings in the destination array are not altered. 508 * If the number of destination strings is less than the number 509 * of fields, the trailing part of the input string, including any 510 * field delimiters, is placed in the last destination string. 511 * @param status A reference to a UErrorCode to receive any errors. 512 * @return The number of fields into which the input string was split. 513 * @stable ICU 2.4 514 */ 515 virtual int32_t split(const UnicodeString &input, 516 UnicodeString dest[], 517 int32_t destCapacity, 518 UErrorCode &status) const; 519 520 521 /** 522 * Split a string into fields. Somewhat like split() from Perl or Java. 523 * Pattern matches identify delimiters that separate the input 524 * into fields. The input data between the delimiters becomes the 525 * fields themselves. 526 * 527 * If the delimiter pattern includes capture groups, the captured text will 528 * also appear in the destination array of output strings, interspersed 529 * with the fields. This is similar to Perl, but differs from Java, 530 * which ignores the presence of capture groups in the pattern. 531 * 532 * Trailing empty fields will always be returned, assuming sufficient 533 * destination capacity. This differs from the default behavior for Java 534 * and Perl where trailing empty fields are not returned. 535 * 536 * The number of strings produced by the split operation is returned. 537 * This count includes the strings from capture groups in the delimiter pattern. 538 * This behavior differs from Java, which ignores capture groups. 539 * 540 * For the best performance on split() operations, 541 * <code>RegexMatcher::split</code> is preferable to this function 542 * 543 * @param input The string to be split into fields. The field delimiters 544 * match the pattern (in the "this" object) 545 * @param dest An array of mutable UText structs to receive the results of the split. 546 * If a field is NULL, a new UText is allocated to contain the results for 547 * that field. This new UText is not guaranteed to be mutable. 548 * @param destCapacity The number of elements in the destination array. 549 * If the number of fields found is less than destCapacity, the 550 * extra strings in the destination array are not altered. 551 * If the number of destination strings is less than the number 552 * of fields, the trailing part of the input string, including any 553 * field delimiters, is placed in the last destination string. 554 * @param status A reference to a UErrorCode to receive any errors. 555 * @return The number of destination strings used. 556 * 557 * @stable ICU 4.6 558 */ 559 virtual int32_t split(UText *input, 560 UText *dest[], 561 int32_t destCapacity, 562 UErrorCode &status) const; 563 564 565 /** 566 * ICU "poor man's RTTI", returns a UClassID for the actual class. 567 * 568 * @stable ICU 2.4 569 */ 570 virtual UClassID getDynamicClassID() const; 571 572 /** 573 * ICU "poor man's RTTI", returns a UClassID for this class. 574 * 575 * @stable ICU 2.4 576 */ 577 static UClassID U_EXPORT2 getStaticClassID(); 578 579 private: 580 // 581 // Implementation Data 582 // 583 UText *fPattern; // The original pattern string. 584 UnicodeString *fPatternString; // The original pattern UncodeString if relevant 585 uint32_t fFlags; // The flags used when compiling the pattern. 586 // 587 UVector64 *fCompiledPat; // The compiled pattern p-code. 588 UnicodeString fLiteralText; // Any literal string data from the pattern, 589 // after un-escaping, for use during the match. 590 591 UVector *fSets; // Any UnicodeSets referenced from the pattern. 592 Regex8BitSet *fSets8; // (and fast sets for latin-1 range.) 593 594 595 UErrorCode fDeferredStatus; // status if some prior error has left this 596 // RegexPattern in an unusable state. 597 598 int32_t fMinMatchLen; // Minimum Match Length. All matches will have length 599 // >= this value. For some patterns, this calculated 600 // value may be less than the true shortest 601 // possible match. 602 603 int32_t fFrameSize; // Size of a state stack frame in the 604 // execution engine. 605 606 int32_t fDataSize; // The size of the data needed by the pattern that 607 // does not go on the state stack, but has just 608 // a single copy per matcher. 609 610 UVector32 *fGroupMap; // Map from capture group number to position of 611 // the group's variables in the matcher stack frame. 612 613 UnicodeSet **fStaticSets; // Ptr to static (shared) sets for predefined 614 // regex character classes, e.g. Word. 615 616 Regex8BitSet *fStaticSets8; // Ptr to the static (shared) latin-1 only 617 // sets for predefined regex classes. 618 619 int32_t fStartType; // Info on how a match must start. 620 int32_t fInitialStringIdx; // 621 int32_t fInitialStringLen; 622 UnicodeSet *fInitialChars; 623 UChar32 fInitialChar; 624 Regex8BitSet *fInitialChars8; 625 UBool fNeedsAltInput; 626 627 UHashtable *fNamedCaptureMap; // Map from capture group names to numbers. 628 629 friend class RegexCompile; 630 friend class RegexMatcher; 631 friend class RegexCImpl; 632 633 // 634 // Implementation Methods 635 // 636 void init(); // Common initialization, for use by constructors. 637 void zap(); // Common cleanup 638 639 void dumpOp(int32_t index) const; 640 641 public: 642 #ifndef U_HIDE_INTERNAL_API 643 /** 644 * Dump a compiled pattern. Internal debug function. 645 * @internal 646 */ 647 void dumpPattern() const; 648 #endif /* U_HIDE_INTERNAL_API */ 649 }; 650 651 652 653 /** 654 * class RegexMatcher bundles together a regular expression pattern and 655 * input text to which the expression can be applied. It includes methods 656 * for testing for matches, and for find and replace operations. 657 * 658 * <p>Class RegexMatcher is not intended to be subclassed.</p> 659 * 660 * @stable ICU 2.4 661 */ 662 class U_I18N_API RegexMatcher U_FINAL : public UObject { 663 public: 664 665 /** 666 * Construct a RegexMatcher for a regular expression. 667 * This is a convenience method that avoids the need to explicitly create 668 * a RegexPattern object. Note that if several RegexMatchers need to be 669 * created for the same expression, it will be more efficient to 670 * separately create and cache a RegexPattern object, and use 671 * its matcher() method to create the RegexMatcher objects. 672 * 673 * @param regexp The Regular Expression to be compiled. 674 * @param flags Regular expression options, such as case insensitive matching. 675 * @see UREGEX_CASE_INSENSITIVE 676 * @param status Any errors are reported by setting this UErrorCode variable. 677 * @stable ICU 2.6 678 */ 679 RegexMatcher(const UnicodeString ®exp, uint32_t flags, UErrorCode &status); 680 681 /** 682 * Construct a RegexMatcher for a regular expression. 683 * This is a convenience method that avoids the need to explicitly create 684 * a RegexPattern object. Note that if several RegexMatchers need to be 685 * created for the same expression, it will be more efficient to 686 * separately create and cache a RegexPattern object, and use 687 * its matcher() method to create the RegexMatcher objects. 688 * 689 * @param regexp The regular expression to be compiled. 690 * @param flags Regular expression options, such as case insensitive matching. 691 * @see UREGEX_CASE_INSENSITIVE 692 * @param status Any errors are reported by setting this UErrorCode variable. 693 * 694 * @stable ICU 4.6 695 */ 696 RegexMatcher(UText *regexp, uint32_t flags, UErrorCode &status); 697 698 /** 699 * Construct a RegexMatcher for a regular expression. 700 * This is a convenience method that avoids the need to explicitly create 701 * a RegexPattern object. Note that if several RegexMatchers need to be 702 * created for the same expression, it will be more efficient to 703 * separately create and cache a RegexPattern object, and use 704 * its matcher() method to create the RegexMatcher objects. 705 * <p> 706 * The matcher will retain a reference to the supplied input string, and all regexp 707 * pattern matching operations happen directly on the original string. It is 708 * critical that the string not be altered or deleted before use by the regular 709 * expression operations is complete. 710 * 711 * @param regexp The Regular Expression to be compiled. 712 * @param input The string to match. The matcher retains a reference to the 713 * caller's string; mo copy is made. 714 * @param flags Regular expression options, such as case insensitive matching. 715 * @see UREGEX_CASE_INSENSITIVE 716 * @param status Any errors are reported by setting this UErrorCode variable. 717 * @stable ICU 2.6 718 */ 719 RegexMatcher(const UnicodeString ®exp, const UnicodeString &input, 720 uint32_t flags, UErrorCode &status); 721 722 /** 723 * Construct a RegexMatcher for a regular expression. 724 * This is a convenience method that avoids the need to explicitly create 725 * a RegexPattern object. Note that if several RegexMatchers need to be 726 * created for the same expression, it will be more efficient to 727 * separately create and cache a RegexPattern object, and use 728 * its matcher() method to create the RegexMatcher objects. 729 * <p> 730 * The matcher will make a shallow clone of the supplied input text, and all regexp 731 * pattern matching operations happen on this clone. While read-only operations on 732 * the supplied text are permitted, it is critical that the underlying string not be 733 * altered or deleted before use by the regular expression operations is complete. 734 * 735 * @param regexp The Regular Expression to be compiled. 736 * @param input The string to match. The matcher retains a shallow clone of the text. 737 * @param flags Regular expression options, such as case insensitive matching. 738 * @see UREGEX_CASE_INSENSITIVE 739 * @param status Any errors are reported by setting this UErrorCode variable. 740 * 741 * @stable ICU 4.6 742 */ 743 RegexMatcher(UText *regexp, UText *input, 744 uint32_t flags, UErrorCode &status); 745 746 private: 747 /** 748 * Cause a compilation error if an application accidentally attempts to 749 * create a matcher with a (UChar *) string as input rather than 750 * a UnicodeString. Avoids a dangling reference to a temporary string. 751 * <p> 752 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 753 * using one of the aliasing constructors, such as 754 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 755 * or in a UText, using 756 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 757 * 758 */ 759 RegexMatcher(const UnicodeString ®exp, const UChar *input, 760 uint32_t flags, UErrorCode &status); 761 public: 762 763 764 /** 765 * Destructor. 766 * 767 * @stable ICU 2.4 768 */ 769 virtual ~RegexMatcher(); 770 771 772 /** 773 * Attempts to match the entire input region against the pattern. 774 * @param status A reference to a UErrorCode to receive any errors. 775 * @return TRUE if there is a match 776 * @stable ICU 2.4 777 */ 778 virtual UBool matches(UErrorCode &status); 779 780 781 /** 782 * Resets the matcher, then attempts to match the input beginning 783 * at the specified startIndex, and extending to the end of the input. 784 * The input region is reset to include the entire input string. 785 * A successful match must extend to the end of the input. 786 * @param startIndex The input string (native) index at which to begin matching. 787 * @param status A reference to a UErrorCode to receive any errors. 788 * @return TRUE if there is a match 789 * @stable ICU 2.8 790 */ 791 virtual UBool matches(int64_t startIndex, UErrorCode &status); 792 793 794 /** 795 * Attempts to match the input string, starting from the beginning of the region, 796 * against the pattern. Like the matches() method, this function 797 * always starts at the beginning of the input region; 798 * unlike that function, it does not require that the entire region be matched. 799 * 800 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>, 801 * <code>end()</code>, and <code>group()</code> functions.</p> 802 * 803 * @param status A reference to a UErrorCode to receive any errors. 804 * @return TRUE if there is a match at the start of the input string. 805 * @stable ICU 2.4 806 */ 807 virtual UBool lookingAt(UErrorCode &status); 808 809 810 /** 811 * Attempts to match the input string, starting from the specified index, against the pattern. 812 * The match may be of any length, and is not required to extend to the end 813 * of the input string. Contrast with match(). 814 * 815 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>, 816 * <code>end()</code>, and <code>group()</code> functions.</p> 817 * 818 * @param startIndex The input string (native) index at which to begin matching. 819 * @param status A reference to a UErrorCode to receive any errors. 820 * @return TRUE if there is a match. 821 * @stable ICU 2.8 822 */ 823 virtual UBool lookingAt(int64_t startIndex, UErrorCode &status); 824 825 826 /** 827 * Find the next pattern match in the input string. 828 * The find begins searching the input at the location following the end of 829 * the previous match, or at the start of the string if there is no previous match. 830 * If a match is found, <code>start(), end()</code> and <code>group()</code> 831 * will provide more information regarding the match. 832 * <p>Note that if the input string is changed by the application, 833 * use find(startPos, status) instead of find(), because the saved starting 834 * position may not be valid with the altered input string.</p> 835 * @return TRUE if a match is found. 836 * @stable ICU 2.4 837 */ 838 virtual UBool find(); 839 840 841 /** 842 * Find the next pattern match in the input string. 843 * The find begins searching the input at the location following the end of 844 * the previous match, or at the start of the string if there is no previous match. 845 * If a match is found, <code>start(), end()</code> and <code>group()</code> 846 * will provide more information regarding the match. 847 * <p>Note that if the input string is changed by the application, 848 * use find(startPos, status) instead of find(), because the saved starting 849 * position may not be valid with the altered input string.</p> 850 * @param status A reference to a UErrorCode to receive any errors. 851 * @return TRUE if a match is found. 852 * @draft ICU 55 853 */ 854 virtual UBool find(UErrorCode &status); 855 856 /** 857 * Resets this RegexMatcher and then attempts to find the next substring of the 858 * input string that matches the pattern, starting at the specified index. 859 * 860 * @param start The (native) index in the input string to begin the search. 861 * @param status A reference to a UErrorCode to receive any errors. 862 * @return TRUE if a match is found. 863 * @stable ICU 2.4 864 */ 865 virtual UBool find(int64_t start, UErrorCode &status); 866 867 868 /** 869 * Returns a string containing the text matched by the previous match. 870 * If the pattern can match an empty string, an empty string may be returned. 871 * @param status A reference to a UErrorCode to receive any errors. 872 * Possible errors are U_REGEX_INVALID_STATE if no match 873 * has been attempted or the last match failed. 874 * @return a string containing the matched input text. 875 * @stable ICU 2.4 876 */ 877 virtual UnicodeString group(UErrorCode &status) const; 878 879 880 /** 881 * Returns a string containing the text captured by the given group 882 * during the previous match operation. Group(0) is the entire match. 883 * 884 * A zero length string is returned both for capture groups that did not 885 * participate in the match and for actual zero length matches. 886 * To distinguish between these two cases use the function start(), 887 * which returns -1 for non-participating groups. 888 * 889 * @param groupNum the capture group number 890 * @param status A reference to a UErrorCode to receive any errors. 891 * Possible errors are U_REGEX_INVALID_STATE if no match 892 * has been attempted or the last match failed and 893 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 894 * @return the captured text 895 * @stable ICU 2.4 896 */ 897 virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const; 898 899 /** 900 * Returns the number of capturing groups in this matcher's pattern. 901 * @return the number of capture groups 902 * @stable ICU 2.4 903 */ 904 virtual int32_t groupCount() const; 905 906 907 /** 908 * Returns a shallow clone of the entire live input string with the UText current native index 909 * set to the beginning of the requested group. 910 * 911 * @param dest The UText into which the input should be cloned, or NULL to create a new UText 912 * @param group_len A reference to receive the length of the desired capture group 913 * @param status A reference to a UErrorCode to receive any errors. 914 * Possible errors are U_REGEX_INVALID_STATE if no match 915 * has been attempted or the last match failed and 916 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 917 * @return dest if non-NULL, a shallow copy of the input text otherwise 918 * 919 * @stable ICU 4.6 920 */ 921 virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const; 922 923 /** 924 * Returns a shallow clone of the entire live input string with the UText current native index 925 * set to the beginning of the requested group. 926 * 927 * A group length of zero is returned both for capture groups that did not 928 * participate in the match and for actual zero length matches. 929 * To distinguish between these two cases use the function start(), 930 * which returns -1 for non-participating groups. 931 * 932 * @param groupNum The capture group number. 933 * @param dest The UText into which the input should be cloned, or NULL to create a new UText. 934 * @param group_len A reference to receive the length of the desired capture group 935 * @param status A reference to a UErrorCode to receive any errors. 936 * Possible errors are U_REGEX_INVALID_STATE if no match 937 * has been attempted or the last match failed and 938 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 939 * @return dest if non-NULL, a shallow copy of the input text otherwise 940 * 941 * @stable ICU 4.6 942 */ 943 virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const; 944 945 /** 946 * Returns the index in the input string of the start of the text matched 947 * during the previous match operation. 948 * @param status a reference to a UErrorCode to receive any errors. 949 * @return The (native) position in the input string of the start of the last match. 950 * @stable ICU 2.4 951 */ 952 virtual int32_t start(UErrorCode &status) const; 953 954 /** 955 * Returns the index in the input string of the start of the text matched 956 * during the previous match operation. 957 * @param status a reference to a UErrorCode to receive any errors. 958 * @return The (native) position in the input string of the start of the last match. 959 * @stable ICU 4.6 960 */ 961 virtual int64_t start64(UErrorCode &status) const; 962 963 964 /** 965 * Returns the index in the input string of the start of the text matched by the 966 * specified capture group during the previous match operation. Return -1 if 967 * the capture group exists in the pattern, but was not part of the last match. 968 * 969 * @param group the capture group number 970 * @param status A reference to a UErrorCode to receive any errors. Possible 971 * errors are U_REGEX_INVALID_STATE if no match has been 972 * attempted or the last match failed, and 973 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 974 * @return the (native) start position of substring matched by the specified group. 975 * @stable ICU 2.4 976 */ 977 virtual int32_t start(int32_t group, UErrorCode &status) const; 978 979 /** 980 * Returns the index in the input string of the start of the text matched by the 981 * specified capture group during the previous match operation. Return -1 if 982 * the capture group exists in the pattern, but was not part of the last match. 983 * 984 * @param group the capture group number. 985 * @param status A reference to a UErrorCode to receive any errors. Possible 986 * errors are U_REGEX_INVALID_STATE if no match has been 987 * attempted or the last match failed, and 988 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 989 * @return the (native) start position of substring matched by the specified group. 990 * @stable ICU 4.6 991 */ 992 virtual int64_t start64(int32_t group, UErrorCode &status) const; 993 994 /** 995 * Returns the index in the input string of the first character following the 996 * text matched during the previous match operation. 997 * 998 * @param status A reference to a UErrorCode to receive any errors. Possible 999 * errors are U_REGEX_INVALID_STATE if no match has been 1000 * attempted or the last match failed. 1001 * @return the index of the last character matched, plus one. 1002 * The index value returned is a native index, corresponding to 1003 * code units for the underlying encoding type, for example, 1004 * a byte index for UTF-8. 1005 * @stable ICU 2.4 1006 */ 1007 virtual int32_t end(UErrorCode &status) const; 1008 1009 /** 1010 * Returns the index in the input string of the first character following the 1011 * text matched during the previous match operation. 1012 * 1013 * @param status A reference to a UErrorCode to receive any errors. Possible 1014 * errors are U_REGEX_INVALID_STATE if no match has been 1015 * attempted or the last match failed. 1016 * @return the index of the last character matched, plus one. 1017 * The index value returned is a native index, corresponding to 1018 * code units for the underlying encoding type, for example, 1019 * a byte index for UTF-8. 1020 * @stable ICU 4.6 1021 */ 1022 virtual int64_t end64(UErrorCode &status) const; 1023 1024 1025 /** 1026 * Returns the index in the input string of the character following the 1027 * text matched by the specified capture group during the previous match operation. 1028 * 1029 * @param group the capture group number 1030 * @param status A reference to a UErrorCode to receive any errors. Possible 1031 * errors are U_REGEX_INVALID_STATE if no match has been 1032 * attempted or the last match failed and 1033 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 1034 * @return the index of the first character following the text 1035 * captured by the specified group during the previous match operation. 1036 * Return -1 if the capture group exists in the pattern but was not part of the match. 1037 * The index value returned is a native index, corresponding to 1038 * code units for the underlying encoding type, for example, 1039 * a byte index for UTF8. 1040 * @stable ICU 2.4 1041 */ 1042 virtual int32_t end(int32_t group, UErrorCode &status) const; 1043 1044 /** 1045 * Returns the index in the input string of the character following the 1046 * text matched by the specified capture group during the previous match operation. 1047 * 1048 * @param group the capture group number 1049 * @param status A reference to a UErrorCode to receive any errors. Possible 1050 * errors are U_REGEX_INVALID_STATE if no match has been 1051 * attempted or the last match failed and 1052 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 1053 * @return the index of the first character following the text 1054 * captured by the specified group during the previous match operation. 1055 * Return -1 if the capture group exists in the pattern but was not part of the match. 1056 * The index value returned is a native index, corresponding to 1057 * code units for the underlying encoding type, for example, 1058 * a byte index for UTF8. 1059 * @stable ICU 4.6 1060 */ 1061 virtual int64_t end64(int32_t group, UErrorCode &status) const; 1062 1063 /** 1064 * Resets this matcher. The effect is to remove any memory of previous matches, 1065 * and to cause subsequent find() operations to begin at the beginning of 1066 * the input string. 1067 * 1068 * @return this RegexMatcher. 1069 * @stable ICU 2.4 1070 */ 1071 virtual RegexMatcher &reset(); 1072 1073 1074 /** 1075 * Resets this matcher, and set the current input position. 1076 * The effect is to remove any memory of previous matches, 1077 * and to cause subsequent find() operations to begin at 1078 * the specified (native) position in the input string. 1079 * <p> 1080 * The matcher's region is reset to its default, which is the entire 1081 * input string. 1082 * <p> 1083 * An alternative to this function is to set a match region 1084 * beginning at the desired index. 1085 * 1086 * @return this RegexMatcher. 1087 * @stable ICU 2.8 1088 */ 1089 virtual RegexMatcher &reset(int64_t index, UErrorCode &status); 1090 1091 1092 /** 1093 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1094 * to be reused, which is more efficient than creating a new RegexMatcher for 1095 * each input string to be processed. 1096 * @param input The new string on which subsequent pattern matches will operate. 1097 * The matcher retains a reference to the callers string, and operates 1098 * directly on that. Ownership of the string remains with the caller. 1099 * Because no copy of the string is made, it is essential that the 1100 * caller not delete the string until after regexp operations on it 1101 * are done. 1102 * Note that while a reset on the matcher with an input string that is then 1103 * modified across/during matcher operations may be supported currently for UnicodeString, 1104 * this was not originally intended behavior, and support for this is not guaranteed 1105 * in upcoming versions of ICU. 1106 * @return this RegexMatcher. 1107 * @stable ICU 2.4 1108 */ 1109 virtual RegexMatcher &reset(const UnicodeString &input); 1110 1111 1112 /** 1113 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1114 * to be reused, which is more efficient than creating a new RegexMatcher for 1115 * each input string to be processed. 1116 * @param input The new string on which subsequent pattern matches will operate. 1117 * The matcher makes a shallow clone of the given text; ownership of the 1118 * original string remains with the caller. Because no deep copy of the 1119 * text is made, it is essential that the caller not modify the string 1120 * until after regexp operations on it are done. 1121 * @return this RegexMatcher. 1122 * 1123 * @stable ICU 4.6 1124 */ 1125 virtual RegexMatcher &reset(UText *input); 1126 1127 1128 /** 1129 * Set the subject text string upon which the regular expression is looking for matches 1130 * without changing any other aspect of the matching state. 1131 * The new and previous text strings must have the same content. 1132 * 1133 * This function is intended for use in environments where ICU is operating on 1134 * strings that may move around in memory. It provides a mechanism for notifying 1135 * ICU that the string has been relocated, and providing a new UText to access the 1136 * string in its new position. 1137 * 1138 * Note that the regular expression implementation never copies the underlying text 1139 * of a string being matched, but always operates directly on the original text 1140 * provided by the user. Refreshing simply drops the references to the old text 1141 * and replaces them with references to the new. 1142 * 1143 * Caution: this function is normally used only by very specialized, 1144 * system-level code. One example use case is with garbage collection that moves 1145 * the text in memory. 1146 * 1147 * @param input The new (moved) text string. 1148 * @param status Receives errors detected by this function. 1149 * 1150 * @stable ICU 4.8 1151 */ 1152 virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status); 1153 1154 private: 1155 /** 1156 * Cause a compilation error if an application accidentally attempts to 1157 * reset a matcher with a (UChar *) string as input rather than 1158 * a UnicodeString. Avoids a dangling reference to a temporary string. 1159 * <p> 1160 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 1161 * using one of the aliasing constructors, such as 1162 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 1163 * or in a UText, using 1164 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 1165 * 1166 */ 1167 RegexMatcher &reset(const UChar *input); 1168 public: 1169 1170 /** 1171 * Returns the input string being matched. Ownership of the string belongs to 1172 * the matcher; it should not be altered or deleted. This method will work even if the input 1173 * was originally supplied as a UText. 1174 * @return the input string 1175 * @stable ICU 2.4 1176 */ 1177 virtual const UnicodeString &input() const; 1178 1179 /** 1180 * Returns the input string being matched. This is the live input text; it should not be 1181 * altered or deleted. This method will work even if the input was originally supplied as 1182 * a UnicodeString. 1183 * @return the input text 1184 * 1185 * @stable ICU 4.6 1186 */ 1187 virtual UText *inputText() const; 1188 1189 /** 1190 * Returns the input string being matched, either by copying it into the provided 1191 * UText parameter or by returning a shallow clone of the live input. Note that copying 1192 * the entire input may cause significant performance and memory issues. 1193 * @param dest The UText into which the input should be copied, or NULL to create a new UText 1194 * @param status error code 1195 * @return dest if non-NULL, a shallow copy of the input text otherwise 1196 * 1197 * @stable ICU 4.6 1198 */ 1199 virtual UText *getInput(UText *dest, UErrorCode &status) const; 1200 1201 1202 /** Sets the limits of this matcher's region. 1203 * The region is the part of the input string that will be searched to find a match. 1204 * Invoking this method resets the matcher, and then sets the region to start 1205 * at the index specified by the start parameter and end at the index specified 1206 * by the end parameter. 1207 * 1208 * Depending on the transparency and anchoring being used (see useTransparentBounds 1209 * and useAnchoringBounds), certain constructs such as anchors may behave differently 1210 * at or around the boundaries of the region 1211 * 1212 * The function will fail if start is greater than limit, or if either index 1213 * is less than zero or greater than the length of the string being matched. 1214 * 1215 * @param start The (native) index to begin searches at. 1216 * @param limit The index to end searches at (exclusive). 1217 * @param status A reference to a UErrorCode to receive any errors. 1218 * @stable ICU 4.0 1219 */ 1220 virtual RegexMatcher ®ion(int64_t start, int64_t limit, UErrorCode &status); 1221 1222 /** 1223 * Identical to region(start, limit, status) but also allows a start position without 1224 * resetting the region state. 1225 * @param regionStart The region start 1226 * @param regionLimit the limit of the region 1227 * @param startIndex The (native) index within the region bounds at which to begin searches. 1228 * @param status A reference to a UErrorCode to receive any errors. 1229 * If startIndex is not within the specified region bounds, 1230 * U_INDEX_OUTOFBOUNDS_ERROR is returned. 1231 * @stable ICU 4.6 1232 */ 1233 virtual RegexMatcher ®ion(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status); 1234 1235 /** 1236 * Reports the start index of this matcher's region. The searches this matcher 1237 * conducts are limited to finding matches within regionStart (inclusive) and 1238 * regionEnd (exclusive). 1239 * 1240 * @return The starting (native) index of this matcher's region. 1241 * @stable ICU 4.0 1242 */ 1243 virtual int32_t regionStart() const; 1244 1245 /** 1246 * Reports the start index of this matcher's region. The searches this matcher 1247 * conducts are limited to finding matches within regionStart (inclusive) and 1248 * regionEnd (exclusive). 1249 * 1250 * @return The starting (native) index of this matcher's region. 1251 * @stable ICU 4.6 1252 */ 1253 virtual int64_t regionStart64() const; 1254 1255 1256 /** 1257 * Reports the end (limit) index (exclusive) of this matcher's region. The searches 1258 * this matcher conducts are limited to finding matches within regionStart 1259 * (inclusive) and regionEnd (exclusive). 1260 * 1261 * @return The ending point (native) of this matcher's region. 1262 * @stable ICU 4.0 1263 */ 1264 virtual int32_t regionEnd() const; 1265 1266 /** 1267 * Reports the end (limit) index (exclusive) of this matcher's region. The searches 1268 * this matcher conducts are limited to finding matches within regionStart 1269 * (inclusive) and regionEnd (exclusive). 1270 * 1271 * @return The ending point (native) of this matcher's region. 1272 * @stable ICU 4.6 1273 */ 1274 virtual int64_t regionEnd64() const; 1275 1276 /** 1277 * Queries the transparency of region bounds for this matcher. 1278 * See useTransparentBounds for a description of transparent and opaque bounds. 1279 * By default, a matcher uses opaque region boundaries. 1280 * 1281 * @return TRUE if this matcher is using opaque bounds, false if it is not. 1282 * @stable ICU 4.0 1283 */ 1284 virtual UBool hasTransparentBounds() const; 1285 1286 /** 1287 * Sets the transparency of region bounds for this matcher. 1288 * Invoking this function with an argument of true will set this matcher to use transparent bounds. 1289 * If the boolean argument is false, then opaque bounds will be used. 1290 * 1291 * Using transparent bounds, the boundaries of this matcher's region are transparent 1292 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can 1293 * see text beyond the boundaries of the region while checking for a match. 1294 * 1295 * With opaque bounds, no text outside of the matcher's region is visible to lookahead, 1296 * lookbehind, and boundary matching constructs. 1297 * 1298 * By default, a matcher uses opaque bounds. 1299 * 1300 * @param b TRUE for transparent bounds; FALSE for opaque bounds 1301 * @return This Matcher; 1302 * @stable ICU 4.0 1303 **/ 1304 virtual RegexMatcher &useTransparentBounds(UBool b); 1305 1306 1307 /** 1308 * Return true if this matcher is using anchoring bounds. 1309 * By default, matchers use anchoring region bounds. 1310 * 1311 * @return TRUE if this matcher is using anchoring bounds. 1312 * @stable ICU 4.0 1313 */ 1314 virtual UBool hasAnchoringBounds() const; 1315 1316 1317 /** 1318 * Set whether this matcher is using Anchoring Bounds for its region. 1319 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start 1320 * and end of the region. Without Anchoring Bounds, anchors will only match at 1321 * the positions they would in the complete text. 1322 * 1323 * Anchoring Bounds are the default for regions. 1324 * 1325 * @param b TRUE if to enable anchoring bounds; FALSE to disable them. 1326 * @return This Matcher 1327 * @stable ICU 4.0 1328 */ 1329 virtual RegexMatcher &useAnchoringBounds(UBool b); 1330 1331 1332 /** 1333 * Return TRUE if the most recent matching operation attempted to access 1334 * additional input beyond the available input text. 1335 * In this case, additional input text could change the results of the match. 1336 * 1337 * hitEnd() is defined for both successful and unsuccessful matches. 1338 * In either case hitEnd() will return TRUE if if the end of the text was 1339 * reached at any point during the matching process. 1340 * 1341 * @return TRUE if the most recent match hit the end of input 1342 * @stable ICU 4.0 1343 */ 1344 virtual UBool hitEnd() const; 1345 1346 /** 1347 * Return TRUE the most recent match succeeded and additional input could cause 1348 * it to fail. If this method returns false and a match was found, then more input 1349 * might change the match but the match won't be lost. If a match was not found, 1350 * then requireEnd has no meaning. 1351 * 1352 * @return TRUE if more input could cause the most recent match to no longer match. 1353 * @stable ICU 4.0 1354 */ 1355 virtual UBool requireEnd() const; 1356 1357 1358 /** 1359 * Returns the pattern that is interpreted by this matcher. 1360 * @return the RegexPattern for this RegexMatcher 1361 * @stable ICU 2.4 1362 */ 1363 virtual const RegexPattern &pattern() const; 1364 1365 1366 /** 1367 * Replaces every substring of the input that matches the pattern 1368 * with the given replacement string. This is a convenience function that 1369 * provides a complete find-and-replace-all operation. 1370 * 1371 * This method first resets this matcher. It then scans the input string 1372 * looking for matches of the pattern. Input that is not part of any 1373 * match is left unchanged; each match is replaced in the result by the 1374 * replacement string. The replacement string may contain references to 1375 * capture groups. 1376 * 1377 * @param replacement a string containing the replacement text. 1378 * @param status a reference to a UErrorCode to receive any errors. 1379 * @return a string containing the results of the find and replace. 1380 * @stable ICU 2.4 1381 */ 1382 virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status); 1383 1384 1385 /** 1386 * Replaces every substring of the input that matches the pattern 1387 * with the given replacement string. This is a convenience function that 1388 * provides a complete find-and-replace-all operation. 1389 * 1390 * This method first resets this matcher. It then scans the input string 1391 * looking for matches of the pattern. Input that is not part of any 1392 * match is left unchanged; each match is replaced in the result by the 1393 * replacement string. The replacement string may contain references to 1394 * capture groups. 1395 * 1396 * @param replacement a string containing the replacement text. 1397 * @param dest a mutable UText in which the results are placed. 1398 * If NULL, a new UText will be created (which may not be mutable). 1399 * @param status a reference to a UErrorCode to receive any errors. 1400 * @return a string containing the results of the find and replace. 1401 * If a pre-allocated UText was provided, it will always be used and returned. 1402 * 1403 * @stable ICU 4.6 1404 */ 1405 virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status); 1406 1407 1408 /** 1409 * Replaces the first substring of the input that matches 1410 * the pattern with the replacement string. This is a convenience 1411 * function that provides a complete find-and-replace operation. 1412 * 1413 * <p>This function first resets this RegexMatcher. It then scans the input string 1414 * looking for a match of the pattern. Input that is not part 1415 * of the match is appended directly to the result string; the match is replaced 1416 * in the result by the replacement string. The replacement string may contain 1417 * references to captured groups.</p> 1418 * 1419 * <p>The state of the matcher (the position at which a subsequent find() 1420 * would begin) after completing a replaceFirst() is not specified. The 1421 * RegexMatcher should be reset before doing additional find() operations.</p> 1422 * 1423 * @param replacement a string containing the replacement text. 1424 * @param status a reference to a UErrorCode to receive any errors. 1425 * @return a string containing the results of the find and replace. 1426 * @stable ICU 2.4 1427 */ 1428 virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status); 1429 1430 1431 /** 1432 * Replaces the first substring of the input that matches 1433 * the pattern with the replacement string. This is a convenience 1434 * function that provides a complete find-and-replace operation. 1435 * 1436 * <p>This function first resets this RegexMatcher. It then scans the input string 1437 * looking for a match of the pattern. Input that is not part 1438 * of the match is appended directly to the result string; the match is replaced 1439 * in the result by the replacement string. The replacement string may contain 1440 * references to captured groups.</p> 1441 * 1442 * <p>The state of the matcher (the position at which a subsequent find() 1443 * would begin) after completing a replaceFirst() is not specified. The 1444 * RegexMatcher should be reset before doing additional find() operations.</p> 1445 * 1446 * @param replacement a string containing the replacement text. 1447 * @param dest a mutable UText in which the results are placed. 1448 * If NULL, a new UText will be created (which may not be mutable). 1449 * @param status a reference to a UErrorCode to receive any errors. 1450 * @return a string containing the results of the find and replace. 1451 * If a pre-allocated UText was provided, it will always be used and returned. 1452 * 1453 * @stable ICU 4.6 1454 */ 1455 virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status); 1456 1457 1458 /** 1459 * Implements a replace operation intended to be used as part of an 1460 * incremental find-and-replace. 1461 * 1462 * <p>The input string, starting from the end of the previous replacement and ending at 1463 * the start of the current match, is appended to the destination string. Then the 1464 * replacement string is appended to the output string, 1465 * including handling any substitutions of captured text.</p> 1466 * 1467 * <p>For simple, prepackaged, non-incremental find-and-replace 1468 * operations, see replaceFirst() or replaceAll().</p> 1469 * 1470 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1471 * @param replacement A UnicodeString that provides the text to be substituted for 1472 * the input text that matched the regexp pattern. The replacement 1473 * text may contain references to captured text from the 1474 * input. 1475 * @param status A reference to a UErrorCode to receive any errors. Possible 1476 * errors are U_REGEX_INVALID_STATE if no match has been 1477 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1478 * if the replacement text specifies a capture group that 1479 * does not exist in the pattern. 1480 * 1481 * @return this RegexMatcher 1482 * @stable ICU 2.4 1483 * 1484 */ 1485 virtual RegexMatcher &appendReplacement(UnicodeString &dest, 1486 const UnicodeString &replacement, UErrorCode &status); 1487 1488 1489 /** 1490 * Implements a replace operation intended to be used as part of an 1491 * incremental find-and-replace. 1492 * 1493 * <p>The input string, starting from the end of the previous replacement and ending at 1494 * the start of the current match, is appended to the destination string. Then the 1495 * replacement string is appended to the output string, 1496 * including handling any substitutions of captured text.</p> 1497 * 1498 * <p>For simple, prepackaged, non-incremental find-and-replace 1499 * operations, see replaceFirst() or replaceAll().</p> 1500 * 1501 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1502 * Must not be NULL. 1503 * @param replacement A UText that provides the text to be substituted for 1504 * the input text that matched the regexp pattern. The replacement 1505 * text may contain references to captured text from the input. 1506 * @param status A reference to a UErrorCode to receive any errors. Possible 1507 * errors are U_REGEX_INVALID_STATE if no match has been 1508 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1509 * if the replacement text specifies a capture group that 1510 * does not exist in the pattern. 1511 * 1512 * @return this RegexMatcher 1513 * 1514 * @stable ICU 4.6 1515 */ 1516 virtual RegexMatcher &appendReplacement(UText *dest, 1517 UText *replacement, UErrorCode &status); 1518 1519 1520 /** 1521 * As the final step in a find-and-replace operation, append the remainder 1522 * of the input string, starting at the position following the last appendReplacement(), 1523 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1524 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1525 * 1526 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1527 * @return the destination string. 1528 * @stable ICU 2.4 1529 */ 1530 virtual UnicodeString &appendTail(UnicodeString &dest); 1531 1532 1533 /** 1534 * As the final step in a find-and-replace operation, append the remainder 1535 * of the input string, starting at the position following the last appendReplacement(), 1536 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1537 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1538 * 1539 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1540 * Must not be NULL. 1541 * @param status error cod 1542 * @return the destination string. 1543 * 1544 * @stable ICU 4.6 1545 */ 1546 virtual UText *appendTail(UText *dest, UErrorCode &status); 1547 1548 1549 /** 1550 * Split a string into fields. Somewhat like split() from Perl. 1551 * The pattern matches identify delimiters that separate the input 1552 * into fields. The input data between the matches becomes the 1553 * fields themselves. 1554 * 1555 * @param input The string to be split into fields. The field delimiters 1556 * match the pattern (in the "this" object). This matcher 1557 * will be reset to this input string. 1558 * @param dest An array of UnicodeStrings to receive the results of the split. 1559 * This is an array of actual UnicodeString objects, not an 1560 * array of pointers to strings. Local (stack based) arrays can 1561 * work well here. 1562 * @param destCapacity The number of elements in the destination array. 1563 * If the number of fields found is less than destCapacity, the 1564 * extra strings in the destination array are not altered. 1565 * If the number of destination strings is less than the number 1566 * of fields, the trailing part of the input string, including any 1567 * field delimiters, is placed in the last destination string. 1568 * @param status A reference to a UErrorCode to receive any errors. 1569 * @return The number of fields into which the input string was split. 1570 * @stable ICU 2.6 1571 */ 1572 virtual int32_t split(const UnicodeString &input, 1573 UnicodeString dest[], 1574 int32_t destCapacity, 1575 UErrorCode &status); 1576 1577 1578 /** 1579 * Split a string into fields. Somewhat like split() from Perl. 1580 * The pattern matches identify delimiters that separate the input 1581 * into fields. The input data between the matches becomes the 1582 * fields themselves. 1583 * 1584 * @param input The string to be split into fields. The field delimiters 1585 * match the pattern (in the "this" object). This matcher 1586 * will be reset to this input string. 1587 * @param dest An array of mutable UText structs to receive the results of the split. 1588 * If a field is NULL, a new UText is allocated to contain the results for 1589 * that field. This new UText is not guaranteed to be mutable. 1590 * @param destCapacity The number of elements in the destination array. 1591 * If the number of fields found is less than destCapacity, the 1592 * extra strings in the destination array are not altered. 1593 * If the number of destination strings is less than the number 1594 * of fields, the trailing part of the input string, including any 1595 * field delimiters, is placed in the last destination string. 1596 * @param status A reference to a UErrorCode to receive any errors. 1597 * @return The number of fields into which the input string was split. 1598 * 1599 * @stable ICU 4.6 1600 */ 1601 virtual int32_t split(UText *input, 1602 UText *dest[], 1603 int32_t destCapacity, 1604 UErrorCode &status); 1605 1606 /** 1607 * Set a processing time limit for match operations with this Matcher. 1608 * 1609 * Some patterns, when matching certain strings, can run in exponential time. 1610 * For practical purposes, the match operation may appear to be in an 1611 * infinite loop. 1612 * When a limit is set a match operation will fail with an error if the 1613 * limit is exceeded. 1614 * <p> 1615 * The units of the limit are steps of the match engine. 1616 * Correspondence with actual processor time will depend on the speed 1617 * of the processor and the details of the specific pattern, but will 1618 * typically be on the order of milliseconds. 1619 * <p> 1620 * By default, the matching time is not limited. 1621 * <p> 1622 * 1623 * @param limit The limit value, or 0 for no limit. 1624 * @param status A reference to a UErrorCode to receive any errors. 1625 * @stable ICU 4.0 1626 */ 1627 virtual void setTimeLimit(int32_t limit, UErrorCode &status); 1628 1629 /** 1630 * Get the time limit, if any, for match operations made with this Matcher. 1631 * 1632 * @return the maximum allowed time for a match, in units of processing steps. 1633 * @stable ICU 4.0 1634 */ 1635 virtual int32_t getTimeLimit() const; 1636 1637 /** 1638 * Set the amount of heap storage available for use by the match backtracking stack. 1639 * The matcher is also reset, discarding any results from previous matches. 1640 * <p> 1641 * ICU uses a backtracking regular expression engine, with the backtrack stack 1642 * maintained on the heap. This function sets the limit to the amount of memory 1643 * that can be used for this purpose. A backtracking stack overflow will 1644 * result in an error from the match operation that caused it. 1645 * <p> 1646 * A limit is desirable because a malicious or poorly designed pattern can use 1647 * excessive memory, potentially crashing the process. A limit is enabled 1648 * by default. 1649 * <p> 1650 * @param limit The maximum size, in bytes, of the matching backtrack stack. 1651 * A value of zero means no limit. 1652 * The limit must be greater or equal to zero. 1653 * 1654 * @param status A reference to a UErrorCode to receive any errors. 1655 * 1656 * @stable ICU 4.0 1657 */ 1658 virtual void setStackLimit(int32_t limit, UErrorCode &status); 1659 1660 /** 1661 * Get the size of the heap storage available for use by the back tracking stack. 1662 * 1663 * @return the maximum backtracking stack size, in bytes, or zero if the 1664 * stack size is unlimited. 1665 * @stable ICU 4.0 1666 */ 1667 virtual int32_t getStackLimit() const; 1668 1669 1670 /** 1671 * Set a callback function for use with this Matcher. 1672 * During matching operations the function will be called periodically, 1673 * giving the application the opportunity to terminate a long-running 1674 * match. 1675 * 1676 * @param callback A pointer to the user-supplied callback function. 1677 * @param context User context pointer. The value supplied at the 1678 * time the callback function is set will be saved 1679 * and passed to the callback each time that it is called. 1680 * @param status A reference to a UErrorCode to receive any errors. 1681 * @stable ICU 4.0 1682 */ 1683 virtual void setMatchCallback(URegexMatchCallback *callback, 1684 const void *context, 1685 UErrorCode &status); 1686 1687 1688 /** 1689 * Get the callback function for this URegularExpression. 1690 * 1691 * @param callback Out parameter, receives a pointer to the user-supplied 1692 * callback function. 1693 * @param context Out parameter, receives the user context pointer that 1694 * was set when uregex_setMatchCallback() was called. 1695 * @param status A reference to a UErrorCode to receive any errors. 1696 * @stable ICU 4.0 1697 */ 1698 virtual void getMatchCallback(URegexMatchCallback *&callback, 1699 const void *&context, 1700 UErrorCode &status); 1701 1702 1703 /** 1704 * Set a progress callback function for use with find operations on this Matcher. 1705 * During find operations, the callback will be invoked after each return from a 1706 * match attempt, giving the application the opportunity to terminate a long-running 1707 * find operation. 1708 * 1709 * @param callback A pointer to the user-supplied callback function. 1710 * @param context User context pointer. The value supplied at the 1711 * time the callback function is set will be saved 1712 * and passed to the callback each time that it is called. 1713 * @param status A reference to a UErrorCode to receive any errors. 1714 * @stable ICU 4.6 1715 */ 1716 virtual void setFindProgressCallback(URegexFindProgressCallback *callback, 1717 const void *context, 1718 UErrorCode &status); 1719 1720 1721 /** 1722 * Get the find progress callback function for this URegularExpression. 1723 * 1724 * @param callback Out parameter, receives a pointer to the user-supplied 1725 * callback function. 1726 * @param context Out parameter, receives the user context pointer that 1727 * was set when uregex_setFindProgressCallback() was called. 1728 * @param status A reference to a UErrorCode to receive any errors. 1729 * @stable ICU 4.6 1730 */ 1731 virtual void getFindProgressCallback(URegexFindProgressCallback *&callback, 1732 const void *&context, 1733 UErrorCode &status); 1734 1735 #ifndef U_HIDE_INTERNAL_API 1736 /** 1737 * setTrace Debug function, enable/disable tracing of the matching engine. 1738 * For internal ICU development use only. DO NO USE!!!! 1739 * @internal 1740 */ 1741 void setTrace(UBool state); 1742 #endif /* U_HIDE_INTERNAL_API */ 1743 1744 /** 1745 * ICU "poor man's RTTI", returns a UClassID for this class. 1746 * 1747 * @stable ICU 2.2 1748 */ 1749 static UClassID U_EXPORT2 getStaticClassID(); 1750 1751 /** 1752 * ICU "poor man's RTTI", returns a UClassID for the actual class. 1753 * 1754 * @stable ICU 2.2 1755 */ 1756 virtual UClassID getDynamicClassID() const; 1757 1758 private: 1759 // Constructors and other object boilerplate are private. 1760 // Instances of RegexMatcher can not be assigned, copied, cloned, etc. 1761 RegexMatcher(); // default constructor not implemented 1762 RegexMatcher(const RegexPattern *pat); 1763 RegexMatcher(const RegexMatcher &other); 1764 RegexMatcher &operator =(const RegexMatcher &rhs); 1765 void init(UErrorCode &status); // Common initialization 1766 void init2(UText *t, UErrorCode &e); // Common initialization, part 2. 1767 1768 friend class RegexPattern; 1769 friend class RegexCImpl; 1770 public: 1771 #ifndef U_HIDE_INTERNAL_API 1772 /** @internal */ 1773 void resetPreserveRegion(); // Reset matcher state, but preserve any region. 1774 #endif /* U_HIDE_INTERNAL_API */ 1775 private: 1776 1777 // 1778 // MatchAt This is the internal interface to the match engine itself. 1779 // Match status comes back in matcher member variables. 1780 // 1781 void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status); 1782 inline void backTrack(int64_t &inputIdx, int32_t &patIdx); 1783 UBool isWordBoundary(int64_t pos); // perform Perl-like \b test 1784 UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test 1785 REStackFrame *resetStack(); 1786 inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status); 1787 void IncrementTime(UErrorCode &status); 1788 1789 // Call user find callback function, if set. Return TRUE if operation should be interrupted. 1790 inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status); 1791 1792 int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const; 1793 1794 UBool findUsingChunk(UErrorCode &status); 1795 void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status); 1796 UBool isChunkWordBoundary(int32_t pos); 1797 1798 const RegexPattern *fPattern; 1799 RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and 1800 // should delete it when through. 1801 1802 const UnicodeString *fInput; // The string being matched. Only used for input() 1803 UText *fInputText; // The text being matched. Is never NULL. 1804 UText *fAltInputText; // A shallow copy of the text being matched. 1805 // Only created if the pattern contains backreferences. 1806 int64_t fInputLength; // Full length of the input text. 1807 int32_t fFrameSize; // The size of a frame in the backtrack stack. 1808 1809 int64_t fRegionStart; // Start of the input region, default = 0. 1810 int64_t fRegionLimit; // End of input region, default to input.length. 1811 1812 int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $). 1813 int64_t fAnchorLimit; // See useAnchoringBounds 1814 1815 int64_t fLookStart; // Region bounds for look-ahead/behind and 1816 int64_t fLookLimit; // and other boundary tests. See 1817 // useTransparentBounds 1818 1819 int64_t fActiveStart; // Currently active bounds for matching. 1820 int64_t fActiveLimit; // Usually is the same as region, but 1821 // is changed to fLookStart/Limit when 1822 // entering look around regions. 1823 1824 UBool fTransparentBounds; // True if using transparent bounds. 1825 UBool fAnchoringBounds; // True if using anchoring bounds. 1826 1827 UBool fMatch; // True if the last attempted match was successful. 1828 int64_t fMatchStart; // Position of the start of the most recent match 1829 int64_t fMatchEnd; // First position after the end of the most recent match 1830 // Zero if no previous match, even when a region 1831 // is active. 1832 int64_t fLastMatchEnd; // First position after the end of the previous match, 1833 // or -1 if there was no previous match. 1834 int64_t fAppendPosition; // First position after the end of the previous 1835 // appendReplacement(). As described by the 1836 // JavaDoc for Java Matcher, where it is called 1837 // "append position" 1838 UBool fHitEnd; // True if the last match touched the end of input. 1839 UBool fRequireEnd; // True if the last match required end-of-input 1840 // (matched $ or Z) 1841 1842 UVector64 *fStack; 1843 REStackFrame *fFrame; // After finding a match, the last active stack frame, 1844 // which will contain the capture group results. 1845 // NOT valid while match engine is running. 1846 1847 int64_t *fData; // Data area for use by the compiled pattern. 1848 int64_t fSmallData[8]; // Use this for data if it's enough. 1849 1850 int32_t fTimeLimit; // Max time (in arbitrary steps) to let the 1851 // match engine run. Zero for unlimited. 1852 1853 int32_t fTime; // Match time, accumulates while matching. 1854 int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves. 1855 // Kept separately from fTime to keep as much 1856 // code as possible out of the inline 1857 // StateSave function. 1858 1859 int32_t fStackLimit; // Maximum memory size to use for the backtrack 1860 // stack, in bytes. Zero for unlimited. 1861 1862 URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct. 1863 // NULL if there is no callback. 1864 const void *fCallbackContext; // User Context ptr for callback function. 1865 1866 URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct. 1867 // NULL if there is no callback. 1868 const void *fFindProgressCallbackContext; // User Context ptr for callback function. 1869 1870 1871 UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility. 1872 1873 UBool fTraceDebug; // Set true for debug tracing of match engine. 1874 1875 UErrorCode fDeferredStatus; // Save error state that cannot be immediately 1876 // reported, or that permanently disables this matcher. 1877 1878 RuleBasedBreakIterator *fWordBreakItr; 1879 }; 1880 1881 U_NAMESPACE_END 1882 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS 1883 #endif 1884