2 **********************************************************************
3 * Copyright (C) 2002-2016, 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
67 class RuleBasedBreakIterator
;
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.
81 * <p>Class RegexPattern is not intended to be subclassed.</p>
85 class U_I18N_API RegexPattern U_FINAL
: public UObject
{
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>.
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.
103 RegexPattern(const RegexPattern
&source
);
106 * Destructor. Note that a RegexPattern object must persist so long as any
107 * RegexMatcher objects that were created from the RegexPattern are active.
110 virtual ~RegexPattern();
113 * Comparison operator. Two RegexPattern objects are considered equal if they
114 * were constructed from identical source patterns using the same match flag
116 * @param that a RegexPattern object to compare with "this".
117 * @return TRUE if the objects are equivalent.
120 UBool
operator==(const RegexPattern
& that
) const;
123 * Comparison operator. Two RegexPattern objects are considered equal if they
124 * were constructed from identical source patterns using the same match flag
126 * @param that a RegexPattern object to compare with "this".
127 * @return TRUE if the objects are different.
130 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);}
133 * Assignment operator. After assignment, this RegexPattern will behave identically
134 * to the source object.
137 RegexPattern
&operator =(const RegexPattern
&source
);
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
146 virtual RegexPattern
*clone() const;
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.
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>
159 * <p>All pattern match mode flags are set to their default values.</p>
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>
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.
173 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
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.
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>
187 * <p>All pattern match mode flags are set to their default values.</p>
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>
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.
203 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
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
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>
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>
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.
231 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
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
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>
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>
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.
262 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
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
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>
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>
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.
289 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
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
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>
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>
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.
317 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
322 * Get the match mode flags that were used when compiling this pattern.
323 * @return the match mode flags
326 virtual uint32_t flags() const;
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.
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.
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.
345 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
346 UErrorCode
&status
) const;
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.
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>
361 RegexMatcher
*matcher(const UChar
*input
,
362 UErrorCode
&status
) const;
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.
372 * @param status A reference to a UErrorCode to receive any errors.
373 * @return A RegexMatcher object for this pattern and input.
377 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
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.
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.
394 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
395 const UnicodeString
&input
,
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.
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.
413 static UBool U_EXPORT2
matches(UText
*regex
,
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.
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.
426 virtual UnicodeString
pattern() const;
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.
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
439 virtual UText
*patternText(UErrorCode
&status
) const;
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.
447 * The function returns an error status if the specified name does not
448 * appear in the pattern.
450 * @param groupName The capture group name.
451 * @param status A UErrorCode to receive any errors.
455 virtual int32_t groupNumberFromName(const UnicodeString
&groupName
, UErrorCode
&status
) const;
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.
463 * The function returns an error status if the specified name does not
464 * appear in the pattern.
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
470 * @param status A UErrorCode to receive any errors.
474 virtual int32_t groupNumberFromName(const char *groupName
, int32_t nameLength
, UErrorCode
&status
) const;
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
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.
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.
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.
496 * For the best performance on split() operations,
497 * <code>RegexMatcher::split</code> is preferable to this function
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
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.
515 virtual int32_t split(const UnicodeString
&input
,
516 UnicodeString dest
[],
517 int32_t destCapacity
,
518 UErrorCode
&status
) const;
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
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.
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.
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.
540 * For the best performance on split() operations,
541 * <code>RegexMatcher::split</code> is preferable to this function
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.
559 virtual int32_t split(UText
*input
,
561 int32_t destCapacity
,
562 UErrorCode
&status
) const;
566 * ICU "poor man's RTTI", returns a UClassID for the actual class.
570 virtual UClassID
getDynamicClassID() const;
573 * ICU "poor man's RTTI", returns a UClassID for this class.
577 static UClassID U_EXPORT2
getStaticClassID();
581 // Implementation Data
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.
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.
591 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
592 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
595 UErrorCode fDeferredStatus
; // status if some prior error has left this
596 // RegexPattern in an unusable state.
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
603 int32_t fFrameSize
; // Size of a state stack frame in the
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.
610 UVector32
*fGroupMap
; // Map from capture group number to position of
611 // the group's variables in the matcher stack frame.
613 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
614 // regex character classes, e.g. Word.
616 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
617 // sets for predefined regex classes.
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
;
627 UHashtable
*fNamedCaptureMap
; // Map from capture group names to numbers.
629 friend class RegexCompile
;
630 friend class RegexMatcher
;
631 friend class RegexCImpl
;
634 // Implementation Methods
636 void init(); // Common initialization, for use by constructors.
637 void zap(); // Common cleanup
639 void dumpOp(int32_t index
) const;
642 #ifndef U_HIDE_INTERNAL_API
644 * Dump a compiled pattern. Internal debug function.
647 void dumpPattern() const;
648 #endif /* U_HIDE_INTERNAL_API */
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.
658 * <p>Class RegexMatcher is not intended to be subclassed.</p>
662 class U_I18N_API RegexMatcher U_FINAL
: public UObject
{
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.
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.
679 RegexMatcher(const UnicodeString
®exp
, uint32_t flags
, UErrorCode
&status
);
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.
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.
696 RegexMatcher(UText
*regexp
, uint32_t flags
, UErrorCode
&status
);
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.
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.
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.
719 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
720 uint32_t flags
, UErrorCode
&status
);
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.
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.
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.
743 RegexMatcher(UText
*regexp
, UText
*input
,
744 uint32_t flags
, UErrorCode
&status
);
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.
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>
759 RegexMatcher(const UnicodeString
®exp
, const UChar
*input
,
760 uint32_t flags
, UErrorCode
&status
);
769 virtual ~RegexMatcher();
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
778 virtual UBool
matches(UErrorCode
&status
);
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
791 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
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.
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>
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.
807 virtual UBool
lookingAt(UErrorCode
&status
);
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().
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>
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.
823 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
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.
838 virtual UBool
find();
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.
854 virtual UBool
find(UErrorCode
&status
);
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.
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.
865 virtual UBool
find(int64_t start
, UErrorCode
&status
);
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.
877 virtual UnicodeString
group(UErrorCode
&status
) const;
881 * Returns a string containing the text captured by the given group
882 * during the previous match operation. Group(0) is the entire match.
884 * A zero length string is returned both for capture groups that did not
885 * participate in the match and for actual zero length matches.
886 * To distinguish between these two cases use the function start(),
887 * which returns -1 for non-participating groups.
889 * @param groupNum the capture group number
890 * @param status A reference to a UErrorCode to receive any errors.
891 * Possible errors are U_REGEX_INVALID_STATE if no match
892 * has been attempted or the last match failed and
893 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
894 * @return the captured text
897 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
900 * Returns the number of capturing groups in this matcher's pattern.
901 * @return the number of capture groups
904 virtual int32_t groupCount() const;
908 * Returns a shallow clone of the entire live input string with the UText current native index
909 * set to the beginning of the requested group.
911 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
912 * @param group_len A reference to receive the length of the desired capture group
913 * @param status A reference to a UErrorCode to receive any errors.
914 * Possible errors are U_REGEX_INVALID_STATE if no match
915 * has been attempted or the last match failed and
916 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
917 * @return dest if non-NULL, a shallow copy of the input text otherwise
921 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
924 * Returns a shallow clone of the entire live input string with the UText current native index
925 * set to the beginning of the requested group.
927 * A group length of zero is returned both for capture groups that did not
928 * participate in the match and for actual zero length matches.
929 * To distinguish between these two cases use the function start(),
930 * which returns -1 for non-participating groups.
932 * @param groupNum The capture group number.
933 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
934 * @param group_len A reference to receive the length of the desired capture group
935 * @param status A reference to a UErrorCode to receive any errors.
936 * Possible errors are U_REGEX_INVALID_STATE if no match
937 * has been attempted or the last match failed and
938 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
939 * @return dest if non-NULL, a shallow copy of the input text otherwise
943 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
946 * Returns the index in the input string of the start of the text matched
947 * during the previous match operation.
948 * @param status a reference to a UErrorCode to receive any errors.
949 * @return The (native) position in the input string of the start of the last match.
952 virtual int32_t start(UErrorCode
&status
) const;
955 * Returns the index in the input string of the start of the text matched
956 * during the previous match operation.
957 * @param status a reference to a UErrorCode to receive any errors.
958 * @return The (native) position in the input string of the start of the last match.
961 virtual int64_t start64(UErrorCode
&status
) const;
965 * Returns the index in the input string of the start of the text matched by the
966 * specified capture group during the previous match operation. Return -1 if
967 * the capture group exists in the pattern, but was not part of the last match.
969 * @param group the capture group number
970 * @param status A reference to a UErrorCode to receive any errors. Possible
971 * errors are U_REGEX_INVALID_STATE if no match has been
972 * attempted or the last match failed, and
973 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
974 * @return the (native) start position of substring matched by the specified group.
977 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
980 * Returns the index in the input string of the start of the text matched by the
981 * specified capture group during the previous match operation. Return -1 if
982 * the capture group exists in the pattern, but was not part of the last match.
984 * @param group the capture group number.
985 * @param status A reference to a UErrorCode to receive any errors. Possible
986 * errors are U_REGEX_INVALID_STATE if no match has been
987 * attempted or the last match failed, and
988 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
989 * @return the (native) start position of substring matched by the specified group.
992 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
995 * Returns the index in the input string of the first character following the
996 * text matched during the previous match operation.
998 * @param status A reference to a UErrorCode to receive any errors. Possible
999 * errors are U_REGEX_INVALID_STATE if no match has been
1000 * attempted or the last match failed.
1001 * @return the index of the last character matched, plus one.
1002 * The index value returned is a native index, corresponding to
1003 * code units for the underlying encoding type, for example,
1004 * a byte index for UTF-8.
1007 virtual int32_t end(UErrorCode
&status
) const;
1010 * Returns the index in the input string of the first character following the
1011 * text matched during the previous match operation.
1013 * @param status A reference to a UErrorCode to receive any errors. Possible
1014 * errors are U_REGEX_INVALID_STATE if no match has been
1015 * attempted or the last match failed.
1016 * @return the index of the last character matched, plus one.
1017 * The index value returned is a native index, corresponding to
1018 * code units for the underlying encoding type, for example,
1019 * a byte index for UTF-8.
1022 virtual int64_t end64(UErrorCode
&status
) const;
1026 * Returns the index in the input string of the character following the
1027 * text matched by the specified capture group during the previous match operation.
1029 * @param group the capture group number
1030 * @param status A reference to a UErrorCode to receive any errors. Possible
1031 * errors are U_REGEX_INVALID_STATE if no match has been
1032 * attempted or the last match failed and
1033 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1034 * @return the index of the first character following the text
1035 * captured by the specified group during the previous match operation.
1036 * Return -1 if the capture group exists in the pattern but was not part of the match.
1037 * The index value returned is a native index, corresponding to
1038 * code units for the underlying encoding type, for example,
1039 * a byte index for UTF8.
1042 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
1045 * Returns the index in the input string of the character following the
1046 * text matched by the specified capture group during the previous match operation.
1048 * @param group the capture group number
1049 * @param status A reference to a UErrorCode to receive any errors. Possible
1050 * errors are U_REGEX_INVALID_STATE if no match has been
1051 * attempted or the last match failed and
1052 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1053 * @return the index of the first character following the text
1054 * captured by the specified group during the previous match operation.
1055 * Return -1 if the capture group exists in the pattern but was not part of the match.
1056 * The index value returned is a native index, corresponding to
1057 * code units for the underlying encoding type, for example,
1058 * a byte index for UTF8.
1061 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
1064 * Resets this matcher. The effect is to remove any memory of previous matches,
1065 * and to cause subsequent find() operations to begin at the beginning of
1068 * @return this RegexMatcher.
1071 virtual RegexMatcher
&reset();
1075 * Resets this matcher, and set the current input position.
1076 * The effect is to remove any memory of previous matches,
1077 * and to cause subsequent find() operations to begin at
1078 * the specified (native) position in the input string.
1080 * The matcher's region is reset to its default, which is the entire
1083 * An alternative to this function is to set a match region
1084 * beginning at the desired index.
1086 * @return this RegexMatcher.
1089 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1093 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1094 * to be reused, which is more efficient than creating a new RegexMatcher for
1095 * each input string to be processed.
1096 * @param input The new string on which subsequent pattern matches will operate.
1097 * The matcher retains a reference to the callers string, and operates
1098 * directly on that. Ownership of the string remains with the caller.
1099 * Because no copy of the string is made, it is essential that the
1100 * caller not delete the string until after regexp operations on it
1102 * Note that while a reset on the matcher with an input string that is then
1103 * modified across/during matcher operations may be supported currently for UnicodeString,
1104 * this was not originally intended behavior, and support for this is not guaranteed
1105 * in upcoming versions of ICU.
1106 * @return this RegexMatcher.
1109 virtual RegexMatcher
&reset(const UnicodeString
&input
);
1113 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1114 * to be reused, which is more efficient than creating a new RegexMatcher for
1115 * each input string to be processed.
1116 * @param input The new string on which subsequent pattern matches will operate.
1117 * The matcher makes a shallow clone of the given text; ownership of the
1118 * original string remains with the caller. Because no deep copy of the
1119 * text is made, it is essential that the caller not modify the string
1120 * until after regexp operations on it are done.
1121 * @return this RegexMatcher.
1125 virtual RegexMatcher
&reset(UText
*input
);
1129 * Set the subject text string upon which the regular expression is looking for matches
1130 * without changing any other aspect of the matching state.
1131 * The new and previous text strings must have the same content.
1133 * This function is intended for use in environments where ICU is operating on
1134 * strings that may move around in memory. It provides a mechanism for notifying
1135 * ICU that the string has been relocated, and providing a new UText to access the
1136 * string in its new position.
1138 * Note that the regular expression implementation never copies the underlying text
1139 * of a string being matched, but always operates directly on the original text
1140 * provided by the user. Refreshing simply drops the references to the old text
1141 * and replaces them with references to the new.
1143 * Caution: this function is normally used only by very specialized,
1144 * system-level code. One example use case is with garbage collection that moves
1145 * the text in memory.
1147 * @param input The new (moved) text string.
1148 * @param status Receives errors detected by this function.
1152 virtual RegexMatcher
&refreshInputText(UText
*input
, UErrorCode
&status
);
1156 * Cause a compilation error if an application accidentally attempts to
1157 * reset a matcher with a (UChar *) string as input rather than
1158 * a UnicodeString. Avoids a dangling reference to a temporary string.
1160 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
1161 * using one of the aliasing constructors, such as
1162 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
1163 * or in a UText, using
1164 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
1167 RegexMatcher
&reset(const UChar
*input
);
1171 * Returns the input string being matched. Ownership of the string belongs to
1172 * the matcher; it should not be altered or deleted. This method will work even if the input
1173 * was originally supplied as a UText.
1174 * @return the input string
1177 virtual const UnicodeString
&input() const;
1180 * Returns the input string being matched. This is the live input text; it should not be
1181 * altered or deleted. This method will work even if the input was originally supplied as
1183 * @return the input text
1187 virtual UText
*inputText() const;
1190 * Returns the input string being matched, either by copying it into the provided
1191 * UText parameter or by returning a shallow clone of the live input. Note that copying
1192 * the entire input may cause significant performance and memory issues.
1193 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1194 * @param status error code
1195 * @return dest if non-NULL, a shallow copy of the input text otherwise
1199 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1202 /** Sets the limits of this matcher's region.
1203 * The region is the part of the input string that will be searched to find a match.
1204 * Invoking this method resets the matcher, and then sets the region to start
1205 * at the index specified by the start parameter and end at the index specified
1206 * by the end parameter.
1208 * Depending on the transparency and anchoring being used (see useTransparentBounds
1209 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1210 * at or around the boundaries of the region
1212 * The function will fail if start is greater than limit, or if either index
1213 * is less than zero or greater than the length of the string being matched.
1215 * @param start The (native) index to begin searches at.
1216 * @param limit The index to end searches at (exclusive).
1217 * @param status A reference to a UErrorCode to receive any errors.
1220 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1223 * Identical to region(start, limit, status) but also allows a start position without
1224 * resetting the region state.
1225 * @param regionStart The region start
1226 * @param regionLimit the limit of the region
1227 * @param startIndex The (native) index within the region bounds at which to begin searches.
1228 * @param status A reference to a UErrorCode to receive any errors.
1229 * If startIndex is not within the specified region bounds,
1230 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1233 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
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).
1240 * @return The starting (native) index of this matcher's region.
1243 virtual int32_t regionStart() const;
1246 * Reports the start index of this matcher's region. The searches this matcher
1247 * conducts are limited to finding matches within regionStart (inclusive) and
1248 * regionEnd (exclusive).
1250 * @return The starting (native) index of this matcher's region.
1253 virtual int64_t regionStart64() const;
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).
1261 * @return The ending point (native) of this matcher's region.
1264 virtual int32_t regionEnd() const;
1267 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1268 * this matcher conducts are limited to finding matches within regionStart
1269 * (inclusive) and regionEnd (exclusive).
1271 * @return The ending point (native) of this matcher's region.
1274 virtual int64_t regionEnd64() const;
1277 * Queries the transparency of region bounds for this matcher.
1278 * See useTransparentBounds for a description of transparent and opaque bounds.
1279 * By default, a matcher uses opaque region boundaries.
1281 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1284 virtual UBool
hasTransparentBounds() const;
1287 * Sets the transparency of region bounds for this matcher.
1288 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1289 * If the boolean argument is false, then opaque bounds will be used.
1291 * Using transparent bounds, the boundaries of this matcher's region are transparent
1292 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1293 * see text beyond the boundaries of the region while checking for a match.
1295 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1296 * lookbehind, and boundary matching constructs.
1298 * By default, a matcher uses opaque bounds.
1300 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1301 * @return This Matcher;
1304 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1308 * Return true if this matcher is using anchoring bounds.
1309 * By default, matchers use anchoring region bounds.
1311 * @return TRUE if this matcher is using anchoring bounds.
1314 virtual UBool
hasAnchoringBounds() const;
1318 * Set whether this matcher is using Anchoring Bounds for its region.
1319 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1320 * and end of the region. Without Anchoring Bounds, anchors will only match at
1321 * the positions they would in the complete text.
1323 * Anchoring Bounds are the default for regions.
1325 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1326 * @return This Matcher
1329 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1333 * Return TRUE if the most recent matching operation attempted to access
1334 * additional input beyond the available input text.
1335 * In this case, additional input text could change the results of the match.
1337 * hitEnd() is defined for both successful and unsuccessful matches.
1338 * In either case hitEnd() will return TRUE if if the end of the text was
1339 * reached at any point during the matching process.
1341 * @return TRUE if the most recent match hit the end of input
1344 virtual UBool
hitEnd() const;
1347 * Return TRUE the most recent match succeeded and additional input could cause
1348 * it to fail. If this method returns false and a match was found, then more input
1349 * might change the match but the match won't be lost. If a match was not found,
1350 * then requireEnd has no meaning.
1352 * @return TRUE if more input could cause the most recent match to no longer match.
1355 virtual UBool
requireEnd() const;
1359 * Returns the pattern that is interpreted by this matcher.
1360 * @return the RegexPattern for this RegexMatcher
1363 virtual const RegexPattern
&pattern() const;
1367 * Replaces every substring of the input that matches the pattern
1368 * with the given replacement string. This is a convenience function that
1369 * provides a complete find-and-replace-all operation.
1371 * This method first resets this matcher. It then scans the input string
1372 * looking for matches of the pattern. Input that is not part of any
1373 * match is left unchanged; each match is replaced in the result by the
1374 * replacement string. The replacement string may contain references to
1377 * @param replacement a string containing the replacement text.
1378 * @param status a reference to a UErrorCode to receive any errors.
1379 * @return a string containing the results of the find and replace.
1382 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
1386 * Replaces every substring of the input that matches the pattern
1387 * with the given replacement string. This is a convenience function that
1388 * provides a complete find-and-replace-all operation.
1390 * This method first resets this matcher. It then scans the input string
1391 * looking for matches of the pattern. Input that is not part of any
1392 * match is left unchanged; each match is replaced in the result by the
1393 * replacement string. The replacement string may contain references to
1396 * @param replacement a string containing the replacement text.
1397 * @param dest a mutable UText in which the results are placed.
1398 * If NULL, a new UText will be created (which may not be mutable).
1399 * @param status a reference to a UErrorCode to receive any errors.
1400 * @return a string containing the results of the find and replace.
1401 * If a pre-allocated UText was provided, it will always be used and returned.
1405 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1409 * Replaces the first substring of the input that matches
1410 * the pattern with the replacement string. This is a convenience
1411 * function that provides a complete find-and-replace operation.
1413 * <p>This function first resets this RegexMatcher. It then scans the input string
1414 * looking for a match of the pattern. Input that is not part
1415 * of the match is appended directly to the result string; the match is replaced
1416 * in the result by the replacement string. The replacement string may contain
1417 * references to captured groups.</p>
1419 * <p>The state of the matcher (the position at which a subsequent find()
1420 * would begin) after completing a replaceFirst() is not specified. The
1421 * RegexMatcher should be reset before doing additional find() operations.</p>
1423 * @param replacement a string containing the replacement text.
1424 * @param status a reference to a UErrorCode to receive any errors.
1425 * @return a string containing the results of the find and replace.
1428 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1432 * Replaces the first substring of the input that matches
1433 * the pattern with the replacement string. This is a convenience
1434 * function that provides a complete find-and-replace operation.
1436 * <p>This function first resets this RegexMatcher. It then scans the input string
1437 * looking for a match of the pattern. Input that is not part
1438 * of the match is appended directly to the result string; the match is replaced
1439 * in the result by the replacement string. The replacement string may contain
1440 * references to captured groups.</p>
1442 * <p>The state of the matcher (the position at which a subsequent find()
1443 * would begin) after completing a replaceFirst() is not specified. The
1444 * RegexMatcher should be reset before doing additional find() operations.</p>
1446 * @param replacement a string containing the replacement text.
1447 * @param dest a mutable UText in which the results are placed.
1448 * If NULL, a new UText will be created (which may not be mutable).
1449 * @param status a reference to a UErrorCode to receive any errors.
1450 * @return a string containing the results of the find and replace.
1451 * If a pre-allocated UText was provided, it will always be used and returned.
1455 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1459 * Implements a replace operation intended to be used as part of an
1460 * incremental find-and-replace.
1462 * <p>The input string, starting from the end of the previous replacement and ending at
1463 * the start of the current match, is appended to the destination string. Then the
1464 * replacement string is appended to the output string,
1465 * including handling any substitutions of captured text.</p>
1467 * <p>For simple, prepackaged, non-incremental find-and-replace
1468 * operations, see replaceFirst() or replaceAll().</p>
1470 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1471 * @param replacement A UnicodeString that provides the text to be substituted for
1472 * the input text that matched the regexp pattern. The replacement
1473 * text may contain references to captured text from the
1475 * @param status A reference to a UErrorCode to receive any errors. Possible
1476 * errors are U_REGEX_INVALID_STATE if no match has been
1477 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1478 * if the replacement text specifies a capture group that
1479 * does not exist in the pattern.
1481 * @return this RegexMatcher
1485 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1486 const UnicodeString
&replacement
, UErrorCode
&status
);
1490 * Implements a replace operation intended to be used as part of an
1491 * incremental find-and-replace.
1493 * <p>The input string, starting from the end of the previous replacement and ending at
1494 * the start of the current match, is appended to the destination string. Then the
1495 * replacement string is appended to the output string,
1496 * including handling any substitutions of captured text.</p>
1498 * <p>For simple, prepackaged, non-incremental find-and-replace
1499 * operations, see replaceFirst() or replaceAll().</p>
1501 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1503 * @param replacement A UText that provides the text to be substituted for
1504 * the input text that matched the regexp pattern. The replacement
1505 * text may contain references to captured text from the input.
1506 * @param status A reference to a UErrorCode to receive any errors. Possible
1507 * errors are U_REGEX_INVALID_STATE if no match has been
1508 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1509 * if the replacement text specifies a capture group that
1510 * does not exist in the pattern.
1512 * @return this RegexMatcher
1516 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1517 UText
*replacement
, UErrorCode
&status
);
1521 * As the final step in a find-and-replace operation, append the remainder
1522 * of the input string, starting at the position following the last appendReplacement(),
1523 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1524 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1526 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1527 * @return the destination string.
1530 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1534 * As the final step in a find-and-replace operation, append the remainder
1535 * of the input string, starting at the position following the last appendReplacement(),
1536 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1537 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1539 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1541 * @param status error cod
1542 * @return the destination string.
1546 virtual UText
*appendTail(UText
*dest
, 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 UnicodeStrings to receive the results of the split.
1559 * This is an array of actual UnicodeString objects, not an
1560 * array of pointers to strings. Local (stack based) arrays can
1562 * @param destCapacity The number of elements in the destination array.
1563 * If the number of fields found is less than destCapacity, the
1564 * extra strings in the destination array are not altered.
1565 * If the number of destination strings is less than the number
1566 * of fields, the trailing part of the input string, including any
1567 * field delimiters, is placed in the last destination string.
1568 * @param status A reference to a UErrorCode to receive any errors.
1569 * @return The number of fields into which the input string was split.
1572 virtual int32_t split(const UnicodeString
&input
,
1573 UnicodeString dest
[],
1574 int32_t destCapacity
,
1575 UErrorCode
&status
);
1579 * Split a string into fields. Somewhat like split() from Perl.
1580 * The pattern matches identify delimiters that separate the input
1581 * into fields. The input data between the matches becomes the
1582 * fields themselves.
1584 * @param input The string to be split into fields. The field delimiters
1585 * match the pattern (in the "this" object). This matcher
1586 * will be reset to this input string.
1587 * @param dest An array of mutable UText structs to receive the results of the split.
1588 * If a field is NULL, a new UText is allocated to contain the results for
1589 * that field. This new UText is not guaranteed to be mutable.
1590 * @param destCapacity The number of elements in the destination array.
1591 * If the number of fields found is less than destCapacity, the
1592 * extra strings in the destination array are not altered.
1593 * If the number of destination strings is less than the number
1594 * of fields, the trailing part of the input string, including any
1595 * field delimiters, is placed in the last destination string.
1596 * @param status A reference to a UErrorCode to receive any errors.
1597 * @return The number of fields into which the input string was split.
1601 virtual int32_t split(UText
*input
,
1603 int32_t destCapacity
,
1604 UErrorCode
&status
);
1607 * Set a processing time limit for match operations with this Matcher.
1609 * Some patterns, when matching certain strings, can run in exponential time.
1610 * For practical purposes, the match operation may appear to be in an
1612 * When a limit is set a match operation will fail with an error if the
1613 * limit is exceeded.
1615 * The units of the limit are steps of the match engine.
1616 * Correspondence with actual processor time will depend on the speed
1617 * of the processor and the details of the specific pattern, but will
1618 * typically be on the order of milliseconds.
1620 * By default, the matching time is not limited.
1623 * @param limit The limit value, or 0 for no limit.
1624 * @param status A reference to a UErrorCode to receive any errors.
1627 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1630 * Get the time limit, if any, for match operations made with this Matcher.
1632 * @return the maximum allowed time for a match, in units of processing steps.
1635 virtual int32_t getTimeLimit() const;
1638 * Set the amount of heap storage available for use by the match backtracking stack.
1639 * The matcher is also reset, discarding any results from previous matches.
1641 * ICU uses a backtracking regular expression engine, with the backtrack stack
1642 * maintained on the heap. This function sets the limit to the amount of memory
1643 * that can be used for this purpose. A backtracking stack overflow will
1644 * result in an error from the match operation that caused it.
1646 * A limit is desirable because a malicious or poorly designed pattern can use
1647 * excessive memory, potentially crashing the process. A limit is enabled
1650 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1651 * A value of zero means no limit.
1652 * The limit must be greater or equal to zero.
1654 * @param status A reference to a UErrorCode to receive any errors.
1658 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1661 * Get the size of the heap storage available for use by the back tracking stack.
1663 * @return the maximum backtracking stack size, in bytes, or zero if the
1664 * stack size is unlimited.
1667 virtual int32_t getStackLimit() const;
1671 * Set a callback function for use with this Matcher.
1672 * During matching operations the function will be called periodically,
1673 * giving the application the opportunity to terminate a long-running
1676 * @param callback A pointer to the user-supplied callback function.
1677 * @param context User context pointer. The value supplied at the
1678 * time the callback function is set will be saved
1679 * and passed to the callback each time that it is called.
1680 * @param status A reference to a UErrorCode to receive any errors.
1683 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1684 const void *context
,
1685 UErrorCode
&status
);
1689 * Get the callback function for this URegularExpression.
1691 * @param callback Out parameter, receives a pointer to the user-supplied
1692 * callback function.
1693 * @param context Out parameter, receives the user context pointer that
1694 * was set when uregex_setMatchCallback() was called.
1695 * @param status A reference to a UErrorCode to receive any errors.
1698 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1699 const void *&context
,
1700 UErrorCode
&status
);
1704 * Set a progress callback function for use with find operations on this Matcher.
1705 * During find operations, the callback will be invoked after each return from a
1706 * match attempt, giving the application the opportunity to terminate a long-running
1709 * @param callback A pointer to the user-supplied callback function.
1710 * @param context User context pointer. The value supplied at the
1711 * time the callback function is set will be saved
1712 * and passed to the callback each time that it is called.
1713 * @param status A reference to a UErrorCode to receive any errors.
1716 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1717 const void *context
,
1718 UErrorCode
&status
);
1722 * Get the find progress callback function for this URegularExpression.
1724 * @param callback Out parameter, receives a pointer to the user-supplied
1725 * callback function.
1726 * @param context Out parameter, receives the user context pointer that
1727 * was set when uregex_setFindProgressCallback() was called.
1728 * @param status A reference to a UErrorCode to receive any errors.
1731 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1732 const void *&context
,
1733 UErrorCode
&status
);
1735 #ifndef U_HIDE_INTERNAL_API
1737 * setTrace Debug function, enable/disable tracing of the matching engine.
1738 * For internal ICU development use only. DO NO USE!!!!
1741 void setTrace(UBool state
);
1742 #endif /* U_HIDE_INTERNAL_API */
1745 * ICU "poor man's RTTI", returns a UClassID for this class.
1749 static UClassID U_EXPORT2
getStaticClassID();
1752 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1756 virtual UClassID
getDynamicClassID() const;
1759 // Constructors and other object boilerplate are private.
1760 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1761 RegexMatcher(); // default constructor not implemented
1762 RegexMatcher(const RegexPattern
*pat
);
1763 RegexMatcher(const RegexMatcher
&other
);
1764 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1765 void init(UErrorCode
&status
); // Common initialization
1766 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1768 friend class RegexPattern
;
1769 friend class RegexCImpl
;
1771 #ifndef U_HIDE_INTERNAL_API
1773 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1774 #endif /* U_HIDE_INTERNAL_API */
1778 // MatchAt This is the internal interface to the match engine itself.
1779 // Match status comes back in matcher member variables.
1781 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1782 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1783 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1784 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1785 REStackFrame
*resetStack();
1786 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1787 void IncrementTime(UErrorCode
&status
);
1789 // Call user find callback function, if set. Return TRUE if operation should be interrupted.
1790 inline UBool
findProgressInterrupt(int64_t matchIndex
, UErrorCode
&status
);
1792 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1794 UBool
findUsingChunk(UErrorCode
&status
);
1795 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1796 UBool
isChunkWordBoundary(int32_t pos
);
1798 const RegexPattern
*fPattern
;
1799 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1800 // should delete it when through.
1802 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1803 UText
*fInputText
; // The text being matched. Is never NULL.
1804 UText
*fAltInputText
; // A shallow copy of the text being matched.
1805 // Only created if the pattern contains backreferences.
1806 int64_t fInputLength
; // Full length of the input text.
1807 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1809 int64_t fRegionStart
; // Start of the input region, default = 0.
1810 int64_t fRegionLimit
; // End of input region, default to input.length.
1812 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1813 int64_t fAnchorLimit
; // See useAnchoringBounds
1815 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1816 int64_t fLookLimit
; // and other boundary tests. See
1817 // useTransparentBounds
1819 int64_t fActiveStart
; // Currently active bounds for matching.
1820 int64_t fActiveLimit
; // Usually is the same as region, but
1821 // is changed to fLookStart/Limit when
1822 // entering look around regions.
1824 UBool fTransparentBounds
; // True if using transparent bounds.
1825 UBool fAnchoringBounds
; // True if using anchoring bounds.
1827 UBool fMatch
; // True if the last attempted match was successful.
1828 int64_t fMatchStart
; // Position of the start of the most recent match
1829 int64_t fMatchEnd
; // First position after the end of the most recent match
1830 // Zero if no previous match, even when a region
1832 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1833 // or -1 if there was no previous match.
1834 int64_t fAppendPosition
; // First position after the end of the previous
1835 // appendReplacement(). As described by the
1836 // JavaDoc for Java Matcher, where it is called
1837 // "append position"
1838 UBool fHitEnd
; // True if the last match touched the end of input.
1839 UBool fRequireEnd
; // True if the last match required end-of-input
1843 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1844 // which will contain the capture group results.
1845 // NOT valid while match engine is running.
1847 int64_t *fData
; // Data area for use by the compiled pattern.
1848 int64_t fSmallData
[8]; // Use this for data if it's enough.
1850 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1851 // match engine run. Zero for unlimited.
1853 int32_t fTime
; // Match time, accumulates while matching.
1854 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1855 // Kept separately from fTime to keep as much
1856 // code as possible out of the inline
1857 // StateSave function.
1859 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1860 // stack, in bytes. Zero for unlimited.
1862 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1863 // NULL if there is no callback.
1864 const void *fCallbackContext
; // User Context ptr for callback function.
1866 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1867 // NULL if there is no callback.
1868 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1871 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1873 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1875 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1876 // reported, or that permanently disables this matcher.
1878 RuleBasedBreakIterator
*fWordBreakItr
;
1882 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS