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