2 **********************************************************************
3 * Copyright (C) 2002-2013, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 * created on: 2002oct22
11 * created by: Andy Heninger
13 * ICU Regular Expressions, API for C++
23 * \brief C++ API: Regular Expressions
25 * <h2>Regular Expression API</h2>
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>
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>
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.
45 #include "unicode/utypes.h"
47 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
49 #include "unicode/uobject.h"
50 #include "unicode/unistr.h"
51 #include "unicode/utext.h"
52 #include "unicode/parseerr.h"
54 #include "unicode/uregex.h"
56 // Forward Declarations
65 class RuleBasedBreakIterator
;
71 #ifndef U_HIDE_INTERNAL_API
73 * RBBIPatternDump Debug function, displays the compiled form of a pattern.
77 U_INTERNAL
void U_EXPORT2
78 RegexPatternDump(const RegexPattern
*pat
);
80 #undef RegexPatternDump
81 #define RegexPatternDump(pat)
83 #endif /* U_HIDE_INTERNAL_API */
88 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
89 * factory methods for creating a RegexPattern object from the source (string) form
90 * of a regular expression, methods for creating RegexMatchers that allow the pattern
91 * to be applied to input text, and a few convenience methods for simple common
92 * uses of regular expressions.
94 * <p>Class RegexPattern is not intended to be subclassed.</p>
98 class U_I18N_API RegexPattern
: public UObject
{
102 * default constructor. Create a RegexPattern object that refers to no actual
103 * pattern. Not normally needed; RegexPattern objects are usually
104 * created using the factory method <code>compile()</code>.
111 * Copy Constructor. Create a new RegexPattern object that is equivalent
112 * to the source object.
113 * @param source the pattern object to be copied.
116 RegexPattern(const RegexPattern
&source
);
119 * Destructor. Note that a RegexPattern object must persist so long as any
120 * RegexMatcher objects that were created from the RegexPattern are active.
123 virtual ~RegexPattern();
126 * Comparison operator. Two RegexPattern objects are considered equal if they
127 * were constructed from identical source patterns using the same match flag
129 * @param that a RegexPattern object to compare with "this".
130 * @return TRUE if the objects are equivalent.
133 UBool
operator==(const RegexPattern
& that
) const;
136 * Comparison operator. Two RegexPattern objects are considered equal if they
137 * were constructed from identical source patterns using the same match flag
139 * @param that a RegexPattern object to compare with "this".
140 * @return TRUE if the objects are different.
143 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);}
146 * Assignment operator. After assignment, this RegexPattern will behave identically
147 * to the source object.
150 RegexPattern
&operator =(const RegexPattern
&source
);
153 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
154 * intended to be subclasses, <code>clone()</code> and the copy construction are
155 * equivalent operations.
156 * @return the copy of this RegexPattern
159 virtual RegexPattern
*clone() const;
163 * Compiles the regular expression in string form into a RegexPattern
164 * object. These compile methods, rather than the constructors, are the usual
165 * way that RegexPattern objects are created.
167 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
168 * objects created from the pattern are active. RegexMatchers keep a pointer
169 * back to their pattern, so premature deletion of the pattern is a
170 * catastrophic error.</p>
172 * <p>All pattern match mode flags are set to their default values.</p>
174 * <p>Note that it is often more convenient to construct a RegexMatcher directly
175 * from a pattern string rather than separately compiling the pattern and
176 * then creating a RegexMatcher object from the pattern.</p>
178 * @param regex The regular expression to be compiled.
179 * @param pe Receives the position (line and column nubers) of any error
180 * within the regular expression.)
181 * @param status A reference to a UErrorCode to receive any errors.
182 * @return A regexPattern object for the compiled pattern.
186 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
191 * Compiles the regular expression in string form into a RegexPattern
192 * object. These compile methods, rather than the constructors, are the usual
193 * way that RegexPattern objects are created.
195 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
196 * objects created from the pattern are active. RegexMatchers keep a pointer
197 * back to their pattern, so premature deletion of the pattern is a
198 * catastrophic error.</p>
200 * <p>All pattern match mode flags are set to their default values.</p>
202 * <p>Note that it is often more convenient to construct a RegexMatcher directly
203 * from a pattern string rather than separately compiling the pattern and
204 * then creating a RegexMatcher object from the pattern.</p>
206 * @param regex The regular expression to be compiled. Note, the text referred
207 * to by this UText must not be deleted during the lifetime of the
208 * RegexPattern object or any RegexMatcher object created from it.
209 * @param pe Receives the position (line and column nubers) of any error
210 * within the regular expression.)
211 * @param status A reference to a UErrorCode to receive any errors.
212 * @return A regexPattern object for the compiled pattern.
216 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
221 * Compiles the regular expression in string form into a RegexPattern
222 * object using the specified match mode flags. These compile methods,
223 * rather than the constructors, are the usual way that RegexPattern objects
226 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
227 * objects created from the pattern are active. RegexMatchers keep a pointer
228 * back to their pattern, so premature deletion of the pattern is a
229 * catastrophic error.</p>
231 * <p>Note that it is often more convenient to construct a RegexMatcher directly
232 * from a pattern string instead of than separately compiling the pattern and
233 * then creating a RegexMatcher object from the pattern.</p>
235 * @param regex The regular expression to be compiled.
236 * @param flags The match mode flags to be used.
237 * @param pe Receives the position (line and column numbers) of any error
238 * within the regular expression.)
239 * @param status A reference to a UErrorCode to receive any errors.
240 * @return A regexPattern object for the compiled pattern.
244 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
250 * Compiles the regular expression in string form into a RegexPattern
251 * object using the specified match mode flags. These compile methods,
252 * rather than the constructors, are the usual way that RegexPattern objects
255 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
256 * objects created from the pattern are active. RegexMatchers keep a pointer
257 * back to their pattern, so premature deletion of the pattern is a
258 * catastrophic error.</p>
260 * <p>Note that it is often more convenient to construct a RegexMatcher directly
261 * from a pattern string instead of than separately compiling the pattern and
262 * then creating a RegexMatcher object from the pattern.</p>
264 * @param regex The regular expression to be compiled. Note, the text referred
265 * to by this UText must not be deleted during the lifetime of the
266 * RegexPattern object or any RegexMatcher object created from it.
267 * @param flags The match mode flags to be used.
268 * @param pe Receives the position (line and column numbers) of any error
269 * within the regular expression.)
270 * @param status A reference to a UErrorCode to receive any errors.
271 * @return A regexPattern object for the compiled pattern.
275 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
281 * Compiles the regular expression in string form into a RegexPattern
282 * object using the specified match mode flags. These compile methods,
283 * rather than the constructors, are the usual way that RegexPattern objects
286 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
287 * objects created from the pattern are active. RegexMatchers keep a pointer
288 * back to their pattern, so premature deletion of the pattern is a
289 * catastrophic error.</p>
291 * <p>Note that it is often more convenient to construct a RegexMatcher directly
292 * from a pattern string instead of than separately compiling the pattern and
293 * then creating a RegexMatcher object from the pattern.</p>
295 * @param regex The regular expression to be compiled.
296 * @param flags The match mode flags to be used.
297 * @param status A reference to a UErrorCode to receive any errors.
298 * @return A regexPattern object for the compiled pattern.
302 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
307 * Compiles the regular expression in string form into a RegexPattern
308 * object using the specified match mode flags. These compile methods,
309 * rather than the constructors, are the usual way that RegexPattern objects
312 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
313 * objects created from the pattern are active. RegexMatchers keep a pointer
314 * back to their pattern, so premature deletion of the pattern is a
315 * catastrophic error.</p>
317 * <p>Note that it is often more convenient to construct a RegexMatcher directly
318 * from a pattern string instead of than separately compiling the pattern and
319 * then creating a RegexMatcher object from the pattern.</p>
321 * @param regex The regular expression to be compiled. Note, the text referred
322 * to by this UText must not be deleted during the lifetime of the
323 * RegexPattern object or any RegexMatcher object created from it.
324 * @param flags The match mode flags to be used.
325 * @param status A reference to a UErrorCode to receive any errors.
326 * @return A regexPattern object for the compiled pattern.
330 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
335 * Get the match mode flags that were used when compiling this pattern.
336 * @return the match mode flags
339 virtual uint32_t flags() const;
342 * Creates a RegexMatcher that will match the given input against this pattern. The
343 * RegexMatcher can then be used to perform match, find or replace operations
344 * on the input. Note that a RegexPattern object must not be deleted while
345 * RegexMatchers created from it still exist and might possibly be used again.
347 * The matcher will retain a reference to the supplied input string, and all regexp
348 * pattern matching operations happen directly on this original string. It is
349 * critical that the string not be altered or deleted before use by the regular
350 * expression operations is complete.
352 * @param input The input string to which the regular expression will be applied.
353 * @param status A reference to a UErrorCode to receive any errors.
354 * @return A RegexMatcher object for this pattern and input.
358 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
359 UErrorCode
&status
) const;
363 * Cause a compilation error if an application accidentally attempts to
364 * create a matcher with a (UChar *) string as input rather than
365 * a UnicodeString. Avoids a dangling reference to a temporary string.
367 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
368 * using one of the aliasing constructors, such as
369 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
370 * or in a UText, using
371 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
375 RegexMatcher
*matcher(const UChar
*input
,
376 UErrorCode
&status
) const;
381 * Creates a RegexMatcher that will match against this pattern. The
382 * RegexMatcher can be used to perform match, find or replace operations.
383 * Note that a RegexPattern object must not be deleted while
384 * RegexMatchers created from it still exist and might possibly be used again.
386 * @param status A reference to a UErrorCode to receive any errors.
387 * @return A RegexMatcher object for this pattern and input.
391 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
395 * Test whether a string matches a regular expression. This convenience function
396 * both compiles the regular expression and applies it in a single operation.
397 * Note that if the same pattern needs to be applied repeatedly, this method will be
398 * less efficient than creating and reusing a RegexMatcher object.
400 * @param regex The regular expression
401 * @param input The string data to be matched
402 * @param pe Receives the position of any syntax errors within the regular expression
403 * @param status A reference to a UErrorCode to receive any errors.
404 * @return True if the regular expression exactly matches the full input string.
408 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
409 const UnicodeString
&input
,
414 * Test whether a string matches a regular expression. This convenience function
415 * both compiles the regular expression and applies it in a single operation.
416 * Note that if the same pattern needs to be applied repeatedly, this method will be
417 * less efficient than creating and reusing a RegexMatcher object.
419 * @param regex The regular expression
420 * @param input The string data to be matched
421 * @param pe Receives the position of any syntax errors within the regular expression
422 * @param status A reference to a UErrorCode to receive any errors.
423 * @return True if the regular expression exactly matches the full input string.
427 static UBool U_EXPORT2
matches(UText
*regex
,
433 * Returns the regular expression from which this pattern was compiled. This method will work
434 * even if the pattern was compiled from a UText.
436 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
437 * the returned string may no longer reflect the RegexPattern object.
440 virtual UnicodeString
pattern() const;
444 * Returns the regular expression from which this pattern was compiled. This method will work
445 * even if the pattern was compiled from a UnicodeString.
447 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
448 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
453 virtual UText
*patternText(UErrorCode
&status
) const;
457 * Split a string into fields. Somewhat like split() from Perl or Java.
458 * Pattern matches identify delimiters that separate the input
459 * into fields. The input data between the delimiters becomes the
462 * If the delimiter pattern includes capture groups, the captured text will
463 * also appear in the destination array of output strings, interspersed
464 * with the fields. This is similar to Perl, but differs from Java,
465 * which ignores the presence of capture groups in the pattern.
467 * Trailing empty fields will always be returned, assuming sufficient
468 * destination capacity. This differs from the default behavior for Java
469 * and Perl where trailing empty fields are not returned.
471 * The number of strings produced by the split operation is returned.
472 * This count includes the strings from capture groups in the delimiter pattern.
473 * This behavior differs from Java, which ignores capture groups.
475 * For the best performance on split() operations,
476 * <code>RegexMatcher::split</code> is preferable to this function
478 * @param input The string to be split into fields. The field delimiters
479 * match the pattern (in the "this" object)
480 * @param dest An array of UnicodeStrings to receive the results of the split.
481 * This is an array of actual UnicodeString objects, not an
482 * array of pointers to strings. Local (stack based) arrays can
484 * @param destCapacity The number of elements in the destination array.
485 * If the number of fields found is less than destCapacity, the
486 * extra strings in the destination array are not altered.
487 * If the number of destination strings is less than the number
488 * of fields, the trailing part of the input string, including any
489 * field delimiters, is placed in the last destination string.
490 * @param status A reference to a UErrorCode to receive any errors.
491 * @return The number of fields into which the input string was split.
494 virtual int32_t split(const UnicodeString
&input
,
495 UnicodeString dest
[],
496 int32_t destCapacity
,
497 UErrorCode
&status
) const;
501 * Split a string into fields. Somewhat like split() from Perl or Java.
502 * Pattern matches identify delimiters that separate the input
503 * into fields. The input data between the delimiters becomes the
506 * If the delimiter pattern includes capture groups, the captured text will
507 * also appear in the destination array of output strings, interspersed
508 * with the fields. This is similar to Perl, but differs from Java,
509 * which ignores the presence of capture groups in the pattern.
511 * Trailing empty fields will always be returned, assuming sufficient
512 * destination capacity. This differs from the default behavior for Java
513 * and Perl where trailing empty fields are not returned.
515 * The number of strings produced by the split operation is returned.
516 * This count includes the strings from capture groups in the delimiter pattern.
517 * This behavior differs from Java, which ignores capture groups.
519 * For the best performance on split() operations,
520 * <code>RegexMatcher::split</code> is preferable to this function
522 * @param input The string to be split into fields. The field delimiters
523 * match the pattern (in the "this" object)
524 * @param dest An array of mutable UText structs to receive the results of the split.
525 * If a field is NULL, a new UText is allocated to contain the results for
526 * that field. This new UText is not guaranteed to be mutable.
527 * @param destCapacity The number of elements in the destination array.
528 * If the number of fields found is less than destCapacity, the
529 * extra strings in the destination array are not altered.
530 * If the number of destination strings is less than the number
531 * of fields, the trailing part of the input string, including any
532 * field delimiters, is placed in the last destination string.
533 * @param status A reference to a UErrorCode to receive any errors.
534 * @return The number of destination strings used.
538 virtual int32_t split(UText
*input
,
540 int32_t destCapacity
,
541 UErrorCode
&status
) const;
545 * ICU "poor man's RTTI", returns a UClassID for the actual class.
549 virtual UClassID
getDynamicClassID() const;
552 * ICU "poor man's RTTI", returns a UClassID for this class.
556 static UClassID U_EXPORT2
getStaticClassID();
560 // Implementation Data
562 UText
*fPattern
; // The original pattern string.
563 UnicodeString
*fPatternString
; // The original pattern UncodeString if relevant
564 uint32_t fFlags
; // The flags used when compiling the pattern.
566 UVector64
*fCompiledPat
; // The compiled pattern p-code.
567 UnicodeString fLiteralText
; // Any literal string data from the pattern,
568 // after un-escaping, for use during the match.
570 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
571 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
574 UErrorCode fDeferredStatus
; // status if some prior error has left this
575 // RegexPattern in an unusable state.
577 int32_t fMinMatchLen
; // Minimum Match Length. All matches will have length
578 // >= this value. For some patterns, this calculated
579 // value may be less than the true shortest
582 int32_t fFrameSize
; // Size of a state stack frame in the
585 int32_t fDataSize
; // The size of the data needed by the pattern that
586 // does not go on the state stack, but has just
587 // a single copy per matcher.
589 UVector32
*fGroupMap
; // Map from capture group number to position of
590 // the group's variables in the matcher stack frame.
592 int32_t fMaxCaptureDigits
;
594 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
595 // regex character classes, e.g. Word.
597 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
598 // sets for predefined regex classes.
600 int32_t fStartType
; // Info on how a match must start.
601 int32_t fInitialStringIdx
; //
602 int32_t fInitialStringLen
;
603 UnicodeSet
*fInitialChars
;
604 UChar32 fInitialChar
;
605 Regex8BitSet
*fInitialChars8
;
606 UBool fNeedsAltInput
;
608 friend class RegexCompile
;
609 friend class RegexMatcher
;
610 friend class RegexCImpl
;
613 // Implementation Methods
615 void init(); // Common initialization, for use by constructors.
616 void zap(); // Common cleanup
618 void dumpOp(int32_t index
) const;
619 friend void U_EXPORT2
RegexPatternDump(const RegexPattern
*);
627 * class RegexMatcher bundles together a regular expression pattern and
628 * input text to which the expression can be applied. It includes methods
629 * for testing for matches, and for find and replace operations.
631 * <p>Class RegexMatcher is not intended to be subclassed.</p>
635 class U_I18N_API RegexMatcher
: public UObject
{
639 * Construct a RegexMatcher for a regular expression.
640 * This is a convenience method that avoids the need to explicitly create
641 * a RegexPattern object. Note that if several RegexMatchers need to be
642 * created for the same expression, it will be more efficient to
643 * separately create and cache a RegexPattern object, and use
644 * its matcher() method to create the RegexMatcher objects.
646 * @param regexp The Regular Expression to be compiled.
647 * @param flags Regular expression options, such as case insensitive matching.
648 * @see UREGEX_CASE_INSENSITIVE
649 * @param status Any errors are reported by setting this UErrorCode variable.
652 RegexMatcher(const UnicodeString
®exp
, uint32_t flags
, UErrorCode
&status
);
655 * Construct a RegexMatcher for a regular expression.
656 * This is a convenience method that avoids the need to explicitly create
657 * a RegexPattern object. Note that if several RegexMatchers need to be
658 * created for the same expression, it will be more efficient to
659 * separately create and cache a RegexPattern object, and use
660 * its matcher() method to create the RegexMatcher objects.
662 * @param regexp The regular expression to be compiled.
663 * @param flags Regular expression options, such as case insensitive matching.
664 * @see UREGEX_CASE_INSENSITIVE
665 * @param status Any errors are reported by setting this UErrorCode variable.
669 RegexMatcher(UText
*regexp
, uint32_t flags
, UErrorCode
&status
);
672 * Construct a RegexMatcher for a regular expression.
673 * This is a convenience method that avoids the need to explicitly create
674 * a RegexPattern object. Note that if several RegexMatchers need to be
675 * created for the same expression, it will be more efficient to
676 * separately create and cache a RegexPattern object, and use
677 * its matcher() method to create the RegexMatcher objects.
679 * The matcher will retain a reference to the supplied input string, and all regexp
680 * pattern matching operations happen directly on the original string. It is
681 * critical that the string not be altered or deleted before use by the regular
682 * expression operations is complete.
684 * @param regexp The Regular Expression to be compiled.
685 * @param input The string to match. The matcher retains a reference to the
686 * caller's string; mo copy is made.
687 * @param flags Regular expression options, such as case insensitive matching.
688 * @see UREGEX_CASE_INSENSITIVE
689 * @param status Any errors are reported by setting this UErrorCode variable.
692 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
693 uint32_t flags
, UErrorCode
&status
);
696 * Construct a RegexMatcher for a regular expression.
697 * This is a convenience method that avoids the need to explicitly create
698 * a RegexPattern object. Note that if several RegexMatchers need to be
699 * created for the same expression, it will be more efficient to
700 * separately create and cache a RegexPattern object, and use
701 * its matcher() method to create the RegexMatcher objects.
703 * The matcher will make a shallow clone of the supplied input text, and all regexp
704 * pattern matching operations happen on this clone. While read-only operations on
705 * the supplied text are permitted, it is critical that the underlying string not be
706 * altered or deleted before use by the regular expression operations is complete.
708 * @param regexp The Regular Expression to be compiled.
709 * @param input The string to match. The matcher retains a shallow clone of the text.
710 * @param flags Regular expression options, such as case insensitive matching.
711 * @see UREGEX_CASE_INSENSITIVE
712 * @param status Any errors are reported by setting this UErrorCode variable.
716 RegexMatcher(UText
*regexp
, UText
*input
,
717 uint32_t flags
, UErrorCode
&status
);
721 * Cause a compilation error if an application accidentally attempts to
722 * create a matcher with a (UChar *) string as input rather than
723 * a UnicodeString. Avoids a dangling reference to a temporary string.
725 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
726 * using one of the aliasing constructors, such as
727 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
728 * or in a UText, using
729 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
733 RegexMatcher(const UnicodeString
®exp
, const UChar
*input
,
734 uint32_t flags
, UErrorCode
&status
);
743 virtual ~RegexMatcher();
747 * Attempts to match the entire input region against the pattern.
748 * @param status A reference to a UErrorCode to receive any errors.
749 * @return TRUE if there is a match
752 virtual UBool
matches(UErrorCode
&status
);
756 * Resets the matcher, then attempts to match the input beginning
757 * at the specified startIndex, and extending to the end of the input.
758 * The input region is reset to include the entire input string.
759 * A successful match must extend to the end of the input.
760 * @param startIndex The input string (native) index at which to begin matching.
761 * @param status A reference to a UErrorCode to receive any errors.
762 * @return TRUE if there is a match
765 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
769 * Attempts to match the input string, starting from the beginning of the region,
770 * against the pattern. Like the matches() method, this function
771 * always starts at the beginning of the input region;
772 * unlike that function, it does not require that the entire region be matched.
774 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
775 * <code>end()</code>, and <code>group()</code> functions.</p>
777 * @param status A reference to a UErrorCode to receive any errors.
778 * @return TRUE if there is a match at the start of the input string.
781 virtual UBool
lookingAt(UErrorCode
&status
);
785 * Attempts to match the input string, starting from the specified index, against the pattern.
786 * The match may be of any length, and is not required to extend to the end
787 * of the input string. Contrast with match().
789 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
790 * <code>end()</code>, and <code>group()</code> functions.</p>
792 * @param startIndex The input string (native) index at which to begin matching.
793 * @param status A reference to a UErrorCode to receive any errors.
794 * @return TRUE if there is a match.
797 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
801 * Find the next pattern match in the input string.
802 * The find begins searching the input at the location following the end of
803 * the previous match, or at the start of the string if there is no previous match.
804 * If a match is found, <code>start(), end()</code> and <code>group()</code>
805 * will provide more information regarding the match.
806 * <p>Note that if the input string is changed by the application,
807 * use find(startPos, status) instead of find(), because the saved starting
808 * position may not be valid with the altered input string.</p>
809 * @return TRUE if a match is found.
812 virtual UBool
find();
816 * Resets this RegexMatcher and then attempts to find the next substring of the
817 * input string that matches the pattern, starting at the specified index.
819 * @param start The (native) index in the input string to begin the search.
820 * @param status A reference to a UErrorCode to receive any errors.
821 * @return TRUE if a match is found.
824 virtual UBool
find(int64_t start
, UErrorCode
&status
);
828 * Returns a string containing the text matched by the previous match.
829 * If the pattern can match an empty string, an empty string may be returned.
830 * @param status A reference to a UErrorCode to receive any errors.
831 * Possible errors are U_REGEX_INVALID_STATE if no match
832 * has been attempted or the last match failed.
833 * @return a string containing the matched input text.
836 virtual UnicodeString
group(UErrorCode
&status
) const;
840 * Returns a string containing the text captured by the given group
841 * during the previous match operation. Group(0) is the entire match.
843 * @param groupNum the capture group number
844 * @param status A reference to a UErrorCode to receive any errors.
845 * Possible errors are U_REGEX_INVALID_STATE if no match
846 * has been attempted or the last match failed and
847 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
848 * @return the captured text
851 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
855 * Returns the number of capturing groups in this matcher's pattern.
856 * @return the number of capture groups
859 virtual int32_t groupCount() const;
863 * Returns a shallow clone of the entire live input string with the UText current native index
864 * set to the beginning of the requested group.
866 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
867 * @param group_len A reference to receive the length of the desired capture group
868 * @param status A reference to a UErrorCode to receive any errors.
869 * Possible errors are U_REGEX_INVALID_STATE if no match
870 * has been attempted or the last match failed and
871 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
872 * @return dest if non-NULL, a shallow copy of the input text otherwise
876 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
879 * Returns a shallow clone of the entire live input string with the UText current native index
880 * set to the beginning of the requested group.
882 * @param groupNum The capture group number.
883 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
884 * @param group_len A reference to receive the length of the desired capture group
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 dest if non-NULL, a shallow copy of the input text otherwise
893 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
896 * Returns a string containing the text captured by the given group
897 * during the previous match operation. Group(0) is the entire match.
899 * @param groupNum the capture group number
900 * @param dest A mutable UText in which the matching text is placed.
901 * If NULL, a new UText will be created (which may not be mutable).
902 * @param status A reference to a UErrorCode to receive any errors.
903 * Possible errors are U_REGEX_INVALID_STATE if no match
904 * has been attempted or the last match failed.
905 * @return A string containing the matched input text. If a pre-allocated UText
906 * was provided, it will always be used and returned.
908 * @internal ICU 4.4 technology preview
910 virtual UText
*group(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
914 * Returns the index in the input string of the start of the text matched
915 * during the previous match operation.
916 * @param status a reference to a UErrorCode to receive any errors.
917 * @return The (native) position in the input string of the start of the last match.
920 virtual int32_t start(UErrorCode
&status
) const;
923 * Returns the index in the input string of the start of the text matched
924 * during the previous match operation.
925 * @param status a reference to a UErrorCode to receive any errors.
926 * @return The (native) position in the input string of the start of the last match.
929 virtual int64_t start64(UErrorCode
&status
) const;
933 * Returns the index in the input string of the start of the text matched by the
934 * specified capture group during the previous match operation. Return -1 if
935 * the capture group exists in the pattern, but was not part of the last match.
937 * @param group the capture group number
938 * @param status A reference to a UErrorCode to receive any errors. Possible
939 * errors are U_REGEX_INVALID_STATE if no match has been
940 * attempted or the last match failed, and
941 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
942 * @return the (native) start position of substring matched by the specified group.
945 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
948 * Returns the index in the input string of the start of the text matched by the
949 * specified capture group during the previous match operation. Return -1 if
950 * the capture group exists in the pattern, but was not part of the last match.
952 * @param group the capture group number.
953 * @param status A reference to a UErrorCode to receive any errors. Possible
954 * errors are U_REGEX_INVALID_STATE if no match has been
955 * attempted or the last match failed, and
956 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
957 * @return the (native) start position of substring matched by the specified group.
960 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
964 * Returns the index in the input string of the first character following the
965 * text matched during the previous match operation.
967 * @param status A reference to a UErrorCode to receive any errors. Possible
968 * errors are U_REGEX_INVALID_STATE if no match has been
969 * attempted or the last match failed.
970 * @return the index of the last character matched, plus one.
971 * The index value returned is a native index, corresponding to
972 * code units for the underlying encoding type, for example,
973 * a byte index for UTF-8.
976 virtual int32_t end(UErrorCode
&status
) const;
979 * Returns the index in the input string of the first character following the
980 * text matched during the previous match operation.
982 * @param status A reference to a UErrorCode to receive any errors. Possible
983 * errors are U_REGEX_INVALID_STATE if no match has been
984 * attempted or the last match failed.
985 * @return the index of the last character matched, plus one.
986 * The index value returned is a native index, corresponding to
987 * code units for the underlying encoding type, for example,
988 * a byte index for UTF-8.
991 virtual int64_t end64(UErrorCode
&status
) const;
995 * Returns the index in the input string of the character following the
996 * text matched by the specified capture group during the previous match operation.
998 * @param group the capture group number
999 * @param status A reference to a UErrorCode to receive any errors. Possible
1000 * errors are U_REGEX_INVALID_STATE if no match has been
1001 * attempted or the last match failed and
1002 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1003 * @return the index of the first character following the text
1004 * captured by the specified group during the previous match operation.
1005 * Return -1 if the capture group exists in the pattern but was not part of the match.
1006 * The index value returned is a native index, corresponding to
1007 * code units for the underlying encoding type, for example,
1008 * a byte index for UTF8.
1011 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
1014 * Returns the index in the input string of the character following the
1015 * text matched by the specified capture group during the previous match operation.
1017 * @param group the capture group number
1018 * @param status A reference to a UErrorCode to receive any errors. Possible
1019 * errors are U_REGEX_INVALID_STATE if no match has been
1020 * attempted or the last match failed and
1021 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1022 * @return the index of the first character following the text
1023 * captured by the specified group during the previous match operation.
1024 * Return -1 if the capture group exists in the pattern but was not part of the match.
1025 * The index value returned is a native index, corresponding to
1026 * code units for the underlying encoding type, for example,
1027 * a byte index for UTF8.
1030 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
1034 * Resets this matcher. The effect is to remove any memory of previous matches,
1035 * and to cause subsequent find() operations to begin at the beginning of
1038 * @return this RegexMatcher.
1041 virtual RegexMatcher
&reset();
1045 * Resets this matcher, and set the current input position.
1046 * The effect is to remove any memory of previous matches,
1047 * and to cause subsequent find() operations to begin at
1048 * the specified (native) position in the input string.
1050 * The matcher's region is reset to its default, which is the entire
1053 * An alternative to this function is to set a match region
1054 * beginning at the desired index.
1056 * @return this RegexMatcher.
1059 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1063 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1064 * to be reused, which is more efficient than creating a new RegexMatcher for
1065 * each input string to be processed.
1066 * @param input The new string on which subsequent pattern matches will operate.
1067 * The matcher retains a reference to the callers string, and operates
1068 * directly on that. Ownership of the string remains with the caller.
1069 * Because no copy of the string is made, it is essential that the
1070 * caller not delete the string until after regexp operations on it
1072 * Note that while a reset on the matcher with an input string that is then
1073 * modified across/during matcher operations may be supported currently for UnicodeString,
1074 * this was not originally intended behavior, and support for this is not guaranteed
1075 * in upcoming versions of ICU.
1076 * @return this RegexMatcher.
1079 virtual RegexMatcher
&reset(const UnicodeString
&input
);
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 makes a shallow clone of the given text; ownership of the
1088 * original string remains with the caller. Because no deep copy of the
1089 * text is made, it is essential that the caller not modify the string
1090 * until after regexp operations on it are done.
1091 * @return this RegexMatcher.
1095 virtual RegexMatcher
&reset(UText
*input
);
1099 * Set the subject text string upon which the regular expression is looking for matches
1100 * without changing any other aspect of the matching state.
1101 * The new and previous text strings must have the same content.
1103 * This function is intended for use in environments where ICU is operating on
1104 * strings that may move around in memory. It provides a mechanism for notifying
1105 * ICU that the string has been relocated, and providing a new UText to access the
1106 * string in its new position.
1108 * Note that the regular expression implementation never copies the underlying text
1109 * of a string being matched, but always operates directly on the original text
1110 * provided by the user. Refreshing simply drops the references to the old text
1111 * and replaces them with references to the new.
1113 * Caution: this function is normally used only by very specialized,
1114 * system-level code. One example use case is with garbage collection that moves
1115 * the text in memory.
1117 * @param input The new (moved) text string.
1118 * @param status Receives errors detected by this function.
1122 virtual RegexMatcher
&refreshInputText(UText
*input
, UErrorCode
&status
);
1126 * Cause a compilation error if an application accidentally attempts to
1127 * reset a matcher with a (UChar *) string as input rather than
1128 * a UnicodeString. Avoids a dangling reference to a temporary string.
1130 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
1131 * using one of the aliasing constructors, such as
1132 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
1133 * or in a UText, using
1134 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
1138 RegexMatcher
&reset(const UChar
*input
);
1142 * Returns the input string being matched. Ownership of the string belongs to
1143 * the matcher; it should not be altered or deleted. This method will work even if the input
1144 * was originally supplied as a UText.
1145 * @return the input string
1148 virtual const UnicodeString
&input() const;
1151 * Returns the input string being matched. This is the live input text; it should not be
1152 * altered or deleted. This method will work even if the input was originally supplied as
1154 * @return the input text
1158 virtual UText
*inputText() const;
1161 * Returns the input string being matched, either by copying it into the provided
1162 * UText parameter or by returning a shallow clone of the live input. Note that copying
1163 * the entire input may cause significant performance and memory issues.
1164 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1165 * @param status error code
1166 * @return dest if non-NULL, a shallow copy of the input text otherwise
1170 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1173 /** Sets the limits of this matcher's region.
1174 * The region is the part of the input string that will be searched to find a match.
1175 * Invoking this method resets the matcher, and then sets the region to start
1176 * at the index specified by the start parameter and end at the index specified
1177 * by the end parameter.
1179 * Depending on the transparency and anchoring being used (see useTransparentBounds
1180 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1181 * at or around the boundaries of the region
1183 * The function will fail if start is greater than limit, or if either index
1184 * is less than zero or greater than the length of the string being matched.
1186 * @param start The (native) index to begin searches at.
1187 * @param limit The index to end searches at (exclusive).
1188 * @param status A reference to a UErrorCode to receive any errors.
1191 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1194 * Identical to region(start, limit, status) but also allows a start position without
1195 * resetting the region state.
1196 * @param regionStart The region start
1197 * @param regionLimit the limit of the region
1198 * @param startIndex The (native) index within the region bounds at which to begin searches.
1199 * @param status A reference to a UErrorCode to receive any errors.
1200 * If startIndex is not within the specified region bounds,
1201 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1204 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
1207 * Reports the start index of this matcher's region. The searches this matcher
1208 * conducts are limited to finding matches within regionStart (inclusive) and
1209 * regionEnd (exclusive).
1211 * @return The starting (native) index of this matcher's region.
1214 virtual int32_t regionStart() const;
1217 * Reports the start index of this matcher's region. The searches this matcher
1218 * conducts are limited to finding matches within regionStart (inclusive) and
1219 * regionEnd (exclusive).
1221 * @return The starting (native) index of this matcher's region.
1224 virtual int64_t regionStart64() const;
1228 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1229 * this matcher conducts are limited to finding matches within regionStart
1230 * (inclusive) and regionEnd (exclusive).
1232 * @return The ending point (native) of this matcher's region.
1235 virtual int32_t regionEnd() const;
1238 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1239 * this matcher conducts are limited to finding matches within regionStart
1240 * (inclusive) and regionEnd (exclusive).
1242 * @return The ending point (native) of this matcher's region.
1245 virtual int64_t regionEnd64() const;
1248 * Queries the transparency of region bounds for this matcher.
1249 * See useTransparentBounds for a description of transparent and opaque bounds.
1250 * By default, a matcher uses opaque region boundaries.
1252 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1255 virtual UBool
hasTransparentBounds() const;
1258 * Sets the transparency of region bounds for this matcher.
1259 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1260 * If the boolean argument is false, then opaque bounds will be used.
1262 * Using transparent bounds, the boundaries of this matcher's region are transparent
1263 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1264 * see text beyond the boundaries of the region while checking for a match.
1266 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1267 * lookbehind, and boundary matching constructs.
1269 * By default, a matcher uses opaque bounds.
1271 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1272 * @return This Matcher;
1275 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1279 * Return true if this matcher is using anchoring bounds.
1280 * By default, matchers use anchoring region bounds.
1282 * @return TRUE if this matcher is using anchoring bounds.
1285 virtual UBool
hasAnchoringBounds() const;
1289 * Set whether this matcher is using Anchoring Bounds for its region.
1290 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1291 * and end of the region. Without Anchoring Bounds, anchors will only match at
1292 * the positions they would in the complete text.
1294 * Anchoring Bounds are the default for regions.
1296 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1297 * @return This Matcher
1300 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1304 * Return TRUE if the most recent matching operation attempted to access
1305 * additional input beyond the available input text.
1306 * In this case, additional input text could change the results of the match.
1308 * hitEnd() is defined for both successful and unsuccessful matches.
1309 * In either case hitEnd() will return TRUE if if the end of the text was
1310 * reached at any point during the matching process.
1312 * @return TRUE if the most recent match hit the end of input
1315 virtual UBool
hitEnd() const;
1318 * Return TRUE the most recent match succeeded and additional input could cause
1319 * it to fail. If this method returns false and a match was found, then more input
1320 * might change the match but the match won't be lost. If a match was not found,
1321 * then requireEnd has no meaning.
1323 * @return TRUE if more input could cause the most recent match to no longer match.
1326 virtual UBool
requireEnd() const;
1330 * Returns the pattern that is interpreted by this matcher.
1331 * @return the RegexPattern for this RegexMatcher
1334 virtual const RegexPattern
&pattern() const;
1338 * Replaces every substring of the input that matches the pattern
1339 * with the given replacement string. This is a convenience function that
1340 * provides a complete find-and-replace-all operation.
1342 * This method first resets this matcher. It then scans the input string
1343 * looking for matches of the pattern. Input that is not part of any
1344 * match is left unchanged; each match is replaced in the result by the
1345 * replacement string. The replacement string may contain references to
1348 * @param replacement a string containing the replacement text.
1349 * @param status a reference to a UErrorCode to receive any errors.
1350 * @return a string containing the results of the find and replace.
1353 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
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.
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
1367 * @param replacement a string containing the replacement text.
1368 * @param dest a mutable UText in which the results are placed.
1369 * If NULL, a new UText will be created (which may not be mutable).
1370 * @param status a reference to a UErrorCode to receive any errors.
1371 * @return a string containing the results of the find and replace.
1372 * If a pre-allocated UText was provided, it will always be used and returned.
1376 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1380 * Replaces the first substring of the input that matches
1381 * the pattern with the replacement string. This is a convenience
1382 * function that provides a complete find-and-replace operation.
1384 * <p>This function first resets this RegexMatcher. It then scans the input string
1385 * looking for a match of the pattern. Input that is not part
1386 * of the match is appended directly to the result string; the match is replaced
1387 * in the result by the replacement string. The replacement string may contain
1388 * references to captured groups.</p>
1390 * <p>The state of the matcher (the position at which a subsequent find()
1391 * would begin) after completing a replaceFirst() is not specified. The
1392 * RegexMatcher should be reset before doing additional find() operations.</p>
1394 * @param replacement a string containing the replacement text.
1395 * @param status a reference to a UErrorCode to receive any errors.
1396 * @return a string containing the results of the find and replace.
1399 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1403 * Replaces the first substring of the input that matches
1404 * the pattern with the replacement string. This is a convenience
1405 * function that provides a complete find-and-replace operation.
1407 * <p>This function first resets this RegexMatcher. It then scans the input string
1408 * looking for a match of the pattern. Input that is not part
1409 * of the match is appended directly to the result string; the match is replaced
1410 * in the result by the replacement string. The replacement string may contain
1411 * references to captured groups.</p>
1413 * <p>The state of the matcher (the position at which a subsequent find()
1414 * would begin) after completing a replaceFirst() is not specified. The
1415 * RegexMatcher should be reset before doing additional find() operations.</p>
1417 * @param replacement a string containing the replacement text.
1418 * @param dest a mutable UText in which the results are placed.
1419 * If NULL, a new UText will be created (which may not be mutable).
1420 * @param status a reference to a UErrorCode to receive any errors.
1421 * @return a string containing the results of the find and replace.
1422 * If a pre-allocated UText was provided, it will always be used and returned.
1426 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1430 * Implements a replace operation intended to be used as part of an
1431 * incremental find-and-replace.
1433 * <p>The input string, starting from the end of the previous replacement and ending at
1434 * the start of the current match, is appended to the destination string. Then the
1435 * replacement string is appended to the output string,
1436 * including handling any substitutions of captured text.</p>
1438 * <p>For simple, prepackaged, non-incremental find-and-replace
1439 * operations, see replaceFirst() or replaceAll().</p>
1441 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1442 * @param replacement A UnicodeString that provides the text to be substituted for
1443 * the input text that matched the regexp pattern. The replacement
1444 * text may contain references to captured text from the
1446 * @param status A reference to a UErrorCode to receive any errors. Possible
1447 * errors are U_REGEX_INVALID_STATE if no match has been
1448 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1449 * if the replacement text specifies a capture group that
1450 * does not exist in the pattern.
1452 * @return this RegexMatcher
1456 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1457 const UnicodeString
&replacement
, UErrorCode
&status
);
1461 * Implements a replace operation intended to be used as part of an
1462 * incremental find-and-replace.
1464 * <p>The input string, starting from the end of the previous replacement and ending at
1465 * the start of the current match, is appended to the destination string. Then the
1466 * replacement string is appended to the output string,
1467 * including handling any substitutions of captured text.</p>
1469 * <p>For simple, prepackaged, non-incremental find-and-replace
1470 * operations, see replaceFirst() or replaceAll().</p>
1472 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1474 * @param replacement A UText that provides the text to be substituted for
1475 * the input text that matched the regexp pattern. The replacement
1476 * text may contain references to captured text from the input.
1477 * @param status A reference to a UErrorCode to receive any errors. Possible
1478 * errors are U_REGEX_INVALID_STATE if no match has been
1479 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1480 * if the replacement text specifies a capture group that
1481 * does not exist in the pattern.
1483 * @return this RegexMatcher
1487 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1488 UText
*replacement
, UErrorCode
&status
);
1492 * As the final step in a find-and-replace operation, append the remainder
1493 * of the input string, starting at the position following the last appendReplacement(),
1494 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1495 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1497 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1498 * @return the destination string.
1501 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1505 * As the final step in a find-and-replace operation, append the remainder
1506 * of the input string, starting at the position following the last appendReplacement(),
1507 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1508 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1510 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1512 * @param status error cod
1513 * @return the destination string.
1517 virtual UText
*appendTail(UText
*dest
, UErrorCode
&status
);
1521 * Split a string into fields. Somewhat like split() from Perl.
1522 * The pattern matches identify delimiters that separate the input
1523 * into fields. The input data between the matches becomes the
1524 * fields themselves.
1526 * @param input The string to be split into fields. The field delimiters
1527 * match the pattern (in the "this" object). This matcher
1528 * will be reset to this input string.
1529 * @param dest An array of UnicodeStrings to receive the results of the split.
1530 * This is an array of actual UnicodeString objects, not an
1531 * array of pointers to strings. Local (stack based) arrays can
1533 * @param destCapacity The number of elements in the destination array.
1534 * If the number of fields found is less than destCapacity, the
1535 * extra strings in the destination array are not altered.
1536 * If the number of destination strings is less than the number
1537 * of fields, the trailing part of the input string, including any
1538 * field delimiters, is placed in the last destination string.
1539 * @param status A reference to a UErrorCode to receive any errors.
1540 * @return The number of fields into which the input string was split.
1543 virtual int32_t split(const UnicodeString
&input
,
1544 UnicodeString dest
[],
1545 int32_t destCapacity
,
1546 UErrorCode
&status
);
1550 * Split a string into fields. Somewhat like split() from Perl.
1551 * The pattern matches identify delimiters that separate the input
1552 * into fields. The input data between the matches becomes the
1553 * fields themselves.
1555 * @param input The string to be split into fields. The field delimiters
1556 * match the pattern (in the "this" object). This matcher
1557 * will be reset to this input string.
1558 * @param dest An array of mutable UText structs to receive the results of the split.
1559 * If a field is NULL, a new UText is allocated to contain the results for
1560 * that field. This new UText is not guaranteed to be mutable.
1561 * @param destCapacity The number of elements in the destination array.
1562 * If the number of fields found is less than destCapacity, the
1563 * extra strings in the destination array are not altered.
1564 * If the number of destination strings is less than the number
1565 * of fields, the trailing part of the input string, including any
1566 * field delimiters, is placed in the last destination string.
1567 * @param status A reference to a UErrorCode to receive any errors.
1568 * @return The number of fields into which the input string was split.
1572 virtual int32_t split(UText
*input
,
1574 int32_t destCapacity
,
1575 UErrorCode
&status
);
1578 * Set a processing time limit for match operations with this Matcher.
1580 * Some patterns, when matching certain strings, can run in exponential time.
1581 * For practical purposes, the match operation may appear to be in an
1583 * When a limit is set a match operation will fail with an error if the
1584 * limit is exceeded.
1586 * The units of the limit are steps of the match engine.
1587 * Correspondence with actual processor time will depend on the speed
1588 * of the processor and the details of the specific pattern, but will
1589 * typically be on the order of milliseconds.
1591 * By default, the matching time is not limited.
1594 * @param limit The limit value, or 0 for no limit.
1595 * @param status A reference to a UErrorCode to receive any errors.
1598 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1601 * Get the time limit, if any, for match operations made with this Matcher.
1603 * @return the maximum allowed time for a match, in units of processing steps.
1606 virtual int32_t getTimeLimit() const;
1609 * Set the amount of heap storage available for use by the match backtracking stack.
1610 * The matcher is also reset, discarding any results from previous matches.
1612 * ICU uses a backtracking regular expression engine, with the backtrack stack
1613 * maintained on the heap. This function sets the limit to the amount of memory
1614 * that can be used for this purpose. A backtracking stack overflow will
1615 * result in an error from the match operation that caused it.
1617 * A limit is desirable because a malicious or poorly designed pattern can use
1618 * excessive memory, potentially crashing the process. A limit is enabled
1621 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1622 * A value of zero means no limit.
1623 * The limit must be greater or equal to zero.
1625 * @param status A reference to a UErrorCode to receive any errors.
1629 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1632 * Get the size of the heap storage available for use by the back tracking stack.
1634 * @return the maximum backtracking stack size, in bytes, or zero if the
1635 * stack size is unlimited.
1638 virtual int32_t getStackLimit() const;
1642 * Set a callback function for use with this Matcher.
1643 * During matching operations the function will be called periodically,
1644 * giving the application the opportunity to terminate a long-running
1647 * @param callback A pointer to the user-supplied callback function.
1648 * @param context User context pointer. The value supplied at the
1649 * time the callback function is set will be saved
1650 * and passed to the callback each time that it is called.
1651 * @param status A reference to a UErrorCode to receive any errors.
1654 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1655 const void *context
,
1656 UErrorCode
&status
);
1660 * Get the callback function for this URegularExpression.
1662 * @param callback Out parameter, receives a pointer to the user-supplied
1663 * callback function.
1664 * @param context Out parameter, receives the user context pointer that
1665 * was set when uregex_setMatchCallback() was called.
1666 * @param status A reference to a UErrorCode to receive any errors.
1669 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1670 const void *&context
,
1671 UErrorCode
&status
);
1675 * Set a progress callback function for use with find operations on this Matcher.
1676 * During find operations, the callback will be invoked after each return from a
1677 * match attempt, giving the application the opportunity to terminate a long-running
1680 * @param callback A pointer to the user-supplied callback function.
1681 * @param context User context pointer. The value supplied at the
1682 * time the callback function is set will be saved
1683 * and passed to the callback each time that it is called.
1684 * @param status A reference to a UErrorCode to receive any errors.
1687 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1688 const void *context
,
1689 UErrorCode
&status
);
1693 * Get the find progress callback function for this URegularExpression.
1695 * @param callback Out parameter, receives a pointer to the user-supplied
1696 * callback function.
1697 * @param context Out parameter, receives the user context pointer that
1698 * was set when uregex_setFindProgressCallback() was called.
1699 * @param status A reference to a UErrorCode to receive any errors.
1702 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1703 const void *&context
,
1704 UErrorCode
&status
);
1706 #ifndef U_HIDE_INTERNAL_API
1708 * setTrace Debug function, enable/disable tracing of the matching engine.
1709 * For internal ICU development use only. DO NO USE!!!!
1712 void setTrace(UBool state
);
1713 #endif /* U_HIDE_INTERNAL_API */
1716 * ICU "poor man's RTTI", returns a UClassID for this class.
1720 static UClassID U_EXPORT2
getStaticClassID();
1723 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1727 virtual UClassID
getDynamicClassID() const;
1730 // Constructors and other object boilerplate are private.
1731 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1732 RegexMatcher(); // default constructor not implemented
1733 RegexMatcher(const RegexPattern
*pat
);
1734 RegexMatcher(const RegexMatcher
&other
);
1735 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1736 void init(UErrorCode
&status
); // Common initialization
1737 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1739 friend class RegexPattern
;
1740 friend class RegexCImpl
;
1742 #ifndef U_HIDE_INTERNAL_API
1744 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1745 #endif /* U_HIDE_INTERNAL_API */
1749 // MatchAt This is the internal interface to the match engine itself.
1750 // Match status comes back in matcher member variables.
1752 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1753 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1754 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1755 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1756 REStackFrame
*resetStack();
1757 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1758 void IncrementTime(UErrorCode
&status
);
1759 UBool
ReportFindProgress(int64_t matchIndex
, UErrorCode
&status
);
1761 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1763 UBool
findUsingChunk();
1764 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1765 UBool
isChunkWordBoundary(int32_t pos
);
1767 const RegexPattern
*fPattern
;
1768 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1769 // should delete it when through.
1771 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1772 UText
*fInputText
; // The text being matched. Is never NULL.
1773 UText
*fAltInputText
; // A shallow copy of the text being matched.
1774 // Only created if the pattern contains backreferences.
1775 int64_t fInputLength
; // Full length of the input text.
1776 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1778 int64_t fRegionStart
; // Start of the input region, default = 0.
1779 int64_t fRegionLimit
; // End of input region, default to input.length.
1781 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1782 int64_t fAnchorLimit
; // See useAnchoringBounds
1784 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1785 int64_t fLookLimit
; // and other boundary tests. See
1786 // useTransparentBounds
1788 int64_t fActiveStart
; // Currently active bounds for matching.
1789 int64_t fActiveLimit
; // Usually is the same as region, but
1790 // is changed to fLookStart/Limit when
1791 // entering look around regions.
1793 UBool fTransparentBounds
; // True if using transparent bounds.
1794 UBool fAnchoringBounds
; // True if using anchoring bounds.
1796 UBool fMatch
; // True if the last attempted match was successful.
1797 int64_t fMatchStart
; // Position of the start of the most recent match
1798 int64_t fMatchEnd
; // First position after the end of the most recent match
1799 // Zero if no previous match, even when a region
1801 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1802 // or -1 if there was no previous match.
1803 int64_t fAppendPosition
; // First position after the end of the previous
1804 // appendReplacement(). As described by the
1805 // JavaDoc for Java Matcher, where it is called
1806 // "append position"
1807 UBool fHitEnd
; // True if the last match touched the end of input.
1808 UBool fRequireEnd
; // True if the last match required end-of-input
1812 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1813 // which will contain the capture group results.
1814 // NOT valid while match engine is running.
1816 int64_t *fData
; // Data area for use by the compiled pattern.
1817 int64_t fSmallData
[8]; // Use this for data if it's enough.
1819 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1820 // match engine run. Zero for unlimited.
1822 int32_t fTime
; // Match time, accumulates while matching.
1823 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1824 // Kept separately from fTime to keep as much
1825 // code as possible out of the inline
1826 // StateSave function.
1828 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1829 // stack, in bytes. Zero for unlimited.
1831 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1832 // NULL if there is no callback.
1833 const void *fCallbackContext
; // User Context ptr for callback function.
1835 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1836 // NULL if there is no callback.
1837 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1840 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1842 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1844 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1845 // reported, or that permanently disables this matcher.
1847 RuleBasedBreakIterator
*fWordBreakItr
;
1851 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS