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 * @param groupNum the capture group number 885 * @param status A reference to a UErrorCode to receive any errors. 886 * Possible errors are U_REGEX_INVALID_STATE if no match 887 * has been attempted or the last match failed and 888 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 889 * @return the captured text 890 * @stable ICU 2.4 891 */ 892 virtual UnicodeString group(int32_t groupNum, UErrorCode &status) const; 893 894 /** 895 * Returns the number of capturing groups in this matcher's pattern. 896 * @return the number of capture groups 897 * @stable ICU 2.4 898 */ 899 virtual int32_t groupCount() const; 900 901 902 /** 903 * Returns a shallow clone of the entire live input string with the UText current native index 904 * set to the beginning of the requested group. 905 * 906 * @param dest The UText into which the input should be cloned, or NULL to create a new UText 907 * @param group_len A reference to receive the length of the desired capture group 908 * @param status A reference to a UErrorCode to receive any errors. 909 * Possible errors are U_REGEX_INVALID_STATE if no match 910 * has been attempted or the last match failed and 911 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 912 * @return dest if non-NULL, a shallow copy of the input text otherwise 913 * 914 * @stable ICU 4.6 915 */ 916 virtual UText *group(UText *dest, int64_t &group_len, UErrorCode &status) const; 917 918 /** 919 * Returns a shallow clone of the entire live input string with the UText current native index 920 * set to the beginning of the requested group. 921 * 922 * @param groupNum The capture group number. 923 * @param dest The UText into which the input should be cloned, or NULL to create a new UText. 924 * @param group_len A reference to receive the length of the desired capture group 925 * @param status A reference to a UErrorCode to receive any errors. 926 * Possible errors are U_REGEX_INVALID_STATE if no match 927 * has been attempted or the last match failed and 928 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 929 * @return dest if non-NULL, a shallow copy of the input text otherwise 930 * 931 * @stable ICU 4.6 932 */ 933 virtual UText *group(int32_t groupNum, UText *dest, int64_t &group_len, UErrorCode &status) const; 934 935 /** 936 * Returns the index in the input string of the start of the text matched 937 * during the previous match operation. 938 * @param status a reference to a UErrorCode to receive any errors. 939 * @return The (native) position in the input string of the start of the last match. 940 * @stable ICU 2.4 941 */ 942 virtual int32_t start(UErrorCode &status) const; 943 944 /** 945 * Returns the index in the input string of the start of the text matched 946 * during the previous match operation. 947 * @param status a reference to a UErrorCode to receive any errors. 948 * @return The (native) position in the input string of the start of the last match. 949 * @stable ICU 4.6 950 */ 951 virtual int64_t start64(UErrorCode &status) const; 952 953 954 /** 955 * Returns the index in the input string of the start of the text matched by the 956 * specified capture group during the previous match operation. Return -1 if 957 * the capture group exists in the pattern, but was not part of the last match. 958 * 959 * @param group the capture group number 960 * @param status A reference to a UErrorCode to receive any errors. Possible 961 * errors are U_REGEX_INVALID_STATE if no match has been 962 * attempted or the last match failed, and 963 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 964 * @return the (native) start position of substring matched by the specified group. 965 * @stable ICU 2.4 966 */ 967 virtual int32_t start(int32_t group, UErrorCode &status) const; 968 969 /** 970 * Returns the index in the input string of the start of the text matched by the 971 * specified capture group during the previous match operation. Return -1 if 972 * the capture group exists in the pattern, but was not part of the last match. 973 * 974 * @param group the capture group number. 975 * @param status A reference to a UErrorCode to receive any errors. Possible 976 * errors are U_REGEX_INVALID_STATE if no match has been 977 * attempted or the last match failed, and 978 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number. 979 * @return the (native) start position of substring matched by the specified group. 980 * @stable ICU 4.6 981 */ 982 virtual int64_t start64(int32_t group, UErrorCode &status) const; 983 984 /** 985 * Returns the index in the input string of the first character following the 986 * text matched during the previous match operation. 987 * 988 * @param status A reference to a UErrorCode to receive any errors. Possible 989 * errors are U_REGEX_INVALID_STATE if no match has been 990 * attempted or the last match failed. 991 * @return the index of the last character matched, plus one. 992 * The index value returned is a native index, corresponding to 993 * code units for the underlying encoding type, for example, 994 * a byte index for UTF-8. 995 * @stable ICU 2.4 996 */ 997 virtual int32_t end(UErrorCode &status) const; 998 999 /** 1000 * Returns the index in the input string of the first character following the 1001 * text matched during the previous match operation. 1002 * 1003 * @param status A reference to a UErrorCode to receive any errors. Possible 1004 * errors are U_REGEX_INVALID_STATE if no match has been 1005 * attempted or the last match failed. 1006 * @return the index of the last character matched, plus one. 1007 * The index value returned is a native index, corresponding to 1008 * code units for the underlying encoding type, for example, 1009 * a byte index for UTF-8. 1010 * @stable ICU 4.6 1011 */ 1012 virtual int64_t end64(UErrorCode &status) const; 1013 1014 1015 /** 1016 * Returns the index in the input string of the character following the 1017 * text matched by the specified capture group during the previous match operation. 1018 * 1019 * @param group the capture group number 1020 * @param status A reference to a UErrorCode to receive any errors. Possible 1021 * errors are U_REGEX_INVALID_STATE if no match has been 1022 * attempted or the last match failed and 1023 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 1024 * @return the index of the first character following the text 1025 * captured by the specified group during the previous match operation. 1026 * Return -1 if the capture group exists in the pattern but was not part of the match. 1027 * The index value returned is a native index, corresponding to 1028 * code units for the underlying encoding type, for example, 1029 * a byte index for UTF8. 1030 * @stable ICU 2.4 1031 */ 1032 virtual int32_t end(int32_t group, UErrorCode &status) const; 1033 1034 /** 1035 * Returns the index in the input string of the character following the 1036 * text matched by the specified capture group during the previous match operation. 1037 * 1038 * @param group the capture group number 1039 * @param status A reference to a UErrorCode to receive any errors. Possible 1040 * errors are U_REGEX_INVALID_STATE if no match has been 1041 * attempted or the last match failed and 1042 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number 1043 * @return the index of the first character following the text 1044 * captured by the specified group during the previous match operation. 1045 * Return -1 if the capture group exists in the pattern but was not part of the match. 1046 * The index value returned is a native index, corresponding to 1047 * code units for the underlying encoding type, for example, 1048 * a byte index for UTF8. 1049 * @stable ICU 4.6 1050 */ 1051 virtual int64_t end64(int32_t group, UErrorCode &status) const; 1052 1053 /** 1054 * Resets this matcher. The effect is to remove any memory of previous matches, 1055 * and to cause subsequent find() operations to begin at the beginning of 1056 * the input string. 1057 * 1058 * @return this RegexMatcher. 1059 * @stable ICU 2.4 1060 */ 1061 virtual RegexMatcher &reset(); 1062 1063 1064 /** 1065 * Resets this matcher, and set the current input position. 1066 * The effect is to remove any memory of previous matches, 1067 * and to cause subsequent find() operations to begin at 1068 * the specified (native) position in the input string. 1069 * <p> 1070 * The matcher's region is reset to its default, which is the entire 1071 * input string. 1072 * <p> 1073 * An alternative to this function is to set a match region 1074 * beginning at the desired index. 1075 * 1076 * @return this RegexMatcher. 1077 * @stable ICU 2.8 1078 */ 1079 virtual RegexMatcher &reset(int64_t index, UErrorCode &status); 1080 1081 1082 /** 1083 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1084 * to be reused, which is more efficient than creating a new RegexMatcher for 1085 * each input string to be processed. 1086 * @param input The new string on which subsequent pattern matches will operate. 1087 * The matcher retains a reference to the callers string, and operates 1088 * directly on that. Ownership of the string remains with the caller. 1089 * Because no copy of the string is made, it is essential that the 1090 * caller not delete the string until after regexp operations on it 1091 * are done. 1092 * Note that while a reset on the matcher with an input string that is then 1093 * modified across/during matcher operations may be supported currently for UnicodeString, 1094 * this was not originally intended behavior, and support for this is not guaranteed 1095 * in upcoming versions of ICU. 1096 * @return this RegexMatcher. 1097 * @stable ICU 2.4 1098 */ 1099 virtual RegexMatcher &reset(const UnicodeString &input); 1100 1101 1102 /** 1103 * Resets this matcher with a new input string. This allows instances of RegexMatcher 1104 * to be reused, which is more efficient than creating a new RegexMatcher for 1105 * each input string to be processed. 1106 * @param input The new string on which subsequent pattern matches will operate. 1107 * The matcher makes a shallow clone of the given text; ownership of the 1108 * original string remains with the caller. Because no deep copy of the 1109 * text is made, it is essential that the caller not modify the string 1110 * until after regexp operations on it are done. 1111 * @return this RegexMatcher. 1112 * 1113 * @stable ICU 4.6 1114 */ 1115 virtual RegexMatcher &reset(UText *input); 1116 1117 1118 /** 1119 * Set the subject text string upon which the regular expression is looking for matches 1120 * without changing any other aspect of the matching state. 1121 * The new and previous text strings must have the same content. 1122 * 1123 * This function is intended for use in environments where ICU is operating on 1124 * strings that may move around in memory. It provides a mechanism for notifying 1125 * ICU that the string has been relocated, and providing a new UText to access the 1126 * string in its new position. 1127 * 1128 * Note that the regular expression implementation never copies the underlying text 1129 * of a string being matched, but always operates directly on the original text 1130 * provided by the user. Refreshing simply drops the references to the old text 1131 * and replaces them with references to the new. 1132 * 1133 * Caution: this function is normally used only by very specialized, 1134 * system-level code. One example use case is with garbage collection that moves 1135 * the text in memory. 1136 * 1137 * @param input The new (moved) text string. 1138 * @param status Receives errors detected by this function. 1139 * 1140 * @stable ICU 4.8 1141 */ 1142 virtual RegexMatcher &refreshInputText(UText *input, UErrorCode &status); 1143 1144 private: 1145 /** 1146 * Cause a compilation error if an application accidentally attempts to 1147 * reset a matcher with a (UChar *) string as input rather than 1148 * a UnicodeString. Avoids a dangling reference to a temporary string. 1149 * <p> 1150 * To efficiently work with UChar *strings, wrap the data in a UnicodeString 1151 * using one of the aliasing constructors, such as 1152 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code> 1153 * or in a UText, using 1154 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code> 1155 * 1156 */ 1157 RegexMatcher &reset(const UChar *input); 1158 public: 1159 1160 /** 1161 * Returns the input string being matched. Ownership of the string belongs to 1162 * the matcher; it should not be altered or deleted. This method will work even if the input 1163 * was originally supplied as a UText. 1164 * @return the input string 1165 * @stable ICU 2.4 1166 */ 1167 virtual const UnicodeString &input() const; 1168 1169 /** 1170 * Returns the input string being matched. This is the live input text; it should not be 1171 * altered or deleted. This method will work even if the input was originally supplied as 1172 * a UnicodeString. 1173 * @return the input text 1174 * 1175 * @stable ICU 4.6 1176 */ 1177 virtual UText *inputText() const; 1178 1179 /** 1180 * Returns the input string being matched, either by copying it into the provided 1181 * UText parameter or by returning a shallow clone of the live input. Note that copying 1182 * the entire input may cause significant performance and memory issues. 1183 * @param dest The UText into which the input should be copied, or NULL to create a new UText 1184 * @param status error code 1185 * @return dest if non-NULL, a shallow copy of the input text otherwise 1186 * 1187 * @stable ICU 4.6 1188 */ 1189 virtual UText *getInput(UText *dest, UErrorCode &status) const; 1190 1191 1192 /** Sets the limits of this matcher's region. 1193 * The region is the part of the input string that will be searched to find a match. 1194 * Invoking this method resets the matcher, and then sets the region to start 1195 * at the index specified by the start parameter and end at the index specified 1196 * by the end parameter. 1197 * 1198 * Depending on the transparency and anchoring being used (see useTransparentBounds 1199 * and useAnchoringBounds), certain constructs such as anchors may behave differently 1200 * at or around the boundaries of the region 1201 * 1202 * The function will fail if start is greater than limit, or if either index 1203 * is less than zero or greater than the length of the string being matched. 1204 * 1205 * @param start The (native) index to begin searches at. 1206 * @param limit The index to end searches at (exclusive). 1207 * @param status A reference to a UErrorCode to receive any errors. 1208 * @stable ICU 4.0 1209 */ 1210 virtual RegexMatcher ®ion(int64_t start, int64_t limit, UErrorCode &status); 1211 1212 /** 1213 * Identical to region(start, limit, status) but also allows a start position without 1214 * resetting the region state. 1215 * @param regionStart The region start 1216 * @param regionLimit the limit of the region 1217 * @param startIndex The (native) index within the region bounds at which to begin searches. 1218 * @param status A reference to a UErrorCode to receive any errors. 1219 * If startIndex is not within the specified region bounds, 1220 * U_INDEX_OUTOFBOUNDS_ERROR is returned. 1221 * @stable ICU 4.6 1222 */ 1223 virtual RegexMatcher ®ion(int64_t regionStart, int64_t regionLimit, int64_t startIndex, UErrorCode &status); 1224 1225 /** 1226 * Reports the start index of this matcher's region. The searches this matcher 1227 * conducts are limited to finding matches within regionStart (inclusive) and 1228 * regionEnd (exclusive). 1229 * 1230 * @return The starting (native) index of this matcher's region. 1231 * @stable ICU 4.0 1232 */ 1233 virtual int32_t regionStart() const; 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.6 1242 */ 1243 virtual int64_t regionStart64() const; 1244 1245 1246 /** 1247 * Reports the end (limit) index (exclusive) of this matcher's region. The searches 1248 * this matcher conducts are limited to finding matches within regionStart 1249 * (inclusive) and regionEnd (exclusive). 1250 * 1251 * @return The ending point (native) of this matcher's region. 1252 * @stable ICU 4.0 1253 */ 1254 virtual int32_t regionEnd() const; 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.6 1263 */ 1264 virtual int64_t regionEnd64() const; 1265 1266 /** 1267 * Queries the transparency of region bounds for this matcher. 1268 * See useTransparentBounds for a description of transparent and opaque bounds. 1269 * By default, a matcher uses opaque region boundaries. 1270 * 1271 * @return TRUE if this matcher is using opaque bounds, false if it is not. 1272 * @stable ICU 4.0 1273 */ 1274 virtual UBool hasTransparentBounds() const; 1275 1276 /** 1277 * Sets the transparency of region bounds for this matcher. 1278 * Invoking this function with an argument of true will set this matcher to use transparent bounds. 1279 * If the boolean argument is false, then opaque bounds will be used. 1280 * 1281 * Using transparent bounds, the boundaries of this matcher's region are transparent 1282 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can 1283 * see text beyond the boundaries of the region while checking for a match. 1284 * 1285 * With opaque bounds, no text outside of the matcher's region is visible to lookahead, 1286 * lookbehind, and boundary matching constructs. 1287 * 1288 * By default, a matcher uses opaque bounds. 1289 * 1290 * @param b TRUE for transparent bounds; FALSE for opaque bounds 1291 * @return This Matcher; 1292 * @stable ICU 4.0 1293 **/ 1294 virtual RegexMatcher &useTransparentBounds(UBool b); 1295 1296 1297 /** 1298 * Return true if this matcher is using anchoring bounds. 1299 * By default, matchers use anchoring region bounds. 1300 * 1301 * @return TRUE if this matcher is using anchoring bounds. 1302 * @stable ICU 4.0 1303 */ 1304 virtual UBool hasAnchoringBounds() const; 1305 1306 1307 /** 1308 * Set whether this matcher is using Anchoring Bounds for its region. 1309 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start 1310 * and end of the region. Without Anchoring Bounds, anchors will only match at 1311 * the positions they would in the complete text. 1312 * 1313 * Anchoring Bounds are the default for regions. 1314 * 1315 * @param b TRUE if to enable anchoring bounds; FALSE to disable them. 1316 * @return This Matcher 1317 * @stable ICU 4.0 1318 */ 1319 virtual RegexMatcher &useAnchoringBounds(UBool b); 1320 1321 1322 /** 1323 * Return TRUE if the most recent matching operation attempted to access 1324 * additional input beyond the available input text. 1325 * In this case, additional input text could change the results of the match. 1326 * 1327 * hitEnd() is defined for both successful and unsuccessful matches. 1328 * In either case hitEnd() will return TRUE if if the end of the text was 1329 * reached at any point during the matching process. 1330 * 1331 * @return TRUE if the most recent match hit the end of input 1332 * @stable ICU 4.0 1333 */ 1334 virtual UBool hitEnd() const; 1335 1336 /** 1337 * Return TRUE the most recent match succeeded and additional input could cause 1338 * it to fail. If this method returns false and a match was found, then more input 1339 * might change the match but the match won't be lost. If a match was not found, 1340 * then requireEnd has no meaning. 1341 * 1342 * @return TRUE if more input could cause the most recent match to no longer match. 1343 * @stable ICU 4.0 1344 */ 1345 virtual UBool requireEnd() const; 1346 1347 1348 /** 1349 * Returns the pattern that is interpreted by this matcher. 1350 * @return the RegexPattern for this RegexMatcher 1351 * @stable ICU 2.4 1352 */ 1353 virtual const RegexPattern &pattern() const; 1354 1355 1356 /** 1357 * Replaces every substring of the input that matches the pattern 1358 * with the given replacement string. This is a convenience function that 1359 * provides a complete find-and-replace-all operation. 1360 * 1361 * This method first resets this matcher. It then scans the input string 1362 * looking for matches of the pattern. Input that is not part of any 1363 * match is left unchanged; each match is replaced in the result by the 1364 * replacement string. The replacement string may contain references to 1365 * capture groups. 1366 * 1367 * @param replacement a string containing the replacement text. 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 * @stable ICU 2.4 1371 */ 1372 virtual UnicodeString replaceAll(const UnicodeString &replacement, UErrorCode &status); 1373 1374 1375 /** 1376 * Replaces every substring of the input that matches the pattern 1377 * with the given replacement string. This is a convenience function that 1378 * provides a complete find-and-replace-all operation. 1379 * 1380 * This method first resets this matcher. It then scans the input string 1381 * looking for matches of the pattern. Input that is not part of any 1382 * match is left unchanged; each match is replaced in the result by the 1383 * replacement string. The replacement string may contain references to 1384 * capture groups. 1385 * 1386 * @param replacement a string containing the replacement text. 1387 * @param dest a mutable UText in which the results are placed. 1388 * If NULL, a new UText will be created (which may not be mutable). 1389 * @param status a reference to a UErrorCode to receive any errors. 1390 * @return a string containing the results of the find and replace. 1391 * If a pre-allocated UText was provided, it will always be used and returned. 1392 * 1393 * @stable ICU 4.6 1394 */ 1395 virtual UText *replaceAll(UText *replacement, UText *dest, UErrorCode &status); 1396 1397 1398 /** 1399 * Replaces the first substring of the input that matches 1400 * the pattern with the replacement string. This is a convenience 1401 * function that provides a complete find-and-replace operation. 1402 * 1403 * <p>This function first resets this RegexMatcher. It then scans the input string 1404 * looking for a match of the pattern. Input that is not part 1405 * of the match is appended directly to the result string; the match is replaced 1406 * in the result by the replacement string. The replacement string may contain 1407 * references to captured groups.</p> 1408 * 1409 * <p>The state of the matcher (the position at which a subsequent find() 1410 * would begin) after completing a replaceFirst() is not specified. The 1411 * RegexMatcher should be reset before doing additional find() operations.</p> 1412 * 1413 * @param replacement a string containing the replacement text. 1414 * @param status a reference to a UErrorCode to receive any errors. 1415 * @return a string containing the results of the find and replace. 1416 * @stable ICU 2.4 1417 */ 1418 virtual UnicodeString replaceFirst(const UnicodeString &replacement, UErrorCode &status); 1419 1420 1421 /** 1422 * Replaces the first substring of the input that matches 1423 * the pattern with the replacement string. This is a convenience 1424 * function that provides a complete find-and-replace operation. 1425 * 1426 * <p>This function first resets this RegexMatcher. It then scans the input string 1427 * looking for a match of the pattern. Input that is not part 1428 * of the match is appended directly to the result string; the match is replaced 1429 * in the result by the replacement string. The replacement string may contain 1430 * references to captured groups.</p> 1431 * 1432 * <p>The state of the matcher (the position at which a subsequent find() 1433 * would begin) after completing a replaceFirst() is not specified. The 1434 * RegexMatcher should be reset before doing additional find() operations.</p> 1435 * 1436 * @param replacement a string containing the replacement text. 1437 * @param dest a mutable UText in which the results are placed. 1438 * If NULL, a new UText will be created (which may not be mutable). 1439 * @param status a reference to a UErrorCode to receive any errors. 1440 * @return a string containing the results of the find and replace. 1441 * If a pre-allocated UText was provided, it will always be used and returned. 1442 * 1443 * @stable ICU 4.6 1444 */ 1445 virtual UText *replaceFirst(UText *replacement, UText *dest, UErrorCode &status); 1446 1447 1448 /** 1449 * Implements a replace operation intended to be used as part of an 1450 * incremental find-and-replace. 1451 * 1452 * <p>The input string, starting from the end of the previous replacement and ending at 1453 * the start of the current match, is appended to the destination string. Then the 1454 * replacement string is appended to the output string, 1455 * including handling any substitutions of captured text.</p> 1456 * 1457 * <p>For simple, prepackaged, non-incremental find-and-replace 1458 * operations, see replaceFirst() or replaceAll().</p> 1459 * 1460 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1461 * @param replacement A UnicodeString that provides the text to be substituted for 1462 * the input text that matched the regexp pattern. The replacement 1463 * text may contain references to captured text from the 1464 * input. 1465 * @param status A reference to a UErrorCode to receive any errors. Possible 1466 * errors are U_REGEX_INVALID_STATE if no match has been 1467 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1468 * if the replacement text specifies a capture group that 1469 * does not exist in the pattern. 1470 * 1471 * @return this RegexMatcher 1472 * @stable ICU 2.4 1473 * 1474 */ 1475 virtual RegexMatcher &appendReplacement(UnicodeString &dest, 1476 const UnicodeString &replacement, UErrorCode &status); 1477 1478 1479 /** 1480 * Implements a replace operation intended to be used as part of an 1481 * incremental find-and-replace. 1482 * 1483 * <p>The input string, starting from the end of the previous replacement and ending at 1484 * the start of the current match, is appended to the destination string. Then the 1485 * replacement string is appended to the output string, 1486 * including handling any substitutions of captured text.</p> 1487 * 1488 * <p>For simple, prepackaged, non-incremental find-and-replace 1489 * operations, see replaceFirst() or replaceAll().</p> 1490 * 1491 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1492 * Must not be NULL. 1493 * @param replacement A UText that provides the text to be substituted for 1494 * the input text that matched the regexp pattern. The replacement 1495 * text may contain references to captured text from the input. 1496 * @param status A reference to a UErrorCode to receive any errors. Possible 1497 * errors are U_REGEX_INVALID_STATE if no match has been 1498 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR 1499 * if the replacement text specifies a capture group that 1500 * does not exist in the pattern. 1501 * 1502 * @return this RegexMatcher 1503 * 1504 * @stable ICU 4.6 1505 */ 1506 virtual RegexMatcher &appendReplacement(UText *dest, 1507 UText *replacement, UErrorCode &status); 1508 1509 1510 /** 1511 * As the final step in a find-and-replace operation, append the remainder 1512 * of the input string, starting at the position following the last appendReplacement(), 1513 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1514 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1515 * 1516 * @param dest A UnicodeString to which the results of the find-and-replace are appended. 1517 * @return the destination string. 1518 * @stable ICU 2.4 1519 */ 1520 virtual UnicodeString &appendTail(UnicodeString &dest); 1521 1522 1523 /** 1524 * As the final step in a find-and-replace operation, append the remainder 1525 * of the input string, starting at the position following the last appendReplacement(), 1526 * to the destination string. <code>appendTail()</code> is intended to be invoked after one 1527 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>. 1528 * 1529 * @param dest A mutable UText to which the results of the find-and-replace are appended. 1530 * Must not be NULL. 1531 * @param status error cod 1532 * @return the destination string. 1533 * 1534 * @stable ICU 4.6 1535 */ 1536 virtual UText *appendTail(UText *dest, UErrorCode &status); 1537 1538 1539 /** 1540 * Split a string into fields. Somewhat like split() from Perl. 1541 * The pattern matches identify delimiters that separate the input 1542 * into fields. The input data between the matches becomes the 1543 * fields themselves. 1544 * 1545 * @param input The string to be split into fields. The field delimiters 1546 * match the pattern (in the "this" object). This matcher 1547 * will be reset to this input string. 1548 * @param dest An array of UnicodeStrings to receive the results of the split. 1549 * This is an array of actual UnicodeString objects, not an 1550 * array of pointers to strings. Local (stack based) arrays can 1551 * work well here. 1552 * @param destCapacity The number of elements in the destination array. 1553 * If the number of fields found is less than destCapacity, the 1554 * extra strings in the destination array are not altered. 1555 * If the number of destination strings is less than the number 1556 * of fields, the trailing part of the input string, including any 1557 * field delimiters, is placed in the last destination string. 1558 * @param status A reference to a UErrorCode to receive any errors. 1559 * @return The number of fields into which the input string was split. 1560 * @stable ICU 2.6 1561 */ 1562 virtual int32_t split(const UnicodeString &input, 1563 UnicodeString dest[], 1564 int32_t destCapacity, 1565 UErrorCode &status); 1566 1567 1568 /** 1569 * Split a string into fields. Somewhat like split() from Perl. 1570 * The pattern matches identify delimiters that separate the input 1571 * into fields. The input data between the matches becomes the 1572 * fields themselves. 1573 * 1574 * @param input The string to be split into fields. The field delimiters 1575 * match the pattern (in the "this" object). This matcher 1576 * will be reset to this input string. 1577 * @param dest An array of mutable UText structs to receive the results of the split. 1578 * If a field is NULL, a new UText is allocated to contain the results for 1579 * that field. This new UText is not guaranteed to be mutable. 1580 * @param destCapacity The number of elements in the destination array. 1581 * If the number of fields found is less than destCapacity, the 1582 * extra strings in the destination array are not altered. 1583 * If the number of destination strings is less than the number 1584 * of fields, the trailing part of the input string, including any 1585 * field delimiters, is placed in the last destination string. 1586 * @param status A reference to a UErrorCode to receive any errors. 1587 * @return The number of fields into which the input string was split. 1588 * 1589 * @stable ICU 4.6 1590 */ 1591 virtual int32_t split(UText *input, 1592 UText *dest[], 1593 int32_t destCapacity, 1594 UErrorCode &status); 1595 1596 /** 1597 * Set a processing time limit for match operations with this Matcher. 1598 * 1599 * Some patterns, when matching certain strings, can run in exponential time. 1600 * For practical purposes, the match operation may appear to be in an 1601 * infinite loop. 1602 * When a limit is set a match operation will fail with an error if the 1603 * limit is exceeded. 1604 * <p> 1605 * The units of the limit are steps of the match engine. 1606 * Correspondence with actual processor time will depend on the speed 1607 * of the processor and the details of the specific pattern, but will 1608 * typically be on the order of milliseconds. 1609 * <p> 1610 * By default, the matching time is not limited. 1611 * <p> 1612 * 1613 * @param limit The limit value, or 0 for no limit. 1614 * @param status A reference to a UErrorCode to receive any errors. 1615 * @stable ICU 4.0 1616 */ 1617 virtual void setTimeLimit(int32_t limit, UErrorCode &status); 1618 1619 /** 1620 * Get the time limit, if any, for match operations made with this Matcher. 1621 * 1622 * @return the maximum allowed time for a match, in units of processing steps. 1623 * @stable ICU 4.0 1624 */ 1625 virtual int32_t getTimeLimit() const; 1626 1627 /** 1628 * Set the amount of heap storage available for use by the match backtracking stack. 1629 * The matcher is also reset, discarding any results from previous matches. 1630 * <p> 1631 * ICU uses a backtracking regular expression engine, with the backtrack stack 1632 * maintained on the heap. This function sets the limit to the amount of memory 1633 * that can be used for this purpose. A backtracking stack overflow will 1634 * result in an error from the match operation that caused it. 1635 * <p> 1636 * A limit is desirable because a malicious or poorly designed pattern can use 1637 * excessive memory, potentially crashing the process. A limit is enabled 1638 * by default. 1639 * <p> 1640 * @param limit The maximum size, in bytes, of the matching backtrack stack. 1641 * A value of zero means no limit. 1642 * The limit must be greater or equal to zero. 1643 * 1644 * @param status A reference to a UErrorCode to receive any errors. 1645 * 1646 * @stable ICU 4.0 1647 */ 1648 virtual void setStackLimit(int32_t limit, UErrorCode &status); 1649 1650 /** 1651 * Get the size of the heap storage available for use by the back tracking stack. 1652 * 1653 * @return the maximum backtracking stack size, in bytes, or zero if the 1654 * stack size is unlimited. 1655 * @stable ICU 4.0 1656 */ 1657 virtual int32_t getStackLimit() const; 1658 1659 1660 /** 1661 * Set a callback function for use with this Matcher. 1662 * During matching operations the function will be called periodically, 1663 * giving the application the opportunity to terminate a long-running 1664 * match. 1665 * 1666 * @param callback A pointer to the user-supplied callback function. 1667 * @param context User context pointer. The value supplied at the 1668 * time the callback function is set will be saved 1669 * and passed to the callback each time that it is called. 1670 * @param status A reference to a UErrorCode to receive any errors. 1671 * @stable ICU 4.0 1672 */ 1673 virtual void setMatchCallback(URegexMatchCallback *callback, 1674 const void *context, 1675 UErrorCode &status); 1676 1677 1678 /** 1679 * Get the callback function for this URegularExpression. 1680 * 1681 * @param callback Out parameter, receives a pointer to the user-supplied 1682 * callback function. 1683 * @param context Out parameter, receives the user context pointer that 1684 * was set when uregex_setMatchCallback() was called. 1685 * @param status A reference to a UErrorCode to receive any errors. 1686 * @stable ICU 4.0 1687 */ 1688 virtual void getMatchCallback(URegexMatchCallback *&callback, 1689 const void *&context, 1690 UErrorCode &status); 1691 1692 1693 /** 1694 * Set a progress callback function for use with find operations on this Matcher. 1695 * During find operations, the callback will be invoked after each return from a 1696 * match attempt, giving the application the opportunity to terminate a long-running 1697 * find operation. 1698 * 1699 * @param callback A pointer to the user-supplied callback function. 1700 * @param context User context pointer. The value supplied at the 1701 * time the callback function is set will be saved 1702 * and passed to the callback each time that it is called. 1703 * @param status A reference to a UErrorCode to receive any errors. 1704 * @stable ICU 4.6 1705 */ 1706 virtual void setFindProgressCallback(URegexFindProgressCallback *callback, 1707 const void *context, 1708 UErrorCode &status); 1709 1710 1711 /** 1712 * Get the find progress callback function for this URegularExpression. 1713 * 1714 * @param callback Out parameter, receives a pointer to the user-supplied 1715 * callback function. 1716 * @param context Out parameter, receives the user context pointer that 1717 * was set when uregex_setFindProgressCallback() was called. 1718 * @param status A reference to a UErrorCode to receive any errors. 1719 * @stable ICU 4.6 1720 */ 1721 virtual void getFindProgressCallback(URegexFindProgressCallback *&callback, 1722 const void *&context, 1723 UErrorCode &status); 1724 1725 #ifndef U_HIDE_INTERNAL_API 1726 /** 1727 * setTrace Debug function, enable/disable tracing of the matching engine. 1728 * For internal ICU development use only. DO NO USE!!!! 1729 * @internal 1730 */ 1731 void setTrace(UBool state); 1732 #endif /* U_HIDE_INTERNAL_API */ 1733 1734 /** 1735 * ICU "poor man's RTTI", returns a UClassID for this class. 1736 * 1737 * @stable ICU 2.2 1738 */ 1739 static UClassID U_EXPORT2 getStaticClassID(); 1740 1741 /** 1742 * ICU "poor man's RTTI", returns a UClassID for the actual class. 1743 * 1744 * @stable ICU 2.2 1745 */ 1746 virtual UClassID getDynamicClassID() const; 1747 1748 private: 1749 // Constructors and other object boilerplate are private. 1750 // Instances of RegexMatcher can not be assigned, copied, cloned, etc. 1751 RegexMatcher(); // default constructor not implemented 1752 RegexMatcher(const RegexPattern *pat); 1753 RegexMatcher(const RegexMatcher &other); 1754 RegexMatcher &operator =(const RegexMatcher &rhs); 1755 void init(UErrorCode &status); // Common initialization 1756 void init2(UText *t, UErrorCode &e); // Common initialization, part 2. 1757 1758 friend class RegexPattern; 1759 friend class RegexCImpl; 1760 public: 1761 #ifndef U_HIDE_INTERNAL_API 1762 /** @internal */ 1763 void resetPreserveRegion(); // Reset matcher state, but preserve any region. 1764 #endif /* U_HIDE_INTERNAL_API */ 1765 private: 1766 1767 // 1768 // MatchAt This is the internal interface to the match engine itself. 1769 // Match status comes back in matcher member variables. 1770 // 1771 void MatchAt(int64_t startIdx, UBool toEnd, UErrorCode &status); 1772 inline void backTrack(int64_t &inputIdx, int32_t &patIdx); 1773 UBool isWordBoundary(int64_t pos); // perform Perl-like \b test 1774 UBool isUWordBoundary(int64_t pos); // perform RBBI based \b test 1775 REStackFrame *resetStack(); 1776 inline REStackFrame *StateSave(REStackFrame *fp, int64_t savePatIdx, UErrorCode &status); 1777 void IncrementTime(UErrorCode &status); 1778 1779 // Call user find callback function, if set. Return TRUE if operation should be interrupted. 1780 inline UBool findProgressInterrupt(int64_t matchIndex, UErrorCode &status); 1781 1782 int64_t appendGroup(int32_t groupNum, UText *dest, UErrorCode &status) const; 1783 1784 UBool findUsingChunk(UErrorCode &status); 1785 void MatchChunkAt(int32_t startIdx, UBool toEnd, UErrorCode &status); 1786 UBool isChunkWordBoundary(int32_t pos); 1787 1788 const RegexPattern *fPattern; 1789 RegexPattern *fPatternOwned; // Non-NULL if this matcher owns the pattern, and 1790 // should delete it when through. 1791 1792 const UnicodeString *fInput; // The string being matched. Only used for input() 1793 UText *fInputText; // The text being matched. Is never NULL. 1794 UText *fAltInputText; // A shallow copy of the text being matched. 1795 // Only created if the pattern contains backreferences. 1796 int64_t fInputLength; // Full length of the input text. 1797 int32_t fFrameSize; // The size of a frame in the backtrack stack. 1798 1799 int64_t fRegionStart; // Start of the input region, default = 0. 1800 int64_t fRegionLimit; // End of input region, default to input.length. 1801 1802 int64_t fAnchorStart; // Region bounds for anchoring operations (^ or $). 1803 int64_t fAnchorLimit; // See useAnchoringBounds 1804 1805 int64_t fLookStart; // Region bounds for look-ahead/behind and 1806 int64_t fLookLimit; // and other boundary tests. See 1807 // useTransparentBounds 1808 1809 int64_t fActiveStart; // Currently active bounds for matching. 1810 int64_t fActiveLimit; // Usually is the same as region, but 1811 // is changed to fLookStart/Limit when 1812 // entering look around regions. 1813 1814 UBool fTransparentBounds; // True if using transparent bounds. 1815 UBool fAnchoringBounds; // True if using anchoring bounds. 1816 1817 UBool fMatch; // True if the last attempted match was successful. 1818 int64_t fMatchStart; // Position of the start of the most recent match 1819 int64_t fMatchEnd; // First position after the end of the most recent match 1820 // Zero if no previous match, even when a region 1821 // is active. 1822 int64_t fLastMatchEnd; // First position after the end of the previous match, 1823 // or -1 if there was no previous match. 1824 int64_t fAppendPosition; // First position after the end of the previous 1825 // appendReplacement(). As described by the 1826 // JavaDoc for Java Matcher, where it is called 1827 // "append position" 1828 UBool fHitEnd; // True if the last match touched the end of input. 1829 UBool fRequireEnd; // True if the last match required end-of-input 1830 // (matched $ or Z) 1831 1832 UVector64 *fStack; 1833 REStackFrame *fFrame; // After finding a match, the last active stack frame, 1834 // which will contain the capture group results. 1835 // NOT valid while match engine is running. 1836 1837 int64_t *fData; // Data area for use by the compiled pattern. 1838 int64_t fSmallData[8]; // Use this for data if it's enough. 1839 1840 int32_t fTimeLimit; // Max time (in arbitrary steps) to let the 1841 // match engine run. Zero for unlimited. 1842 1843 int32_t fTime; // Match time, accumulates while matching. 1844 int32_t fTickCounter; // Low bits counter for time. Counts down StateSaves. 1845 // Kept separately from fTime to keep as much 1846 // code as possible out of the inline 1847 // StateSave function. 1848 1849 int32_t fStackLimit; // Maximum memory size to use for the backtrack 1850 // stack, in bytes. Zero for unlimited. 1851 1852 URegexMatchCallback *fCallbackFn; // Pointer to match progress callback funct. 1853 // NULL if there is no callback. 1854 const void *fCallbackContext; // User Context ptr for callback function. 1855 1856 URegexFindProgressCallback *fFindProgressCallbackFn; // Pointer to match progress callback funct. 1857 // NULL if there is no callback. 1858 const void *fFindProgressCallbackContext; // User Context ptr for callback function. 1859 1860 1861 UBool fInputUniStrMaybeMutable; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility. 1862 1863 UBool fTraceDebug; // Set true for debug tracing of match engine. 1864 1865 UErrorCode fDeferredStatus; // Save error state that cannot be immediately 1866 // reported, or that permanently disables this matcher. 1867 1868 RuleBasedBreakIterator *fWordBreakItr; 1869 }; 1870 1871 U_NAMESPACE_END 1872 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS 1873 #endif 1874