1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2002-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
12 * created on: 2002oct22
13 * created by: Andy Heninger
15 * ICU Regular Expressions, API for C++
25 * \brief C++ API: Regular Expressions
27 * <h2>Regular Expression API</h2>
29 * <p>The ICU API for processing regular expressions consists of two classes,
30 * <code>RegexPattern</code> and <code>RegexMatcher</code>.
31 * <code>RegexPattern</code> objects represent a pre-processed, or compiled
32 * regular expression. They are created from a regular expression pattern string,
33 * and can be used to create <code>RegexMatcher</code> objects for the pattern.</p>
35 * <p>Class <code>RegexMatcher</code> bundles together a regular expression
36 * pattern and a target string to which the search pattern will be applied.
37 * <code>RegexMatcher</code> includes API for doing plain find or search
38 * operations, for search and replace operations, and for obtaining detailed
39 * information about bounds of a match. </p>
41 * <p>Note that by constructing <code>RegexMatcher</code> objects directly from regular
42 * expression pattern strings application code can be simplified and the explicit
43 * need for <code>RegexPattern</code> objects can usually be eliminated.
47 #include "unicode/utypes.h"
49 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
51 #include "unicode/uobject.h"
52 #include "unicode/unistr.h"
53 #include "unicode/utext.h"
54 #include "unicode/parseerr.h"
56 #include "unicode/uregex.h"
58 // Forward Declarations
62 #if U_SHOW_CPLUSPLUS_API
70 class RuleBasedBreakIterator
;
78 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
79 * factory methods for creating a RegexPattern object from the source (string) form
80 * of a regular expression, methods for creating RegexMatchers that allow the pattern
81 * to be applied to input text, and a few convenience methods for simple common
82 * uses of regular expressions.
84 * <p>Class RegexPattern is not intended to be subclassed.</p>
88 class U_I18N_API RegexPattern U_FINAL
: public UObject
{
92 * default constructor. Create a RegexPattern object that refers to no actual
93 * pattern. Not normally needed; RegexPattern objects are usually
94 * created using the factory method <code>compile()</code>.
101 * Copy Constructor. Create a new RegexPattern object that is equivalent
102 * to the source object.
103 * @param source the pattern object to be copied.
106 RegexPattern(const RegexPattern
&source
);
109 * Destructor. Note that a RegexPattern object must persist so long as any
110 * RegexMatcher objects that were created from the RegexPattern are active.
113 virtual ~RegexPattern();
116 * Comparison operator. Two RegexPattern objects are considered equal if they
117 * were constructed from identical source patterns using the same match flag
119 * @param that a RegexPattern object to compare with "this".
120 * @return TRUE if the objects are equivalent.
123 UBool
operator==(const RegexPattern
& that
) const;
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 different.
133 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);}
136 * Assignment operator. After assignment, this RegexPattern will behave identically
137 * to the source object.
140 RegexPattern
&operator =(const RegexPattern
&source
);
143 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
144 * intended to be subclassed, <code>clone()</code> and the copy construction are
145 * equivalent operations.
146 * @return the copy of this RegexPattern
149 virtual RegexPattern
*clone() const;
153 * Compiles the regular expression in string form into a RegexPattern
154 * object. These compile methods, rather than the constructors, are the usual
155 * way that RegexPattern objects are created.
157 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
158 * objects created from the pattern are active. RegexMatchers keep a pointer
159 * back to their pattern, so premature deletion of the pattern is a
160 * catastrophic error.</p>
162 * <p>All pattern match mode flags are set to their default values.</p>
164 * <p>Note that it is often more convenient to construct a RegexMatcher directly
165 * from a pattern string rather than separately compiling the pattern and
166 * then creating a RegexMatcher object from the pattern.</p>
168 * @param regex The regular expression to be compiled.
169 * @param pe Receives the position (line and column nubers) of any error
170 * within the regular expression.)
171 * @param status A reference to a UErrorCode to receive any errors.
172 * @return A regexPattern object for the compiled pattern.
176 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
181 * Compiles the regular expression in string form into a RegexPattern
182 * object. These compile methods, rather than the constructors, are the usual
183 * way that RegexPattern objects are created.
185 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
186 * objects created from the pattern are active. RegexMatchers keep a pointer
187 * back to their pattern, so premature deletion of the pattern is a
188 * catastrophic error.</p>
190 * <p>All pattern match mode flags are set to their default values.</p>
192 * <p>Note that it is often more convenient to construct a RegexMatcher directly
193 * from a pattern string rather than separately compiling the pattern and
194 * then creating a RegexMatcher object from the pattern.</p>
196 * @param regex The regular expression to be compiled. Note, the text referred
197 * to by this UText must not be deleted during the lifetime of the
198 * RegexPattern object or any RegexMatcher object created from it.
199 * @param pe Receives the position (line and column nubers) of any error
200 * within the regular expression.)
201 * @param status A reference to a UErrorCode to receive any errors.
202 * @return A regexPattern object for the compiled pattern.
206 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
211 * Compiles the regular expression in string form into a RegexPattern
212 * object using the specified match mode flags. These compile methods,
213 * rather than the constructors, are the usual way that RegexPattern objects
216 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
217 * objects created from the pattern are active. RegexMatchers keep a pointer
218 * back to their pattern, so premature deletion of the pattern is a
219 * catastrophic error.</p>
221 * <p>Note that it is often more convenient to construct a RegexMatcher directly
222 * from a pattern string instead of than separately compiling the pattern and
223 * then creating a RegexMatcher object from the pattern.</p>
225 * @param regex The regular expression to be compiled.
226 * @param flags The match mode flags to be used.
227 * @param pe Receives the position (line and column numbers) of any error
228 * within the regular expression.)
229 * @param status A reference to a UErrorCode to receive any errors.
230 * @return A regexPattern object for the compiled pattern.
234 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
240 * Compiles the regular expression in string form into a RegexPattern
241 * object using the specified match mode flags. These compile methods,
242 * rather than the constructors, are the usual way that RegexPattern objects
245 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
246 * objects created from the pattern are active. RegexMatchers keep a pointer
247 * back to their pattern, so premature deletion of the pattern is a
248 * catastrophic error.</p>
250 * <p>Note that it is often more convenient to construct a RegexMatcher directly
251 * from a pattern string instead of than separately compiling the pattern and
252 * then creating a RegexMatcher object from the pattern.</p>
254 * @param regex The regular expression to be compiled. Note, the text referred
255 * to by this UText must not be deleted during the lifetime of the
256 * RegexPattern object or any RegexMatcher object created from it.
257 * @param flags The match mode flags to be used.
258 * @param pe Receives the position (line and column numbers) of any error
259 * within the regular expression.)
260 * @param status A reference to a UErrorCode to receive any errors.
261 * @return A regexPattern object for the compiled pattern.
265 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
271 * Compiles the regular expression in string form into a RegexPattern
272 * object using the specified match mode flags. These compile methods,
273 * rather than the constructors, are the usual way that RegexPattern objects
276 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
277 * objects created from the pattern are active. RegexMatchers keep a pointer
278 * back to their pattern, so premature deletion of the pattern is a
279 * catastrophic error.</p>
281 * <p>Note that it is often more convenient to construct a RegexMatcher directly
282 * from a pattern string instead of than separately compiling the pattern and
283 * then creating a RegexMatcher object from the pattern.</p>
285 * @param regex The regular expression to be compiled.
286 * @param flags The match mode flags to be used.
287 * @param status A reference to a UErrorCode to receive any errors.
288 * @return A regexPattern object for the compiled pattern.
292 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
297 * Compiles the regular expression in string form into a RegexPattern
298 * object using the specified match mode flags. These compile methods,
299 * rather than the constructors, are the usual way that RegexPattern objects
302 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
303 * objects created from the pattern are active. RegexMatchers keep a pointer
304 * back to their pattern, so premature deletion of the pattern is a
305 * catastrophic error.</p>
307 * <p>Note that it is often more convenient to construct a RegexMatcher directly
308 * from a pattern string instead of than separately compiling the pattern and
309 * then creating a RegexMatcher object from the pattern.</p>
311 * @param regex The regular expression to be compiled. Note, the text referred
312 * to by this UText must not be deleted during the lifetime of the
313 * RegexPattern object or any RegexMatcher object created from it.
314 * @param flags The match mode flags to be used.
315 * @param status A reference to a UErrorCode to receive any errors.
316 * @return A regexPattern object for the compiled pattern.
320 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
325 * Get the match mode flags that were used when compiling this pattern.
326 * @return the match mode flags
329 virtual uint32_t flags() const;
332 * Creates a RegexMatcher that will match the given input against this pattern. The
333 * RegexMatcher can then be used to perform match, find or replace operations
334 * on the input. Note that a RegexPattern object must not be deleted while
335 * RegexMatchers created from it still exist and might possibly be used again.
337 * The matcher will retain a reference to the supplied input string, and all regexp
338 * pattern matching operations happen directly on this original string. It is
339 * critical that the string not be altered or deleted before use by the regular
340 * expression operations is complete.
342 * @param input The input string to which the regular expression will be applied.
343 * @param status A reference to a UErrorCode to receive any errors.
344 * @return A RegexMatcher object for this pattern and input.
348 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
349 UErrorCode
&status
) const;
353 * Cause a compilation error if an application accidentally attempts to
354 * create a matcher with a (char16_t *) string as input rather than
355 * a UnicodeString. Avoids a dangling reference to a temporary string.
357 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
358 * using one of the aliasing constructors, such as
359 * <code>UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);</code>
360 * or in a UText, using
361 * <code>utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);</code>
364 RegexMatcher
*matcher(const char16_t *input
,
365 UErrorCode
&status
) const;
370 * Creates a RegexMatcher that will match against this pattern. The
371 * RegexMatcher can be used to perform match, find or replace operations.
372 * Note that a RegexPattern object must not be deleted while
373 * RegexMatchers created from it still exist and might possibly be used again.
375 * @param status A reference to a UErrorCode to receive any errors.
376 * @return A RegexMatcher object for this pattern and input.
380 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
384 * Test whether a string matches a regular expression. This convenience function
385 * both compiles the regular expression and applies it in a single operation.
386 * Note that if the same pattern needs to be applied repeatedly, this method will be
387 * less efficient than creating and reusing a RegexMatcher object.
389 * @param regex The regular expression
390 * @param input The string data to be matched
391 * @param pe Receives the position of any syntax errors within the regular expression
392 * @param status A reference to a UErrorCode to receive any errors.
393 * @return True if the regular expression exactly matches the full input string.
397 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
398 const UnicodeString
&input
,
403 * Test whether a string matches a regular expression. This convenience function
404 * both compiles the regular expression and applies it in a single operation.
405 * Note that if the same pattern needs to be applied repeatedly, this method will be
406 * less efficient than creating and reusing a RegexMatcher object.
408 * @param regex The regular expression
409 * @param input The string data to be matched
410 * @param pe Receives the position of any syntax errors within the regular expression
411 * @param status A reference to a UErrorCode to receive any errors.
412 * @return True if the regular expression exactly matches the full input string.
416 static UBool U_EXPORT2
matches(UText
*regex
,
422 * Returns the regular expression from which this pattern was compiled. This method will work
423 * even if the pattern was compiled from a UText.
425 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
426 * the returned string may no longer reflect the RegexPattern object.
429 virtual UnicodeString
pattern() const;
433 * Returns the regular expression from which this pattern was compiled. This method will work
434 * even if the pattern was compiled from a UnicodeString.
436 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
437 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
442 virtual UText
*patternText(UErrorCode
&status
) const;
446 * Get the group number corresponding to a named capture group.
447 * The returned number can be used with any function that access
448 * capture groups by number.
450 * The function returns an error status if the specified name does not
451 * appear in the pattern.
453 * @param groupName The capture group name.
454 * @param status A UErrorCode to receive any errors.
458 virtual int32_t groupNumberFromName(const UnicodeString
&groupName
, UErrorCode
&status
) const;
462 * Get the group number corresponding to a named capture group.
463 * The returned number can be used with any function that access
464 * capture groups by number.
466 * The function returns an error status if the specified name does not
467 * appear in the pattern.
469 * @param groupName The capture group name,
470 * platform invariant characters only.
471 * @param nameLength The length of the name, or -1 if the name is
473 * @param status A UErrorCode to receive any errors.
477 virtual int32_t groupNumberFromName(const char *groupName
, int32_t nameLength
, UErrorCode
&status
) const;
481 * Split a string into fields. Somewhat like split() from Perl or Java.
482 * Pattern matches identify delimiters that separate the input
483 * into fields. The input data between the delimiters becomes the
486 * If the delimiter pattern includes capture groups, the captured text will
487 * also appear in the destination array of output strings, interspersed
488 * with the fields. This is similar to Perl, but differs from Java,
489 * which ignores the presence of capture groups in the pattern.
491 * Trailing empty fields will always be returned, assuming sufficient
492 * destination capacity. This differs from the default behavior for Java
493 * and Perl where trailing empty fields are not returned.
495 * The number of strings produced by the split operation is returned.
496 * This count includes the strings from capture groups in the delimiter pattern.
497 * This behavior differs from Java, which ignores capture groups.
499 * For the best performance on split() operations,
500 * <code>RegexMatcher::split</code> is preferable to this function
502 * @param input The string to be split into fields. The field delimiters
503 * match the pattern (in the "this" object)
504 * @param dest An array of UnicodeStrings to receive the results of the split.
505 * This is an array of actual UnicodeString objects, not an
506 * array of pointers to strings. Local (stack based) arrays can
508 * @param destCapacity The number of elements in the destination array.
509 * If the number of fields found is less than destCapacity, the
510 * extra strings in the destination array are not altered.
511 * If the number of destination strings is less than the number
512 * of fields, the trailing part of the input string, including any
513 * field delimiters, is placed in the last destination string.
514 * @param status A reference to a UErrorCode to receive any errors.
515 * @return The number of fields into which the input string was split.
518 virtual int32_t split(const UnicodeString
&input
,
519 UnicodeString dest
[],
520 int32_t destCapacity
,
521 UErrorCode
&status
) const;
525 * Split a string into fields. Somewhat like split() from Perl or Java.
526 * Pattern matches identify delimiters that separate the input
527 * into fields. The input data between the delimiters becomes the
530 * If the delimiter pattern includes capture groups, the captured text will
531 * also appear in the destination array of output strings, interspersed
532 * with the fields. This is similar to Perl, but differs from Java,
533 * which ignores the presence of capture groups in the pattern.
535 * Trailing empty fields will always be returned, assuming sufficient
536 * destination capacity. This differs from the default behavior for Java
537 * and Perl where trailing empty fields are not returned.
539 * The number of strings produced by the split operation is returned.
540 * This count includes the strings from capture groups in the delimiter pattern.
541 * This behavior differs from Java, which ignores capture groups.
543 * For the best performance on split() operations,
544 * <code>RegexMatcher::split</code> is preferable to this function
546 * @param input The string to be split into fields. The field delimiters
547 * match the pattern (in the "this" object)
548 * @param dest An array of mutable UText structs to receive the results of the split.
549 * If a field is NULL, a new UText is allocated to contain the results for
550 * that field. This new UText is not guaranteed to be mutable.
551 * @param destCapacity The number of elements in the destination array.
552 * If the number of fields found is less than destCapacity, the
553 * extra strings in the destination array are not altered.
554 * If the number of destination strings is less than the number
555 * of fields, the trailing part of the input string, including any
556 * field delimiters, is placed in the last destination string.
557 * @param status A reference to a UErrorCode to receive any errors.
558 * @return The number of destination strings used.
562 virtual int32_t split(UText
*input
,
564 int32_t destCapacity
,
565 UErrorCode
&status
) const;
569 * ICU "poor man's RTTI", returns a UClassID for the actual class.
573 virtual UClassID
getDynamicClassID() const;
576 * ICU "poor man's RTTI", returns a UClassID for this class.
580 static UClassID U_EXPORT2
getStaticClassID();
584 // Implementation Data
586 UText
*fPattern
; // The original pattern string.
587 UnicodeString
*fPatternString
; // The original pattern UncodeString if relevant
588 uint32_t fFlags
; // The flags used when compiling the pattern.
590 UVector64
*fCompiledPat
; // The compiled pattern p-code.
591 UnicodeString fLiteralText
; // Any literal string data from the pattern,
592 // after un-escaping, for use during the match.
594 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
595 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
598 UErrorCode fDeferredStatus
; // status if some prior error has left this
599 // RegexPattern in an unusable state.
601 int32_t fMinMatchLen
; // Minimum Match Length. All matches will have length
602 // >= this value. For some patterns, this calculated
603 // value may be less than the true shortest
606 int32_t fFrameSize
; // Size of a state stack frame in the
609 int32_t fDataSize
; // The size of the data needed by the pattern that
610 // does not go on the state stack, but has just
611 // a single copy per matcher.
613 UVector32
*fGroupMap
; // Map from capture group number to position of
614 // the group's variables in the matcher stack frame.
616 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
617 // regex character classes, e.g. Word.
619 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
620 // sets for predefined regex classes.
622 int32_t fStartType
; // Info on how a match must start.
623 int32_t fInitialStringIdx
; //
624 int32_t fInitialStringLen
;
625 UnicodeSet
*fInitialChars
;
626 UChar32 fInitialChar
;
627 Regex8BitSet
*fInitialChars8
;
628 UBool fNeedsAltInput
;
630 UHashtable
*fNamedCaptureMap
; // Map from capture group names to numbers.
632 friend class RegexCompile
;
633 friend class RegexMatcher
;
634 friend class RegexCImpl
;
637 // Implementation Methods
639 void init(); // Common initialization, for use by constructors.
640 void zap(); // Common cleanup
642 void dumpOp(int32_t index
) const;
645 #ifndef U_HIDE_INTERNAL_API
647 * Dump a compiled pattern. Internal debug function.
650 void dumpPattern() const;
651 #endif /* U_HIDE_INTERNAL_API */
657 * class RegexMatcher bundles together a regular expression pattern and
658 * input text to which the expression can be applied. It includes methods
659 * for testing for matches, and for find and replace operations.
661 * <p>Class RegexMatcher is not intended to be subclassed.</p>
665 class U_I18N_API RegexMatcher U_FINAL
: public UObject
{
669 * Construct a RegexMatcher for a regular expression.
670 * This is a convenience method that avoids the need to explicitly create
671 * a RegexPattern object. Note that if several RegexMatchers need to be
672 * created for the same expression, it will be more efficient to
673 * separately create and cache a RegexPattern object, and use
674 * its matcher() method to create the RegexMatcher objects.
676 * @param regexp The Regular Expression to be compiled.
677 * @param flags Regular expression options, such as case insensitive matching.
678 * @see UREGEX_CASE_INSENSITIVE
679 * @param status Any errors are reported by setting this UErrorCode variable.
682 RegexMatcher(const UnicodeString
®exp
, uint32_t flags
, UErrorCode
&status
);
685 * Construct a RegexMatcher for a regular expression.
686 * This is a convenience method that avoids the need to explicitly create
687 * a RegexPattern object. Note that if several RegexMatchers need to be
688 * created for the same expression, it will be more efficient to
689 * separately create and cache a RegexPattern object, and use
690 * its matcher() method to create the RegexMatcher objects.
692 * @param regexp The regular expression to be compiled.
693 * @param flags Regular expression options, such as case insensitive matching.
694 * @see UREGEX_CASE_INSENSITIVE
695 * @param status Any errors are reported by setting this UErrorCode variable.
699 RegexMatcher(UText
*regexp
, uint32_t flags
, UErrorCode
&status
);
702 * Construct a RegexMatcher for a regular expression.
703 * This is a convenience method that avoids the need to explicitly create
704 * a RegexPattern object. Note that if several RegexMatchers need to be
705 * created for the same expression, it will be more efficient to
706 * separately create and cache a RegexPattern object, and use
707 * its matcher() method to create the RegexMatcher objects.
709 * The matcher will retain a reference to the supplied input string, and all regexp
710 * pattern matching operations happen directly on the original string. It is
711 * critical that the string not be altered or deleted before use by the regular
712 * expression operations is complete.
714 * @param regexp The Regular Expression to be compiled.
715 * @param input The string to match. The matcher retains a reference to the
716 * caller's string; mo copy is made.
717 * @param flags Regular expression options, such as case insensitive matching.
718 * @see UREGEX_CASE_INSENSITIVE
719 * @param status Any errors are reported by setting this UErrorCode variable.
722 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
723 uint32_t flags
, UErrorCode
&status
);
726 * Construct a RegexMatcher for a regular expression.
727 * This is a convenience method that avoids the need to explicitly create
728 * a RegexPattern object. Note that if several RegexMatchers need to be
729 * created for the same expression, it will be more efficient to
730 * separately create and cache a RegexPattern object, and use
731 * its matcher() method to create the RegexMatcher objects.
733 * The matcher will make a shallow clone of the supplied input text, and all regexp
734 * pattern matching operations happen on this clone. While read-only operations on
735 * the supplied text are permitted, it is critical that the underlying string not be
736 * altered or deleted before use by the regular expression operations is complete.
738 * @param regexp The Regular Expression to be compiled.
739 * @param input The string to match. The matcher retains a shallow clone of the text.
740 * @param flags Regular expression options, such as case insensitive matching.
741 * @see UREGEX_CASE_INSENSITIVE
742 * @param status Any errors are reported by setting this UErrorCode variable.
746 RegexMatcher(UText
*regexp
, UText
*input
,
747 uint32_t flags
, UErrorCode
&status
);
751 * Cause a compilation error if an application accidentally attempts to
752 * create a matcher with a (char16_t *) string as input rather than
753 * a UnicodeString. Avoids a dangling reference to a temporary string.
755 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
756 * using one of the aliasing constructors, such as
757 * <code>UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);</code>
758 * or in a UText, using
759 * <code>utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);</code>
762 RegexMatcher(const UnicodeString
®exp
, const char16_t *input
,
763 uint32_t flags
, UErrorCode
&status
);
772 virtual ~RegexMatcher();
776 * Attempts to match the entire input region against the pattern.
777 * @param status A reference to a UErrorCode to receive any errors.
778 * @return TRUE if there is a match
781 virtual UBool
matches(UErrorCode
&status
);
785 * Resets the matcher, then attempts to match the input beginning
786 * at the specified startIndex, and extending to the end of the input.
787 * The input region is reset to include the entire input string.
788 * A successful match must extend to the end of the input.
789 * @param startIndex The input string (native) index at which to begin matching.
790 * @param status A reference to a UErrorCode to receive any errors.
791 * @return TRUE if there is a match
794 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
798 * Attempts to match the input string, starting from the beginning of the region,
799 * against the pattern. Like the matches() method, this function
800 * always starts at the beginning of the input region;
801 * unlike that function, it does not require that the entire region be matched.
803 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
804 * <code>end()</code>, and <code>group()</code> functions.</p>
806 * @param status A reference to a UErrorCode to receive any errors.
807 * @return TRUE if there is a match at the start of the input string.
810 virtual UBool
lookingAt(UErrorCode
&status
);
814 * Attempts to match the input string, starting from the specified index, against the pattern.
815 * The match may be of any length, and is not required to extend to the end
816 * of the input string. Contrast with match().
818 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
819 * <code>end()</code>, and <code>group()</code> functions.</p>
821 * @param startIndex The input string (native) index at which to begin matching.
822 * @param status A reference to a UErrorCode to receive any errors.
823 * @return TRUE if there is a match.
826 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
830 * Find the next pattern match in the input string.
831 * The find begins searching the input at the location following the end of
832 * the previous match, or at the start of the string if there is no previous match.
833 * If a match is found, <code>start(), end()</code> and <code>group()</code>
834 * will provide more information regarding the match.
835 * <p>Note that if the input string is changed by the application,
836 * use find(startPos, status) instead of find(), because the saved starting
837 * position may not be valid with the altered input string.</p>
838 * @return TRUE if a match is found.
841 virtual UBool
find();
845 * Find the next pattern match in the input string.
846 * The find begins searching the input at the location following the end of
847 * the previous match, or at the start of the string if there is no previous match.
848 * If a match is found, <code>start(), end()</code> and <code>group()</code>
849 * will provide more information regarding the match.
850 * <p>Note that if the input string is changed by the application,
851 * use find(startPos, status) instead of find(), because the saved starting
852 * position may not be valid with the altered input string.</p>
853 * @param status A reference to a UErrorCode to receive any errors.
854 * @return TRUE if a match is found.
857 virtual UBool
find(UErrorCode
&status
);
860 * Resets this RegexMatcher and then attempts to find the next substring of the
861 * input string that matches the pattern, starting at the specified index.
863 * @param start The (native) index in the input string to begin the search.
864 * @param status A reference to a UErrorCode to receive any errors.
865 * @return TRUE if a match is found.
868 virtual UBool
find(int64_t start
, UErrorCode
&status
);
872 * Returns a string containing the text matched by the previous match.
873 * If the pattern can match an empty string, an empty string may be returned.
874 * @param status A reference to a UErrorCode to receive any errors.
875 * Possible errors are U_REGEX_INVALID_STATE if no match
876 * has been attempted or the last match failed.
877 * @return a string containing the matched input text.
880 virtual UnicodeString
group(UErrorCode
&status
) const;
884 * Returns a string containing the text captured by the given group
885 * during the previous match operation. Group(0) is the entire match.
887 * A zero length string is returned both for capture groups that did not
888 * participate in the match and for actual zero length matches.
889 * To distinguish between these two cases use the function start(),
890 * which returns -1 for non-participating groups.
892 * @param groupNum the capture group number
893 * @param status A reference to a UErrorCode to receive any errors.
894 * Possible errors are U_REGEX_INVALID_STATE if no match
895 * has been attempted or the last match failed and
896 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
897 * @return the captured text
900 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
903 * Returns the number of capturing groups in this matcher's pattern.
904 * @return the number of capture groups
907 virtual int32_t groupCount() const;
911 * Returns a shallow clone of the entire live input string with the UText current native index
912 * set to the beginning of the requested group.
914 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
915 * @param group_len A reference to receive the length of the desired capture group
916 * @param status A reference to a UErrorCode to receive any errors.
917 * Possible errors are U_REGEX_INVALID_STATE if no match
918 * has been attempted or the last match failed and
919 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
920 * @return dest if non-NULL, a shallow copy of the input text otherwise
924 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
927 * Returns a shallow clone of the entire live input string with the UText current native index
928 * set to the beginning of the requested group.
930 * A group length of zero is returned both for capture groups that did not
931 * participate in the match and for actual zero length matches.
932 * To distinguish between these two cases use the function start(),
933 * which returns -1 for non-participating groups.
935 * @param groupNum The capture group number.
936 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
937 * @param group_len A reference to receive the length of the desired capture group
938 * @param status A reference to a UErrorCode to receive any errors.
939 * Possible errors are U_REGEX_INVALID_STATE if no match
940 * has been attempted or the last match failed and
941 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
942 * @return dest if non-NULL, a shallow copy of the input text otherwise
946 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
949 * Returns the index in the input string of the start of the text matched
950 * during the previous match operation.
951 * @param status a reference to a UErrorCode to receive any errors.
952 * @return The (native) position in the input string of the start of the last match.
955 virtual int32_t start(UErrorCode
&status
) const;
958 * Returns the index in the input string of the start of the text matched
959 * during the previous match operation.
960 * @param status a reference to a UErrorCode to receive any errors.
961 * @return The (native) position in the input string of the start of the last match.
964 virtual int64_t start64(UErrorCode
&status
) const;
968 * Returns the index in the input string of the start of the text matched by the
969 * specified capture group during the previous match operation. Return -1 if
970 * the capture group exists in the pattern, but was not part of the last match.
972 * @param group the capture group number
973 * @param status A reference to a UErrorCode to receive any errors. Possible
974 * errors are U_REGEX_INVALID_STATE if no match has been
975 * attempted or the last match failed, and
976 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
977 * @return the (native) start position of substring matched by the specified group.
980 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
983 * Returns the index in the input string of the start of the text matched by the
984 * specified capture group during the previous match operation. Return -1 if
985 * the capture group exists in the pattern, but was not part of the last match.
987 * @param group the capture group number.
988 * @param status A reference to a UErrorCode to receive any errors. Possible
989 * errors are U_REGEX_INVALID_STATE if no match has been
990 * attempted or the last match failed, and
991 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
992 * @return the (native) start position of substring matched by the specified group.
995 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
998 * Returns the index in the input string of the first character following the
999 * text matched during the previous match operation.
1001 * @param status A reference to a UErrorCode to receive any errors. Possible
1002 * errors are U_REGEX_INVALID_STATE if no match has been
1003 * attempted or the last match failed.
1004 * @return the index of the last character matched, plus one.
1005 * The index value returned is a native index, corresponding to
1006 * code units for the underlying encoding type, for example,
1007 * a byte index for UTF-8.
1010 virtual int32_t end(UErrorCode
&status
) const;
1013 * Returns the index in the input string of the first character following the
1014 * text matched during the previous match operation.
1016 * @param status A reference to a UErrorCode to receive any errors. Possible
1017 * errors are U_REGEX_INVALID_STATE if no match has been
1018 * attempted or the last match failed.
1019 * @return the index of the last character matched, plus one.
1020 * The index value returned is a native index, corresponding to
1021 * code units for the underlying encoding type, for example,
1022 * a byte index for UTF-8.
1025 virtual int64_t end64(UErrorCode
&status
) const;
1029 * Returns the index in the input string of the character following the
1030 * text matched by the specified capture group during the previous match operation.
1032 * @param group the capture group number
1033 * @param status A reference to a UErrorCode to receive any errors. Possible
1034 * errors are U_REGEX_INVALID_STATE if no match has been
1035 * attempted or the last match failed and
1036 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1037 * @return the index of the first character following the text
1038 * captured by the specified group during the previous match operation.
1039 * Return -1 if the capture group exists in the pattern but was not part of the match.
1040 * The index value returned is a native index, corresponding to
1041 * code units for the underlying encoding type, for example,
1042 * a byte index for UTF8.
1045 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
1048 * Returns the index in the input string of the character following the
1049 * text matched by the specified capture group during the previous match operation.
1051 * @param group the capture group number
1052 * @param status A reference to a UErrorCode to receive any errors. Possible
1053 * errors are U_REGEX_INVALID_STATE if no match has been
1054 * attempted or the last match failed and
1055 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1056 * @return the index of the first character following the text
1057 * captured by the specified group during the previous match operation.
1058 * Return -1 if the capture group exists in the pattern but was not part of the match.
1059 * The index value returned is a native index, corresponding to
1060 * code units for the underlying encoding type, for example,
1061 * a byte index for UTF8.
1064 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
1067 * Resets this matcher. The effect is to remove any memory of previous matches,
1068 * and to cause subsequent find() operations to begin at the beginning of
1071 * @return this RegexMatcher.
1074 virtual RegexMatcher
&reset();
1078 * Resets this matcher, and set the current input position.
1079 * The effect is to remove any memory of previous matches,
1080 * and to cause subsequent find() operations to begin at
1081 * the specified (native) position in the input string.
1083 * The matcher's region is reset to its default, which is the entire
1086 * An alternative to this function is to set a match region
1087 * beginning at the desired index.
1089 * @return this RegexMatcher.
1092 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1096 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1097 * to be reused, which is more efficient than creating a new RegexMatcher for
1098 * each input string to be processed.
1099 * @param input The new string on which subsequent pattern matches will operate.
1100 * The matcher retains a reference to the callers string, and operates
1101 * directly on that. Ownership of the string remains with the caller.
1102 * Because no copy of the string is made, it is essential that the
1103 * caller not delete the string until after regexp operations on it
1105 * Note that while a reset on the matcher with an input string that is then
1106 * modified across/during matcher operations may be supported currently for UnicodeString,
1107 * this was not originally intended behavior, and support for this is not guaranteed
1108 * in upcoming versions of ICU.
1109 * @return this RegexMatcher.
1112 virtual RegexMatcher
&reset(const UnicodeString
&input
);
1116 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1117 * to be reused, which is more efficient than creating a new RegexMatcher for
1118 * each input string to be processed.
1119 * @param input The new string on which subsequent pattern matches will operate.
1120 * The matcher makes a shallow clone of the given text; ownership of the
1121 * original string remains with the caller. Because no deep copy of the
1122 * text is made, it is essential that the caller not modify the string
1123 * until after regexp operations on it are done.
1124 * @return this RegexMatcher.
1128 virtual RegexMatcher
&reset(UText
*input
);
1132 * Set the subject text string upon which the regular expression is looking for matches
1133 * without changing any other aspect of the matching state.
1134 * The new and previous text strings must have the same content.
1136 * This function is intended for use in environments where ICU is operating on
1137 * strings that may move around in memory. It provides a mechanism for notifying
1138 * ICU that the string has been relocated, and providing a new UText to access the
1139 * string in its new position.
1141 * Note that the regular expression implementation never copies the underlying text
1142 * of a string being matched, but always operates directly on the original text
1143 * provided by the user. Refreshing simply drops the references to the old text
1144 * and replaces them with references to the new.
1146 * Caution: this function is normally used only by very specialized,
1147 * system-level code. One example use case is with garbage collection that moves
1148 * the text in memory.
1150 * @param input The new (moved) text string.
1151 * @param status Receives errors detected by this function.
1155 virtual RegexMatcher
&refreshInputText(UText
*input
, UErrorCode
&status
);
1159 * Cause a compilation error if an application accidentally attempts to
1160 * reset a matcher with a (char16_t *) string as input rather than
1161 * a UnicodeString. Avoids a dangling reference to a temporary string.
1163 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
1164 * using one of the aliasing constructors, such as
1165 * <code>UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);</code>
1166 * or in a UText, using
1167 * <code>utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);</code>
1170 RegexMatcher
&reset(const char16_t *input
);
1174 * Returns the input string being matched. Ownership of the string belongs to
1175 * the matcher; it should not be altered or deleted. This method will work even if the input
1176 * was originally supplied as a UText.
1177 * @return the input string
1180 virtual const UnicodeString
&input() const;
1183 * Returns the input string being matched. This is the live input text; it should not be
1184 * altered or deleted. This method will work even if the input was originally supplied as
1186 * @return the input text
1190 virtual UText
*inputText() const;
1193 * Returns the input string being matched, either by copying it into the provided
1194 * UText parameter or by returning a shallow clone of the live input. Note that copying
1195 * the entire input may cause significant performance and memory issues.
1196 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1197 * @param status error code
1198 * @return dest if non-NULL, a shallow copy of the input text otherwise
1202 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1205 /** Sets the limits of this matcher's region.
1206 * The region is the part of the input string that will be searched to find a match.
1207 * Invoking this method resets the matcher, and then sets the region to start
1208 * at the index specified by the start parameter and end at the index specified
1209 * by the end parameter.
1211 * Depending on the transparency and anchoring being used (see useTransparentBounds
1212 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1213 * at or around the boundaries of the region
1215 * The function will fail if start is greater than limit, or if either index
1216 * is less than zero or greater than the length of the string being matched.
1218 * @param start The (native) index to begin searches at.
1219 * @param limit The index to end searches at (exclusive).
1220 * @param status A reference to a UErrorCode to receive any errors.
1223 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1226 * Identical to region(start, limit, status) but also allows a start position without
1227 * resetting the region state.
1228 * @param regionStart The region start
1229 * @param regionLimit the limit of the region
1230 * @param startIndex The (native) index within the region bounds at which to begin searches.
1231 * @param status A reference to a UErrorCode to receive any errors.
1232 * If startIndex is not within the specified region bounds,
1233 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1236 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
1239 * Reports the start index of this matcher's region. The searches this matcher
1240 * conducts are limited to finding matches within regionStart (inclusive) and
1241 * regionEnd (exclusive).
1243 * @return The starting (native) index of this matcher's region.
1246 virtual int32_t regionStart() const;
1249 * Reports the start index of this matcher's region. The searches this matcher
1250 * conducts are limited to finding matches within regionStart (inclusive) and
1251 * regionEnd (exclusive).
1253 * @return The starting (native) index of this matcher's region.
1256 virtual int64_t regionStart64() const;
1260 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1261 * this matcher conducts are limited to finding matches within regionStart
1262 * (inclusive) and regionEnd (exclusive).
1264 * @return The ending point (native) of this matcher's region.
1267 virtual int32_t regionEnd() const;
1270 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1271 * this matcher conducts are limited to finding matches within regionStart
1272 * (inclusive) and regionEnd (exclusive).
1274 * @return The ending point (native) of this matcher's region.
1277 virtual int64_t regionEnd64() const;
1280 * Queries the transparency of region bounds for this matcher.
1281 * See useTransparentBounds for a description of transparent and opaque bounds.
1282 * By default, a matcher uses opaque region boundaries.
1284 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1287 virtual UBool
hasTransparentBounds() const;
1290 * Sets the transparency of region bounds for this matcher.
1291 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1292 * If the boolean argument is false, then opaque bounds will be used.
1294 * Using transparent bounds, the boundaries of this matcher's region are transparent
1295 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1296 * see text beyond the boundaries of the region while checking for a match.
1298 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1299 * lookbehind, and boundary matching constructs.
1301 * By default, a matcher uses opaque bounds.
1303 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1304 * @return This Matcher;
1307 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1311 * Return true if this matcher is using anchoring bounds.
1312 * By default, matchers use anchoring region bounds.
1314 * @return TRUE if this matcher is using anchoring bounds.
1317 virtual UBool
hasAnchoringBounds() const;
1321 * Set whether this matcher is using Anchoring Bounds for its region.
1322 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1323 * and end of the region. Without Anchoring Bounds, anchors will only match at
1324 * the positions they would in the complete text.
1326 * Anchoring Bounds are the default for regions.
1328 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1329 * @return This Matcher
1332 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1336 * Return TRUE if the most recent matching operation attempted to access
1337 * additional input beyond the available input text.
1338 * In this case, additional input text could change the results of the match.
1340 * hitEnd() is defined for both successful and unsuccessful matches.
1341 * In either case hitEnd() will return TRUE if if the end of the text was
1342 * reached at any point during the matching process.
1344 * @return TRUE if the most recent match hit the end of input
1347 virtual UBool
hitEnd() const;
1350 * Return TRUE the most recent match succeeded and additional input could cause
1351 * it to fail. If this method returns false and a match was found, then more input
1352 * might change the match but the match won't be lost. If a match was not found,
1353 * then requireEnd has no meaning.
1355 * @return TRUE if more input could cause the most recent match to no longer match.
1358 virtual UBool
requireEnd() const;
1362 * Returns the pattern that is interpreted by this matcher.
1363 * @return the RegexPattern for this RegexMatcher
1366 virtual const RegexPattern
&pattern() const;
1370 * Replaces every substring of the input that matches the pattern
1371 * with the given replacement string. This is a convenience function that
1372 * provides a complete find-and-replace-all operation.
1374 * This method first resets this matcher. It then scans the input string
1375 * looking for matches of the pattern. Input that is not part of any
1376 * match is left unchanged; each match is replaced in the result by the
1377 * replacement string. The replacement string may contain references to
1380 * @param replacement a string containing the replacement text.
1381 * @param status a reference to a UErrorCode to receive any errors.
1382 * @return a string containing the results of the find and replace.
1385 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
1389 * Replaces every substring of the input that matches the pattern
1390 * with the given replacement string. This is a convenience function that
1391 * provides a complete find-and-replace-all operation.
1393 * This method first resets this matcher. It then scans the input string
1394 * looking for matches of the pattern. Input that is not part of any
1395 * match is left unchanged; each match is replaced in the result by the
1396 * replacement string. The replacement string may contain references to
1399 * @param replacement a string containing the replacement text.
1400 * @param dest a mutable UText in which the results are placed.
1401 * If NULL, a new UText will be created (which may not be mutable).
1402 * @param status a reference to a UErrorCode to receive any errors.
1403 * @return a string containing the results of the find and replace.
1404 * If a pre-allocated UText was provided, it will always be used and returned.
1408 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1412 * Replaces the first substring of the input that matches
1413 * the pattern with the replacement string. This is a convenience
1414 * function that provides a complete find-and-replace operation.
1416 * <p>This function first resets this RegexMatcher. It then scans the input string
1417 * looking for a match of the pattern. Input that is not part
1418 * of the match is appended directly to the result string; the match is replaced
1419 * in the result by the replacement string. The replacement string may contain
1420 * references to captured groups.</p>
1422 * <p>The state of the matcher (the position at which a subsequent find()
1423 * would begin) after completing a replaceFirst() is not specified. The
1424 * RegexMatcher should be reset before doing additional find() operations.</p>
1426 * @param replacement a string containing the replacement text.
1427 * @param status a reference to a UErrorCode to receive any errors.
1428 * @return a string containing the results of the find and replace.
1431 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1435 * Replaces the first substring of the input that matches
1436 * the pattern with the replacement string. This is a convenience
1437 * function that provides a complete find-and-replace operation.
1439 * <p>This function first resets this RegexMatcher. It then scans the input string
1440 * looking for a match of the pattern. Input that is not part
1441 * of the match is appended directly to the result string; the match is replaced
1442 * in the result by the replacement string. The replacement string may contain
1443 * references to captured groups.</p>
1445 * <p>The state of the matcher (the position at which a subsequent find()
1446 * would begin) after completing a replaceFirst() is not specified. The
1447 * RegexMatcher should be reset before doing additional find() operations.</p>
1449 * @param replacement a string containing the replacement text.
1450 * @param dest a mutable UText in which the results are placed.
1451 * If NULL, a new UText will be created (which may not be mutable).
1452 * @param status a reference to a UErrorCode to receive any errors.
1453 * @return a string containing the results of the find and replace.
1454 * If a pre-allocated UText was provided, it will always be used and returned.
1458 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1462 * Implements a replace operation intended to be used as part of an
1463 * incremental find-and-replace.
1465 * <p>The input string, starting from the end of the previous replacement and ending at
1466 * the start of the current match, is appended to the destination string. Then the
1467 * replacement string is appended to the output string,
1468 * including handling any substitutions of captured text.</p>
1470 * <p>For simple, prepackaged, non-incremental find-and-replace
1471 * operations, see replaceFirst() or replaceAll().</p>
1473 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1474 * @param replacement A UnicodeString 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
1478 * @param status A reference to a UErrorCode to receive any errors. Possible
1479 * errors are U_REGEX_INVALID_STATE if no match has been
1480 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1481 * if the replacement text specifies a capture group that
1482 * does not exist in the pattern.
1484 * @return this RegexMatcher
1488 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1489 const UnicodeString
&replacement
, UErrorCode
&status
);
1493 * Implements a replace operation intended to be used as part of an
1494 * incremental find-and-replace.
1496 * <p>The input string, starting from the end of the previous replacement and ending at
1497 * the start of the current match, is appended to the destination string. Then the
1498 * replacement string is appended to the output string,
1499 * including handling any substitutions of captured text.</p>
1501 * <p>For simple, prepackaged, non-incremental find-and-replace
1502 * operations, see replaceFirst() or replaceAll().</p>
1504 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1506 * @param replacement A UText that provides the text to be substituted for
1507 * the input text that matched the regexp pattern. The replacement
1508 * text may contain references to captured text from the input.
1509 * @param status A reference to a UErrorCode to receive any errors. Possible
1510 * errors are U_REGEX_INVALID_STATE if no match has been
1511 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1512 * if the replacement text specifies a capture group that
1513 * does not exist in the pattern.
1515 * @return this RegexMatcher
1519 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1520 UText
*replacement
, UErrorCode
&status
);
1524 * As the final step in a find-and-replace operation, append the remainder
1525 * of the input string, starting at the position following the last appendReplacement(),
1526 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1527 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1529 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1530 * @return the destination string.
1533 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1537 * As the final step in a find-and-replace operation, append the remainder
1538 * of the input string, starting at the position following the last appendReplacement(),
1539 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1540 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1542 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1544 * @param status error cod
1545 * @return the destination string.
1549 virtual UText
*appendTail(UText
*dest
, UErrorCode
&status
);
1553 * Split a string into fields. Somewhat like split() from Perl.
1554 * The pattern matches identify delimiters that separate the input
1555 * into fields. The input data between the matches becomes the
1556 * fields themselves.
1558 * @param input The string to be split into fields. The field delimiters
1559 * match the pattern (in the "this" object). This matcher
1560 * will be reset to this input string.
1561 * @param dest An array of UnicodeStrings to receive the results of the split.
1562 * This is an array of actual UnicodeString objects, not an
1563 * array of pointers to strings. Local (stack based) arrays can
1565 * @param destCapacity The number of elements in the destination array.
1566 * If the number of fields found is less than destCapacity, the
1567 * extra strings in the destination array are not altered.
1568 * If the number of destination strings is less than the number
1569 * of fields, the trailing part of the input string, including any
1570 * field delimiters, is placed in the last destination string.
1571 * @param status A reference to a UErrorCode to receive any errors.
1572 * @return The number of fields into which the input string was split.
1575 virtual int32_t split(const UnicodeString
&input
,
1576 UnicodeString dest
[],
1577 int32_t destCapacity
,
1578 UErrorCode
&status
);
1582 * Split a string into fields. Somewhat like split() from Perl.
1583 * The pattern matches identify delimiters that separate the input
1584 * into fields. The input data between the matches becomes the
1585 * fields themselves.
1587 * @param input The string to be split into fields. The field delimiters
1588 * match the pattern (in the "this" object). This matcher
1589 * will be reset to this input string.
1590 * @param dest An array of mutable UText structs to receive the results of the split.
1591 * If a field is NULL, a new UText is allocated to contain the results for
1592 * that field. This new UText is not guaranteed to be mutable.
1593 * @param destCapacity The number of elements in the destination array.
1594 * If the number of fields found is less than destCapacity, the
1595 * extra strings in the destination array are not altered.
1596 * If the number of destination strings is less than the number
1597 * of fields, the trailing part of the input string, including any
1598 * field delimiters, is placed in the last destination string.
1599 * @param status A reference to a UErrorCode to receive any errors.
1600 * @return The number of fields into which the input string was split.
1604 virtual int32_t split(UText
*input
,
1606 int32_t destCapacity
,
1607 UErrorCode
&status
);
1610 * Set a processing time limit for match operations with this Matcher.
1612 * Some patterns, when matching certain strings, can run in exponential time.
1613 * For practical purposes, the match operation may appear to be in an
1615 * When a limit is set a match operation will fail with an error if the
1616 * limit is exceeded.
1618 * The units of the limit are steps of the match engine.
1619 * Correspondence with actual processor time will depend on the speed
1620 * of the processor and the details of the specific pattern, but will
1621 * typically be on the order of milliseconds.
1623 * By default, the matching time is not limited.
1626 * @param limit The limit value, or 0 for no limit.
1627 * @param status A reference to a UErrorCode to receive any errors.
1630 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1633 * Get the time limit, if any, for match operations made with this Matcher.
1635 * @return the maximum allowed time for a match, in units of processing steps.
1638 virtual int32_t getTimeLimit() const;
1641 * Set the amount of heap storage available for use by the match backtracking stack.
1642 * The matcher is also reset, discarding any results from previous matches.
1644 * ICU uses a backtracking regular expression engine, with the backtrack stack
1645 * maintained on the heap. This function sets the limit to the amount of memory
1646 * that can be used for this purpose. A backtracking stack overflow will
1647 * result in an error from the match operation that caused it.
1649 * A limit is desirable because a malicious or poorly designed pattern can use
1650 * excessive memory, potentially crashing the process. A limit is enabled
1653 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1654 * A value of zero means no limit.
1655 * The limit must be greater or equal to zero.
1657 * @param status A reference to a UErrorCode to receive any errors.
1661 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1664 * Get the size of the heap storage available for use by the back tracking stack.
1666 * @return the maximum backtracking stack size, in bytes, or zero if the
1667 * stack size is unlimited.
1670 virtual int32_t getStackLimit() const;
1674 * Set a callback function for use with this Matcher.
1675 * During matching operations the function will be called periodically,
1676 * giving the application the opportunity to terminate a long-running
1679 * @param callback A pointer to the user-supplied callback function.
1680 * @param context User context pointer. The value supplied at the
1681 * time the callback function is set will be saved
1682 * and passed to the callback each time that it is called.
1683 * @param status A reference to a UErrorCode to receive any errors.
1686 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1687 const void *context
,
1688 UErrorCode
&status
);
1692 * Get the callback function for this URegularExpression.
1694 * @param callback Out parameter, receives a pointer to the user-supplied
1695 * callback function.
1696 * @param context Out parameter, receives the user context pointer that
1697 * was set when uregex_setMatchCallback() was called.
1698 * @param status A reference to a UErrorCode to receive any errors.
1701 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1702 const void *&context
,
1703 UErrorCode
&status
);
1707 * Set a progress callback function for use with find operations on this Matcher.
1708 * During find operations, the callback will be invoked after each return from a
1709 * match attempt, giving the application the opportunity to terminate a long-running
1712 * @param callback A pointer to the user-supplied callback function.
1713 * @param context User context pointer. The value supplied at the
1714 * time the callback function is set will be saved
1715 * and passed to the callback each time that it is called.
1716 * @param status A reference to a UErrorCode to receive any errors.
1719 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1720 const void *context
,
1721 UErrorCode
&status
);
1725 * Get the find progress callback function for this URegularExpression.
1727 * @param callback Out parameter, receives a pointer to the user-supplied
1728 * callback function.
1729 * @param context Out parameter, receives the user context pointer that
1730 * was set when uregex_setFindProgressCallback() was called.
1731 * @param status A reference to a UErrorCode to receive any errors.
1734 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1735 const void *&context
,
1736 UErrorCode
&status
);
1738 #ifndef U_HIDE_INTERNAL_API
1740 * setTrace Debug function, enable/disable tracing of the matching engine.
1741 * For internal ICU development use only. DO NO USE!!!!
1744 void setTrace(UBool state
);
1745 #endif /* U_HIDE_INTERNAL_API */
1748 * ICU "poor man's RTTI", returns a UClassID for this class.
1752 static UClassID U_EXPORT2
getStaticClassID();
1755 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1759 virtual UClassID
getDynamicClassID() const;
1762 // Constructors and other object boilerplate are private.
1763 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1764 RegexMatcher(); // default constructor not implemented
1765 RegexMatcher(const RegexPattern
*pat
);
1766 RegexMatcher(const RegexMatcher
&other
);
1767 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1768 void init(UErrorCode
&status
); // Common initialization
1769 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1771 friend class RegexPattern
;
1772 friend class RegexCImpl
;
1774 #ifndef U_HIDE_INTERNAL_API
1776 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1777 #endif /* U_HIDE_INTERNAL_API */
1781 // MatchAt This is the internal interface to the match engine itself.
1782 // Match status comes back in matcher member variables.
1784 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1785 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1786 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1787 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1788 REStackFrame
*resetStack();
1789 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1790 void IncrementTime(UErrorCode
&status
);
1792 // Call user find callback function, if set. Return TRUE if operation should be interrupted.
1793 inline UBool
findProgressInterrupt(int64_t matchIndex
, UErrorCode
&status
);
1795 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1797 UBool
findUsingChunk(UErrorCode
&status
);
1798 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1799 UBool
isChunkWordBoundary(int32_t pos
);
1801 const RegexPattern
*fPattern
;
1802 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1803 // should delete it when through.
1805 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1806 UText
*fInputText
; // The text being matched. Is never NULL.
1807 UText
*fAltInputText
; // A shallow copy of the text being matched.
1808 // Only created if the pattern contains backreferences.
1809 int64_t fInputLength
; // Full length of the input text.
1810 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1812 int64_t fRegionStart
; // Start of the input region, default = 0.
1813 int64_t fRegionLimit
; // End of input region, default to input.length.
1815 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1816 int64_t fAnchorLimit
; // See useAnchoringBounds
1818 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1819 int64_t fLookLimit
; // and other boundary tests. See
1820 // useTransparentBounds
1822 int64_t fActiveStart
; // Currently active bounds for matching.
1823 int64_t fActiveLimit
; // Usually is the same as region, but
1824 // is changed to fLookStart/Limit when
1825 // entering look around regions.
1827 UBool fTransparentBounds
; // True if using transparent bounds.
1828 UBool fAnchoringBounds
; // True if using anchoring bounds.
1830 UBool fMatch
; // True if the last attempted match was successful.
1831 int64_t fMatchStart
; // Position of the start of the most recent match
1832 int64_t fMatchEnd
; // First position after the end of the most recent match
1833 // Zero if no previous match, even when a region
1835 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1836 // or -1 if there was no previous match.
1837 int64_t fAppendPosition
; // First position after the end of the previous
1838 // appendReplacement(). As described by the
1839 // JavaDoc for Java Matcher, where it is called
1840 // "append position"
1841 UBool fHitEnd
; // True if the last match touched the end of input.
1842 UBool fRequireEnd
; // True if the last match required end-of-input
1846 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1847 // which will contain the capture group results.
1848 // NOT valid while match engine is running.
1850 int64_t *fData
; // Data area for use by the compiled pattern.
1851 int64_t fSmallData
[8]; // Use this for data if it's enough.
1853 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1854 // match engine run. Zero for unlimited.
1856 int32_t fTime
; // Match time, accumulates while matching.
1857 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1858 // Kept separately from fTime to keep as much
1859 // code as possible out of the inline
1860 // StateSave function.
1862 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1863 // stack, in bytes. Zero for unlimited.
1865 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1866 // NULL if there is no callback.
1867 const void *fCallbackContext
; // User Context ptr for callback function.
1869 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1870 // NULL if there is no callback.
1871 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1874 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1876 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1878 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1879 // reported, or that permanently disables this matcher.
1881 RuleBasedBreakIterator
*fWordBreakItr
;
1885 #endif // U_SHOW_CPLUSPLUS_API
1887 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS