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 * The ICU API for processing regular expressions consists of two classes,
28 * `RegexPattern` and `RegexMatcher`.
29 * `RegexPattern` 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 `RegexMatcher` objects for the pattern.
33 * Class `RegexMatcher` bundles together a regular expression
34 * pattern and a target string to which the search pattern will be applied.
35 * `RegexMatcher` 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.
39 * Note that by constructing `RegexMatcher` objects directly from regular
40 * expression pattern strings application code can be simplified and the explicit
41 * need for `RegexPattern` 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
60 #if U_SHOW_CPLUSPLUS_API
68 class RuleBasedBreakIterator
;
76 * Class `RegexPattern` represents a compiled regular expression. It includes
77 * factory methods for creating a RegexPattern object from the source (string) form
78 * of a regular expression, methods for creating RegexMatchers that allow the pattern
79 * to be applied to input text, and a few convenience methods for simple common
80 * uses of regular expressions.
82 * Class RegexPattern is not intended to be subclassed.
86 class U_I18N_API RegexPattern U_FINAL
: public UObject
{
90 * default constructor. Create a RegexPattern object that refers to no actual
91 * pattern. Not normally needed; RegexPattern objects are usually
92 * created using the factory method `compile()`.
99 * Copy Constructor. Create a new RegexPattern object that is equivalent
100 * to the source object.
101 * @param source the pattern object to be copied.
104 RegexPattern(const RegexPattern
&source
);
107 * Destructor. Note that a RegexPattern object must persist so long as any
108 * RegexMatcher objects that were created from the RegexPattern are active.
111 virtual ~RegexPattern();
114 * Comparison operator. Two RegexPattern objects are considered equal if they
115 * were constructed from identical source patterns using the same #URegexpFlag
117 * @param that a RegexPattern object to compare with "this".
118 * @return TRUE if the objects are equivalent.
121 UBool
operator==(const RegexPattern
& that
) const;
124 * Comparison operator. Two RegexPattern objects are considered equal if they
125 * were constructed from identical source patterns using the same #URegexpFlag
127 * @param that a RegexPattern object to compare with "this".
128 * @return TRUE if the objects are different.
131 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);}
134 * Assignment operator. After assignment, this RegexPattern will behave identically
135 * to the source object.
138 RegexPattern
&operator =(const RegexPattern
&source
);
141 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
142 * intended to be subclassed, <code>clone()</code> and the copy construction are
143 * equivalent operations.
144 * @return the copy of this RegexPattern
147 virtual RegexPattern
*clone() const;
151 * Compiles the regular expression in string form into a RegexPattern
152 * object. These compile methods, rather than the constructors, are the usual
153 * way that RegexPattern objects are created.
155 * Note that RegexPattern objects must not be deleted while RegexMatcher
156 * objects created from the pattern are active. RegexMatchers keep a pointer
157 * back to their pattern, so premature deletion of the pattern is a
158 * catastrophic error.
160 * All #URegexpFlag pattern match mode flags are set to their default values.
162 * Note that it is often more convenient to construct a RegexMatcher directly
163 * from a pattern string rather than separately compiling the pattern and
164 * then creating a RegexMatcher object from the pattern.
166 * @param regex The regular expression to be compiled.
167 * @param pe Receives the position (line and column nubers) of any error
168 * within the regular expression.)
169 * @param status A reference to a UErrorCode to receive any errors.
170 * @return A regexPattern object for the compiled pattern.
174 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
179 * Compiles the regular expression in string form into a RegexPattern
180 * object. These compile methods, rather than the constructors, are the usual
181 * way that RegexPattern objects are created.
183 * Note that RegexPattern objects must not be deleted while RegexMatcher
184 * objects created from the pattern are active. RegexMatchers keep a pointer
185 * back to their pattern, so premature deletion of the pattern is a
186 * catastrophic error.
188 * All #URegexpFlag pattern match mode flags are set to their default values.
190 * Note that it is often more convenient to construct a RegexMatcher directly
191 * from a pattern string rather than separately compiling the pattern and
192 * then creating a RegexMatcher object from the pattern.
194 * @param regex The regular expression to be compiled. Note, the text referred
195 * to by this UText must not be deleted during the lifetime of the
196 * RegexPattern object or any RegexMatcher object created from it.
197 * @param pe Receives the position (line and column nubers) of any error
198 * within the regular expression.)
199 * @param status A reference to a UErrorCode to receive any errors.
200 * @return A regexPattern object for the compiled pattern.
204 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
209 * Compiles the regular expression in string form into a RegexPattern
210 * object using the specified #URegexpFlag match mode flags. These compile methods,
211 * rather than the constructors, are the usual way that RegexPattern objects
214 * Note that RegexPattern objects must not be deleted while RegexMatcher
215 * objects created from the pattern are active. RegexMatchers keep a pointer
216 * back to their pattern, so premature deletion of the pattern is a
217 * catastrophic error.
219 * Note that it is often more convenient to construct a RegexMatcher directly
220 * from a pattern string instead of than separately compiling the pattern and
221 * then creating a RegexMatcher object from the pattern.
223 * @param regex The regular expression to be compiled.
224 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
225 * @param pe Receives the position (line and column numbers) of any error
226 * within the regular expression.)
227 * @param status A reference to a UErrorCode to receive any errors.
228 * @return A regexPattern object for the compiled pattern.
232 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
238 * Compiles the regular expression in string form into a RegexPattern
239 * object using the specified #URegexpFlag match mode flags. These compile methods,
240 * rather than the constructors, are the usual way that RegexPattern objects
243 * Note that RegexPattern objects must not be deleted while RegexMatcher
244 * objects created from the pattern are active. RegexMatchers keep a pointer
245 * back to their pattern, so premature deletion of the pattern is a
246 * catastrophic error.
248 * Note that it is often more convenient to construct a RegexMatcher directly
249 * from a pattern string instead of than separately compiling the pattern and
250 * then creating a RegexMatcher object from the pattern.
252 * @param regex The regular expression to be compiled. Note, the text referred
253 * to by this UText must not be deleted during the lifetime of the
254 * RegexPattern object or any RegexMatcher object created from it.
255 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
256 * @param pe Receives the position (line and column numbers) of any error
257 * within the regular expression.)
258 * @param status A reference to a UErrorCode to receive any errors.
259 * @return A regexPattern object for the compiled pattern.
263 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
269 * Compiles the regular expression in string form into a RegexPattern
270 * object using the specified #URegexpFlag match mode flags. These compile methods,
271 * rather than the constructors, are the usual way that RegexPattern objects
274 * Note that RegexPattern objects must not be deleted while RegexMatcher
275 * objects created from the pattern are active. RegexMatchers keep a pointer
276 * back to their pattern, so premature deletion of the pattern is a
277 * catastrophic error.
279 * Note that it is often more convenient to construct a RegexMatcher directly
280 * from a pattern string instead of than separately compiling the pattern and
281 * then creating a RegexMatcher object from the pattern.
283 * @param regex The regular expression to be compiled.
284 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
285 * @param status A reference to a UErrorCode to receive any errors.
286 * @return A regexPattern object for the compiled pattern.
290 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
295 * Compiles the regular expression in string form into a RegexPattern
296 * object using the specified #URegexpFlag match mode flags. These compile methods,
297 * rather than the constructors, are the usual way that RegexPattern objects
300 * Note that RegexPattern objects must not be deleted while RegexMatcher
301 * objects created from the pattern are active. RegexMatchers keep a pointer
302 * back to their pattern, so premature deletion of the pattern is a
303 * catastrophic error.
305 * Note that it is often more convenient to construct a RegexMatcher directly
306 * from a pattern string instead of than separately compiling the pattern and
307 * then creating a RegexMatcher object from the pattern.
309 * @param regex The regular expression to be compiled. Note, the text referred
310 * to by this UText must not be deleted during the lifetime of the
311 * RegexPattern object or any RegexMatcher object created from it.
312 * @param flags The #URegexpFlag match mode flags to be used, e.g. #UREGEX_CASE_INSENSITIVE.
313 * @param status A reference to a UErrorCode to receive any errors.
314 * @return A regexPattern object for the compiled pattern.
318 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
323 * Get the #URegexpFlag match mode flags that were used when compiling this pattern.
324 * @return the #URegexpFlag match mode flags
327 virtual uint32_t flags() const;
330 * Creates a RegexMatcher that will match the given input against this pattern. The
331 * RegexMatcher can then be used to perform match, find or replace operations
332 * on the input. Note that a RegexPattern object must not be deleted while
333 * RegexMatchers created from it still exist and might possibly be used again.
335 * The matcher will retain a reference to the supplied input string, and all regexp
336 * pattern matching operations happen directly on this original string. It is
337 * critical that the string not be altered or deleted before use by the regular
338 * expression operations is complete.
340 * @param input The input string to which the regular expression will be applied.
341 * @param status A reference to a UErrorCode to receive any errors.
342 * @return A RegexMatcher object for this pattern and input.
346 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
347 UErrorCode
&status
) const;
351 * Cause a compilation error if an application accidentally attempts to
352 * create a matcher with a (char16_t *) string as input rather than
353 * a UnicodeString. Avoids a dangling reference to a temporary string.
355 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
356 * using one of the aliasing constructors, such as
357 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
358 * or in a UText, using
359 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
362 RegexMatcher
*matcher(const char16_t *input
,
363 UErrorCode
&status
) const;
368 * Creates a RegexMatcher that will match against this pattern. The
369 * RegexMatcher can be used to perform match, find or replace operations.
370 * Note that a RegexPattern object must not be deleted while
371 * RegexMatchers created from it still exist and might possibly be used again.
373 * @param status A reference to a UErrorCode to receive any errors.
374 * @return A RegexMatcher object for this pattern and input.
378 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
382 * Test whether a string matches a regular expression. This convenience function
383 * both compiles the regular expression and applies it in a single operation.
384 * Note that if the same pattern needs to be applied repeatedly, this method will be
385 * less efficient than creating and reusing a RegexMatcher object.
387 * @param regex The regular expression
388 * @param input The string data to be matched
389 * @param pe Receives the position of any syntax errors within the regular expression
390 * @param status A reference to a UErrorCode to receive any errors.
391 * @return True if the regular expression exactly matches the full input string.
395 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
396 const UnicodeString
&input
,
401 * Test whether a string matches a regular expression. This convenience function
402 * both compiles the regular expression and applies it in a single operation.
403 * Note that if the same pattern needs to be applied repeatedly, this method will be
404 * less efficient than creating and reusing a RegexMatcher object.
406 * @param regex The regular expression
407 * @param input The string data to be matched
408 * @param pe Receives the position of any syntax errors within the regular expression
409 * @param status A reference to a UErrorCode to receive any errors.
410 * @return True if the regular expression exactly matches the full input string.
414 static UBool U_EXPORT2
matches(UText
*regex
,
420 * Returns the regular expression from which this pattern was compiled. This method will work
421 * even if the pattern was compiled from a UText.
423 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
424 * the returned string may no longer reflect the RegexPattern object.
427 virtual UnicodeString
pattern() const;
431 * Returns the regular expression from which this pattern was compiled. This method will work
432 * even if the pattern was compiled from a UnicodeString.
434 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
435 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
440 virtual UText
*patternText(UErrorCode
&status
) const;
444 * Get the group number corresponding to a named capture group.
445 * The returned number can be used with any function that access
446 * capture groups by number.
448 * The function returns an error status if the specified name does not
449 * appear in the pattern.
451 * @param groupName The capture group name.
452 * @param status A UErrorCode to receive any errors.
456 virtual int32_t groupNumberFromName(const UnicodeString
&groupName
, UErrorCode
&status
) const;
460 * Get the group number corresponding to a named capture group.
461 * The returned number can be used with any function that access
462 * capture groups by number.
464 * The function returns an error status if the specified name does not
465 * appear in the pattern.
467 * @param groupName The capture group name,
468 * platform invariant characters only.
469 * @param nameLength The length of the name, or -1 if the name is
471 * @param status A UErrorCode to receive any errors.
475 virtual int32_t groupNumberFromName(const char *groupName
, int32_t nameLength
, UErrorCode
&status
) const;
479 * Split a string into fields. Somewhat like split() from Perl or Java.
480 * Pattern matches identify delimiters that separate the input
481 * into fields. The input data between the delimiters becomes the
484 * If the delimiter pattern includes capture groups, the captured text will
485 * also appear in the destination array of output strings, interspersed
486 * with the fields. This is similar to Perl, but differs from Java,
487 * which ignores the presence of capture groups in the pattern.
489 * Trailing empty fields will always be returned, assuming sufficient
490 * destination capacity. This differs from the default behavior for Java
491 * and Perl where trailing empty fields are not returned.
493 * The number of strings produced by the split operation is returned.
494 * This count includes the strings from capture groups in the delimiter pattern.
495 * This behavior differs from Java, which ignores capture groups.
497 * For the best performance on split() operations,
498 * <code>RegexMatcher::split</code> is preferable to this function
500 * @param input The string to be split into fields. The field delimiters
501 * match the pattern (in the "this" object)
502 * @param dest An array of UnicodeStrings to receive the results of the split.
503 * This is an array of actual UnicodeString objects, not an
504 * array of pointers to strings. Local (stack based) arrays can
506 * @param destCapacity The number of elements in the destination array.
507 * If the number of fields found is less than destCapacity, the
508 * extra strings in the destination array are not altered.
509 * If the number of destination strings is less than the number
510 * of fields, the trailing part of the input string, including any
511 * field delimiters, is placed in the last destination string.
512 * @param status A reference to a UErrorCode to receive any errors.
513 * @return The number of fields into which the input string was split.
516 virtual int32_t split(const UnicodeString
&input
,
517 UnicodeString dest
[],
518 int32_t destCapacity
,
519 UErrorCode
&status
) const;
523 * Split a string into fields. Somewhat like %split() from Perl or Java.
524 * Pattern matches identify delimiters that separate the input
525 * into fields. The input data between the delimiters becomes the
528 * If the delimiter pattern includes capture groups, the captured text will
529 * also appear in the destination array of output strings, interspersed
530 * with the fields. This is similar to Perl, but differs from Java,
531 * which ignores the presence of capture groups in the pattern.
533 * Trailing empty fields will always be returned, assuming sufficient
534 * destination capacity. This differs from the default behavior for Java
535 * and Perl where trailing empty fields are not returned.
537 * The number of strings produced by the split operation is returned.
538 * This count includes the strings from capture groups in the delimiter pattern.
539 * This behavior differs from Java, which ignores capture groups.
541 * For the best performance on split() operations,
542 * `RegexMatcher::split()` is preferable to this function
544 * @param input The string to be split into fields. The field delimiters
545 * match the pattern (in the "this" object)
546 * @param dest An array of mutable UText structs to receive the results of the split.
547 * If a field is NULL, a new UText is allocated to contain the results for
548 * that field. This new UText is not guaranteed to be mutable.
549 * @param destCapacity The number of elements in the destination array.
550 * If the number of fields found is less than destCapacity, the
551 * extra strings in the destination array are not altered.
552 * If the number of destination strings is less than the number
553 * of fields, the trailing part of the input string, including any
554 * field delimiters, is placed in the last destination string.
555 * @param status A reference to a UErrorCode to receive any errors.
556 * @return The number of destination strings used.
560 virtual int32_t split(UText
*input
,
562 int32_t destCapacity
,
563 UErrorCode
&status
) const;
567 * ICU "poor man's RTTI", returns a UClassID for the actual class.
571 virtual UClassID
getDynamicClassID() const;
574 * ICU "poor man's RTTI", returns a UClassID for this class.
578 static UClassID U_EXPORT2
getStaticClassID();
582 // Implementation Data
584 UText
*fPattern
; // The original pattern string.
585 UnicodeString
*fPatternString
; // The original pattern UncodeString if relevant
586 uint32_t fFlags
; // The flags used when compiling the pattern.
588 UVector64
*fCompiledPat
; // The compiled pattern p-code.
589 UnicodeString fLiteralText
; // Any literal string data from the pattern,
590 // after un-escaping, for use during the match.
592 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
593 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
596 UErrorCode fDeferredStatus
; // status if some prior error has left this
597 // RegexPattern in an unusable state.
599 int32_t fMinMatchLen
; // Minimum Match Length. All matches will have length
600 // >= this value. For some patterns, this calculated
601 // value may be less than the true shortest
604 int32_t fFrameSize
; // Size of a state stack frame in the
607 int32_t fDataSize
; // The size of the data needed by the pattern that
608 // does not go on the state stack, but has just
609 // a single copy per matcher.
611 UVector32
*fGroupMap
; // Map from capture group number to position of
612 // the group's variables in the matcher stack frame.
614 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
615 // regex character classes, e.g. Word.
617 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
618 // sets for predefined regex classes.
620 int32_t fStartType
; // Info on how a match must start.
621 int32_t fInitialStringIdx
; //
622 int32_t fInitialStringLen
;
623 UnicodeSet
*fInitialChars
;
624 UChar32 fInitialChar
;
625 Regex8BitSet
*fInitialChars8
;
626 UBool fNeedsAltInput
;
628 UHashtable
*fNamedCaptureMap
; // Map from capture group names to numbers.
630 friend class RegexCompile
;
631 friend class RegexMatcher
;
632 friend class RegexCImpl
;
635 // Implementation Methods
637 void init(); // Common initialization, for use by constructors.
638 void zap(); // Common cleanup
640 void dumpOp(int32_t index
) const;
643 #ifndef U_HIDE_INTERNAL_API
645 * Dump a compiled pattern. Internal debug function.
648 void dumpPattern() const;
649 #endif /* U_HIDE_INTERNAL_API */
655 * class RegexMatcher bundles together a regular expression pattern and
656 * input text to which the expression can be applied. It includes methods
657 * for testing for matches, and for find and replace operations.
659 * <p>Class RegexMatcher is not intended to be subclassed.</p>
663 class U_I18N_API RegexMatcher U_FINAL
: public UObject
{
667 * Construct a RegexMatcher for a regular expression.
668 * This is a convenience method that avoids the need to explicitly create
669 * a RegexPattern object. Note that if several RegexMatchers need to be
670 * created for the same expression, it will be more efficient to
671 * separately create and cache a RegexPattern object, and use
672 * its matcher() method to create the RegexMatcher objects.
674 * @param regexp The Regular Expression to be compiled.
675 * @param flags #URegexpFlag options, such as #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 #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
691 * @param status Any errors are reported by setting this UErrorCode variable.
695 RegexMatcher(UText
*regexp
, uint32_t flags
, UErrorCode
&status
);
698 * Construct a RegexMatcher for a regular expression.
699 * This is a convenience method that avoids the need to explicitly create
700 * a RegexPattern object. Note that if several RegexMatchers need to be
701 * created for the same expression, it will be more efficient to
702 * separately create and cache a RegexPattern object, and use
703 * its matcher() method to create the RegexMatcher objects.
705 * The matcher will retain a reference to the supplied input string, and all regexp
706 * pattern matching operations happen directly on the original string. It is
707 * critical that the string not be altered or deleted before use by the regular
708 * expression operations is complete.
710 * @param regexp The Regular Expression to be compiled.
711 * @param input The string to match. The matcher retains a reference to the
712 * caller's string; mo copy is made.
713 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
714 * @param status Any errors are reported by setting this UErrorCode variable.
717 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
718 uint32_t flags
, UErrorCode
&status
);
721 * Construct a RegexMatcher for a regular expression.
722 * This is a convenience method that avoids the need to explicitly create
723 * a RegexPattern object. Note that if several RegexMatchers need to be
724 * created for the same expression, it will be more efficient to
725 * separately create and cache a RegexPattern object, and use
726 * its matcher() method to create the RegexMatcher objects.
728 * The matcher will make a shallow clone of the supplied input text, and all regexp
729 * pattern matching operations happen on this clone. While read-only operations on
730 * the supplied text are permitted, it is critical that the underlying string not be
731 * altered or deleted before use by the regular expression operations is complete.
733 * @param regexp The Regular Expression to be compiled.
734 * @param input The string to match. The matcher retains a shallow clone of the text.
735 * @param flags #URegexpFlag options, such as #UREGEX_CASE_INSENSITIVE.
736 * @param status Any errors are reported by setting this UErrorCode variable.
740 RegexMatcher(UText
*regexp
, UText
*input
,
741 uint32_t flags
, UErrorCode
&status
);
745 * Cause a compilation error if an application accidentally attempts to
746 * create a matcher with a (char16_t *) string as input rather than
747 * a UnicodeString. Avoids a dangling reference to a temporary string.
749 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
750 * using one of the aliasing constructors, such as
751 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
752 * or in a UText, using
753 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
755 RegexMatcher(const UnicodeString
®exp
, const char16_t *input
,
756 uint32_t flags
, UErrorCode
&status
);
765 virtual ~RegexMatcher();
769 * Attempts to match the entire input region against the pattern.
770 * @param status A reference to a UErrorCode to receive any errors.
771 * @return TRUE if there is a match
774 virtual UBool
matches(UErrorCode
&status
);
778 * Resets the matcher, then attempts to match the input beginning
779 * at the specified startIndex, and extending to the end of the input.
780 * The input region is reset to include the entire input string.
781 * A successful match must extend to the end of the input.
782 * @param startIndex The input string (native) index at which to begin matching.
783 * @param status A reference to a UErrorCode to receive any errors.
784 * @return TRUE if there is a match
787 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
791 * Attempts to match the input string, starting from the beginning of the region,
792 * against the pattern. Like the matches() method, this function
793 * always starts at the beginning of the input region;
794 * unlike that function, it does not require that the entire region be matched.
796 * If the match succeeds then more information can be obtained via the start(),
797 * end(), and group() functions.
799 * @param status A reference to a UErrorCode to receive any errors.
800 * @return TRUE if there is a match at the start of the input string.
803 virtual UBool
lookingAt(UErrorCode
&status
);
807 * Attempts to match the input string, starting from the specified index, against the pattern.
808 * The match may be of any length, and is not required to extend to the end
809 * of the input string. Contrast with match().
811 * If the match succeeds then more information can be obtained via the start(),
812 * end(), and group() functions.
814 * @param startIndex The input string (native) index at which to begin matching.
815 * @param status A reference to a UErrorCode to receive any errors.
816 * @return TRUE if there is a match.
819 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
823 * Find the next pattern match in the input string.
824 * The find begins searching the input at the location following the end of
825 * the previous match, or at the start of the string if there is no previous match.
826 * If a match is found, `start()`, `end()` and `group()`
827 * will provide more information regarding the match.
828 * Note that if the input string is changed by the application,
829 * use find(startPos, status) instead of find(), because the saved starting
830 * position may not be valid with the altered input string.
831 * @return TRUE if a match is found.
834 virtual UBool
find();
838 * Find the next pattern match in the input string.
839 * The find begins searching the input at the location following the end of
840 * the previous match, or at the start of the string if there is no previous match.
841 * If a match is found, `start()`, `end()` and `group()`
842 * will provide more information regarding the match.
844 * Note that if the input string is changed by the application,
845 * use find(startPos, status) instead of find(), because the saved starting
846 * position may not be valid with the altered input string.
847 * @param status A reference to a UErrorCode to receive any errors.
848 * @return TRUE if a match is found.
851 virtual UBool
find(UErrorCode
&status
);
854 * Resets this RegexMatcher and then attempts to find the next substring of the
855 * input string that matches the pattern, starting at the specified index.
857 * @param start The (native) index in the input string to begin the search.
858 * @param status A reference to a UErrorCode to receive any errors.
859 * @return TRUE if a match is found.
862 virtual UBool
find(int64_t start
, UErrorCode
&status
);
866 * Returns a string containing the text matched by the previous match.
867 * If the pattern can match an empty string, an empty string may be returned.
868 * @param status A reference to a UErrorCode to receive any errors.
869 * Possible errors are U_REGEX_INVALID_STATE if no match
870 * has been attempted or the last match failed.
871 * @return a string containing the matched input text.
874 virtual UnicodeString
group(UErrorCode
&status
) const;
878 * Returns a string containing the text captured by the given group
879 * during the previous match operation. Group(0) is the entire match.
881 * A zero length string is returned both for capture groups that did not
882 * participate in the match and for actual zero length matches.
883 * To distinguish between these two cases use the function start(),
884 * which returns -1 for non-participating groups.
886 * @param groupNum the capture group number
887 * @param status A reference to a UErrorCode to receive any errors.
888 * Possible errors are U_REGEX_INVALID_STATE if no match
889 * has been attempted or the last match failed and
890 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
891 * @return the captured text
894 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
897 * Returns the number of capturing groups in this matcher's pattern.
898 * @return the number of capture groups
901 virtual int32_t groupCount() const;
905 * Returns a shallow clone of the entire live input string with the UText current native index
906 * set to the beginning of the requested group.
908 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
909 * @param group_len A reference to receive the length of the desired capture group
910 * @param status A reference to a UErrorCode to receive any errors.
911 * Possible errors are U_REGEX_INVALID_STATE if no match
912 * has been attempted or the last match failed and
913 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
914 * @return dest if non-NULL, a shallow copy of the input text otherwise
918 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
921 * Returns a shallow clone of the entire live input string with the UText current native index
922 * set to the beginning of the requested group.
924 * A group length of zero is returned both for capture groups that did not
925 * participate in the match and for actual zero length matches.
926 * To distinguish between these two cases use the function start(),
927 * which returns -1 for non-participating groups.
929 * @param groupNum The capture group number.
930 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
931 * @param group_len A reference to receive the length of the desired capture group
932 * @param status A reference to a UErrorCode to receive any errors.
933 * Possible errors are U_REGEX_INVALID_STATE if no match
934 * has been attempted or the last match failed and
935 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
936 * @return dest if non-NULL, a shallow copy of the input text otherwise
940 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
943 * Returns the index in the input string of the start of the text matched
944 * during the previous match operation.
945 * @param status a reference to a UErrorCode to receive any errors.
946 * @return The (native) position in the input string of the start of the last match.
949 virtual int32_t start(UErrorCode
&status
) const;
952 * Returns the index in the input string of the start of the text matched
953 * during the previous match operation.
954 * @param status a reference to a UErrorCode to receive any errors.
955 * @return The (native) position in the input string of the start of the last match.
958 virtual int64_t start64(UErrorCode
&status
) const;
962 * Returns the index in the input string of the start of the text matched by the
963 * specified capture group during the previous match operation. Return -1 if
964 * the capture group exists in the pattern, but was not part of the last match.
966 * @param group the capture group number
967 * @param status A reference to a UErrorCode to receive any errors. Possible
968 * errors are U_REGEX_INVALID_STATE if no match has been
969 * attempted or the last match failed, and
970 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
971 * @return the (native) start position of substring matched by the specified group.
974 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
977 * Returns the index in the input string of the start of the text matched by the
978 * specified capture group during the previous match operation. Return -1 if
979 * the capture group exists in the pattern, but was not part of the last match.
981 * @param group the capture group number.
982 * @param status A reference to a UErrorCode to receive any errors. Possible
983 * errors are U_REGEX_INVALID_STATE if no match has been
984 * attempted or the last match failed, and
985 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
986 * @return the (native) start position of substring matched by the specified group.
989 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
992 * Returns the index in the input string of the first character following the
993 * text matched during the previous match operation.
995 * @param status A reference to a UErrorCode to receive any errors. Possible
996 * errors are U_REGEX_INVALID_STATE if no match has been
997 * attempted or the last match failed.
998 * @return the index of the last character matched, plus one.
999 * The index value returned is a native index, corresponding to
1000 * code units for the underlying encoding type, for example,
1001 * a byte index for UTF-8.
1004 virtual int32_t end(UErrorCode
&status
) const;
1007 * Returns the index in the input string of the first character following the
1008 * text matched during the previous match operation.
1010 * @param status A reference to a UErrorCode to receive any errors. Possible
1011 * errors are U_REGEX_INVALID_STATE if no match has been
1012 * attempted or the last match failed.
1013 * @return the index of the last character matched, plus one.
1014 * The index value returned is a native index, corresponding to
1015 * code units for the underlying encoding type, for example,
1016 * a byte index for UTF-8.
1019 virtual int64_t end64(UErrorCode
&status
) const;
1023 * Returns the index in the input string of the character following the
1024 * text matched by the specified capture group during the previous match operation.
1026 * @param group the capture group number
1027 * @param status A reference to a UErrorCode to receive any errors. Possible
1028 * errors are U_REGEX_INVALID_STATE if no match has been
1029 * attempted or the last match failed and
1030 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1031 * @return the index of the first character following the text
1032 * captured by the specified group during the previous match operation.
1033 * Return -1 if the capture group exists in the pattern but was not part of the match.
1034 * The index value returned is a native index, corresponding to
1035 * code units for the underlying encoding type, for example,
1036 * a byte index for UTF8.
1039 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
1042 * Returns the index in the input string of the character following the
1043 * text matched by the specified capture group during the previous match operation.
1045 * @param group the capture group number
1046 * @param status A reference to a UErrorCode to receive any errors. Possible
1047 * errors are U_REGEX_INVALID_STATE if no match has been
1048 * attempted or the last match failed and
1049 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1050 * @return the index of the first character following the text
1051 * captured by the specified group during the previous match operation.
1052 * Return -1 if the capture group exists in the pattern but was not part of the match.
1053 * The index value returned is a native index, corresponding to
1054 * code units for the underlying encoding type, for example,
1055 * a byte index for UTF8.
1058 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
1061 * Resets this matcher. The effect is to remove any memory of previous matches,
1062 * and to cause subsequent find() operations to begin at the beginning of
1065 * @return this RegexMatcher.
1068 virtual RegexMatcher
&reset();
1072 * Resets this matcher, and set the current input position.
1073 * The effect is to remove any memory of previous matches,
1074 * and to cause subsequent find() operations to begin at
1075 * the specified (native) position in the input string.
1077 * The matcher's region is reset to its default, which is the entire
1080 * An alternative to this function is to set a match region
1081 * beginning at the desired index.
1083 * @return this RegexMatcher.
1086 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1090 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1091 * to be reused, which is more efficient than creating a new RegexMatcher for
1092 * each input string to be processed.
1093 * @param input The new string on which subsequent pattern matches will operate.
1094 * The matcher retains a reference to the callers string, and operates
1095 * directly on that. Ownership of the string remains with the caller.
1096 * Because no copy of the string is made, it is essential that the
1097 * caller not delete the string until after regexp operations on it
1099 * Note that while a reset on the matcher with an input string that is then
1100 * modified across/during matcher operations may be supported currently for UnicodeString,
1101 * this was not originally intended behavior, and support for this is not guaranteed
1102 * in upcoming versions of ICU.
1103 * @return this RegexMatcher.
1106 virtual RegexMatcher
&reset(const UnicodeString
&input
);
1110 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1111 * to be reused, which is more efficient than creating a new RegexMatcher for
1112 * each input string to be processed.
1113 * @param input The new string on which subsequent pattern matches will operate.
1114 * The matcher makes a shallow clone of the given text; ownership of the
1115 * original string remains with the caller. Because no deep copy of the
1116 * text is made, it is essential that the caller not modify the string
1117 * until after regexp operations on it are done.
1118 * @return this RegexMatcher.
1122 virtual RegexMatcher
&reset(UText
*input
);
1126 * Set the subject text string upon which the regular expression is looking for matches
1127 * without changing any other aspect of the matching state.
1128 * The new and previous text strings must have the same content.
1130 * This function is intended for use in environments where ICU is operating on
1131 * strings that may move around in memory. It provides a mechanism for notifying
1132 * ICU that the string has been relocated, and providing a new UText to access the
1133 * string in its new position.
1135 * Note that the regular expression implementation never copies the underlying text
1136 * of a string being matched, but always operates directly on the original text
1137 * provided by the user. Refreshing simply drops the references to the old text
1138 * and replaces them with references to the new.
1140 * Caution: this function is normally used only by very specialized,
1141 * system-level code. One example use case is with garbage collection that moves
1142 * the text in memory.
1144 * @param input The new (moved) text string.
1145 * @param status Receives errors detected by this function.
1149 virtual RegexMatcher
&refreshInputText(UText
*input
, UErrorCode
&status
);
1153 * Cause a compilation error if an application accidentally attempts to
1154 * reset a matcher with a (char16_t *) string as input rather than
1155 * a UnicodeString. Avoids a dangling reference to a temporary string.
1157 * To efficiently work with char16_t *strings, wrap the data in a UnicodeString
1158 * using one of the aliasing constructors, such as
1159 * `UnicodeString(UBool isTerminated, const char16_t *text, int32_t textLength);`
1160 * or in a UText, using
1161 * `utext_openUChars(UText *ut, const char16_t *text, int64_t textLength, UErrorCode *status);`
1164 RegexMatcher
&reset(const char16_t *input
);
1168 * Returns the input string being matched. Ownership of the string belongs to
1169 * the matcher; it should not be altered or deleted. This method will work even if the input
1170 * was originally supplied as a UText.
1171 * @return the input string
1174 virtual const UnicodeString
&input() const;
1177 * Returns the input string being matched. This is the live input text; it should not be
1178 * altered or deleted. This method will work even if the input was originally supplied as
1180 * @return the input text
1184 virtual UText
*inputText() const;
1187 * Returns the input string being matched, either by copying it into the provided
1188 * UText parameter or by returning a shallow clone of the live input. Note that copying
1189 * the entire input may cause significant performance and memory issues.
1190 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1191 * @param status error code
1192 * @return dest if non-NULL, a shallow copy of the input text otherwise
1196 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1199 /** Sets the limits of this matcher's region.
1200 * The region is the part of the input string that will be searched to find a match.
1201 * Invoking this method resets the matcher, and then sets the region to start
1202 * at the index specified by the start parameter and end at the index specified
1203 * by the end parameter.
1205 * Depending on the transparency and anchoring being used (see useTransparentBounds
1206 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1207 * at or around the boundaries of the region
1209 * The function will fail if start is greater than limit, or if either index
1210 * is less than zero or greater than the length of the string being matched.
1212 * @param start The (native) index to begin searches at.
1213 * @param limit The index to end searches at (exclusive).
1214 * @param status A reference to a UErrorCode to receive any errors.
1217 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1220 * Identical to region(start, limit, status) but also allows a start position without
1221 * resetting the region state.
1222 * @param regionStart The region start
1223 * @param regionLimit the limit of the region
1224 * @param startIndex The (native) index within the region bounds at which to begin searches.
1225 * @param status A reference to a UErrorCode to receive any errors.
1226 * If startIndex is not within the specified region bounds,
1227 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1230 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
1233 * Reports the start index of this matcher's region. The searches this matcher
1234 * conducts are limited to finding matches within regionStart (inclusive) and
1235 * regionEnd (exclusive).
1237 * @return The starting (native) index of this matcher's region.
1240 virtual int32_t regionStart() const;
1243 * Reports the start index of this matcher's region. The searches this matcher
1244 * conducts are limited to finding matches within regionStart (inclusive) and
1245 * regionEnd (exclusive).
1247 * @return The starting (native) index of this matcher's region.
1250 virtual int64_t regionStart64() const;
1254 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1255 * this matcher conducts are limited to finding matches within regionStart
1256 * (inclusive) and regionEnd (exclusive).
1258 * @return The ending point (native) of this matcher's region.
1261 virtual int32_t regionEnd() const;
1264 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1265 * this matcher conducts are limited to finding matches within regionStart
1266 * (inclusive) and regionEnd (exclusive).
1268 * @return The ending point (native) of this matcher's region.
1271 virtual int64_t regionEnd64() const;
1274 * Queries the transparency of region bounds for this matcher.
1275 * See useTransparentBounds for a description of transparent and opaque bounds.
1276 * By default, a matcher uses opaque region boundaries.
1278 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1281 virtual UBool
hasTransparentBounds() const;
1284 * Sets the transparency of region bounds for this matcher.
1285 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1286 * If the boolean argument is false, then opaque bounds will be used.
1288 * Using transparent bounds, the boundaries of this matcher's region are transparent
1289 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1290 * see text beyond the boundaries of the region while checking for a match.
1292 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1293 * lookbehind, and boundary matching constructs.
1295 * By default, a matcher uses opaque bounds.
1297 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1298 * @return This Matcher;
1301 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1305 * Return true if this matcher is using anchoring bounds.
1306 * By default, matchers use anchoring region bounds.
1308 * @return TRUE if this matcher is using anchoring bounds.
1311 virtual UBool
hasAnchoringBounds() const;
1315 * Set whether this matcher is using Anchoring Bounds for its region.
1316 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1317 * and end of the region. Without Anchoring Bounds, anchors will only match at
1318 * the positions they would in the complete text.
1320 * Anchoring Bounds are the default for regions.
1322 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1323 * @return This Matcher
1326 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1330 * Return TRUE if the most recent matching operation attempted to access
1331 * additional input beyond the available input text.
1332 * In this case, additional input text could change the results of the match.
1334 * hitEnd() is defined for both successful and unsuccessful matches.
1335 * In either case hitEnd() will return TRUE if if the end of the text was
1336 * reached at any point during the matching process.
1338 * @return TRUE if the most recent match hit the end of input
1341 virtual UBool
hitEnd() const;
1344 * Return TRUE the most recent match succeeded and additional input could cause
1345 * it to fail. If this method returns false and a match was found, then more input
1346 * might change the match but the match won't be lost. If a match was not found,
1347 * then requireEnd has no meaning.
1349 * @return TRUE if more input could cause the most recent match to no longer match.
1352 virtual UBool
requireEnd() const;
1356 * Returns the pattern that is interpreted by this matcher.
1357 * @return the RegexPattern for this RegexMatcher
1360 virtual const RegexPattern
&pattern() const;
1364 * Replaces every substring of the input that matches the pattern
1365 * with the given replacement string. This is a convenience function that
1366 * provides a complete find-and-replace-all operation.
1368 * This method first resets this matcher. It then scans the input string
1369 * looking for matches of the pattern. Input that is not part of any
1370 * match is left unchanged; each match is replaced in the result by the
1371 * replacement string. The replacement string may contain references to
1374 * @param replacement a string containing the replacement text.
1375 * @param status a reference to a UErrorCode to receive any errors.
1376 * @return a string containing the results of the find and replace.
1379 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
1383 * Replaces every substring of the input that matches the pattern
1384 * with the given replacement string. This is a convenience function that
1385 * provides a complete find-and-replace-all operation.
1387 * This method first resets this matcher. It then scans the input string
1388 * looking for matches of the pattern. Input that is not part of any
1389 * match is left unchanged; each match is replaced in the result by the
1390 * replacement string. The replacement string may contain references to
1393 * @param replacement a string containing the replacement text.
1394 * @param dest a mutable UText in which the results are placed.
1395 * If NULL, a new UText will be created (which may not be mutable).
1396 * @param status a reference to a UErrorCode to receive any errors.
1397 * @return a string containing the results of the find and replace.
1398 * If a pre-allocated UText was provided, it will always be used and returned.
1402 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1406 * Replaces the first substring of the input that matches
1407 * the pattern with the replacement string. This is a convenience
1408 * function that provides a complete find-and-replace operation.
1410 * This function first resets this RegexMatcher. It then scans the input string
1411 * looking for a match of the pattern. Input that is not part
1412 * of the match is appended directly to the result string; the match is replaced
1413 * in the result by the replacement string. The replacement string may contain
1414 * references to captured groups.
1416 * The state of the matcher (the position at which a subsequent find()
1417 * would begin) after completing a replaceFirst() is not specified. The
1418 * RegexMatcher should be reset before doing additional find() operations.
1420 * @param replacement a string containing the replacement text.
1421 * @param status a reference to a UErrorCode to receive any errors.
1422 * @return a string containing the results of the find and replace.
1425 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1429 * Replaces the first substring of the input that matches
1430 * the pattern with the replacement string. This is a convenience
1431 * function that provides a complete find-and-replace operation.
1433 * This function first resets this RegexMatcher. It then scans the input string
1434 * looking for a match of the pattern. Input that is not part
1435 * of the match is appended directly to the result string; the match is replaced
1436 * in the result by the replacement string. The replacement string may contain
1437 * references to captured groups.
1439 * The state of the matcher (the position at which a subsequent find()
1440 * would begin) after completing a replaceFirst() is not specified. The
1441 * RegexMatcher should be reset before doing additional find() operations.
1443 * @param replacement a string containing the replacement text.
1444 * @param dest a mutable UText in which the results are placed.
1445 * If NULL, a new UText will be created (which may not be mutable).
1446 * @param status a reference to a UErrorCode to receive any errors.
1447 * @return a string containing the results of the find and replace.
1448 * If a pre-allocated UText was provided, it will always be used and returned.
1452 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1456 * Implements a replace operation intended to be used as part of an
1457 * incremental find-and-replace.
1459 * The input string, starting from the end of the previous replacement and ending at
1460 * the start of the current match, is appended to the destination string. Then the
1461 * replacement string is appended to the output string,
1462 * including handling any substitutions of captured text.
1464 * For simple, prepackaged, non-incremental find-and-replace
1465 * operations, see replaceFirst() or replaceAll().
1467 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1468 * @param replacement A UnicodeString that provides the text to be substituted for
1469 * the input text that matched the regexp pattern. The replacement
1470 * text may contain references to captured text from the
1472 * @param status A reference to a UErrorCode to receive any errors. Possible
1473 * errors are U_REGEX_INVALID_STATE if no match has been
1474 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1475 * if the replacement text specifies a capture group that
1476 * does not exist in the pattern.
1478 * @return this RegexMatcher
1482 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1483 const UnicodeString
&replacement
, UErrorCode
&status
);
1487 * Implements a replace operation intended to be used as part of an
1488 * incremental find-and-replace.
1490 * The input string, starting from the end of the previous replacement and ending at
1491 * the start of the current match, is appended to the destination string. Then the
1492 * replacement string is appended to the output string,
1493 * including handling any substitutions of captured text.
1495 * For simple, prepackaged, non-incremental find-and-replace
1496 * operations, see replaceFirst() or replaceAll().
1498 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1500 * @param replacement A UText that provides the text to be substituted for
1501 * the input text that matched the regexp pattern. The replacement
1502 * text may contain references to captured text from the input.
1503 * @param status A reference to a UErrorCode to receive any errors. Possible
1504 * errors are U_REGEX_INVALID_STATE if no match has been
1505 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1506 * if the replacement text specifies a capture group that
1507 * does not exist in the pattern.
1509 * @return this RegexMatcher
1513 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1514 UText
*replacement
, UErrorCode
&status
);
1518 * As the final step in a find-and-replace operation, append the remainder
1519 * of the input string, starting at the position following the last appendReplacement(),
1520 * to the destination string. `appendTail()` is intended to be invoked after one
1521 * or more invocations of the `RegexMatcher::appendReplacement()`.
1523 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1524 * @return the destination string.
1527 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1531 * As the final step in a find-and-replace operation, append the remainder
1532 * of the input string, starting at the position following the last appendReplacement(),
1533 * to the destination string. `appendTail()` is intended to be invoked after one
1534 * or more invocations of the `RegexMatcher::appendReplacement()`.
1536 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1538 * @param status error cod
1539 * @return the destination string.
1543 virtual UText
*appendTail(UText
*dest
, UErrorCode
&status
);
1547 * Split a string into fields. Somewhat like %split() from Perl.
1548 * The pattern matches identify delimiters that separate the input
1549 * into fields. The input data between the matches becomes the
1550 * fields themselves.
1552 * @param input The string to be split into fields. The field delimiters
1553 * match the pattern (in the "this" object). This matcher
1554 * will be reset to this input string.
1555 * @param dest An array of UnicodeStrings to receive the results of the split.
1556 * This is an array of actual UnicodeString objects, not an
1557 * array of pointers to strings. Local (stack based) arrays can
1559 * @param destCapacity The number of elements in the destination array.
1560 * If the number of fields found is less than destCapacity, the
1561 * extra strings in the destination array are not altered.
1562 * If the number of destination strings is less than the number
1563 * of fields, the trailing part of the input string, including any
1564 * field delimiters, is placed in the last destination string.
1565 * @param status A reference to a UErrorCode to receive any errors.
1566 * @return The number of fields into which the input string was split.
1569 virtual int32_t split(const UnicodeString
&input
,
1570 UnicodeString dest
[],
1571 int32_t destCapacity
,
1572 UErrorCode
&status
);
1576 * Split a string into fields. Somewhat like %split() from Perl.
1577 * The pattern matches identify delimiters that separate the input
1578 * into fields. The input data between the matches becomes the
1579 * fields themselves.
1581 * @param input The string to be split into fields. The field delimiters
1582 * match the pattern (in the "this" object). This matcher
1583 * will be reset to this input string.
1584 * @param dest An array of mutable UText structs to receive the results of the split.
1585 * If a field is NULL, a new UText is allocated to contain the results for
1586 * that field. This new UText is not guaranteed to be mutable.
1587 * @param destCapacity The number of elements in the destination array.
1588 * If the number of fields found is less than destCapacity, the
1589 * extra strings in the destination array are not altered.
1590 * If the number of destination strings is less than the number
1591 * of fields, the trailing part of the input string, including any
1592 * field delimiters, is placed in the last destination string.
1593 * @param status A reference to a UErrorCode to receive any errors.
1594 * @return The number of fields into which the input string was split.
1598 virtual int32_t split(UText
*input
,
1600 int32_t destCapacity
,
1601 UErrorCode
&status
);
1604 * Set a processing time limit for match operations with this Matcher.
1606 * Some patterns, when matching certain strings, can run in exponential time.
1607 * For practical purposes, the match operation may appear to be in an
1609 * When a limit is set a match operation will fail with an error if the
1610 * limit is exceeded.
1612 * The units of the limit are steps of the match engine.
1613 * Correspondence with actual processor time will depend on the speed
1614 * of the processor and the details of the specific pattern, but will
1615 * typically be on the order of milliseconds.
1617 * By default, the matching time is not limited.
1620 * @param limit The limit value, or 0 for no limit.
1621 * @param status A reference to a UErrorCode to receive any errors.
1624 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1627 * Get the time limit, if any, for match operations made with this Matcher.
1629 * @return the maximum allowed time for a match, in units of processing steps.
1632 virtual int32_t getTimeLimit() const;
1635 * Set the amount of heap storage available for use by the match backtracking stack.
1636 * The matcher is also reset, discarding any results from previous matches.
1638 * ICU uses a backtracking regular expression engine, with the backtrack stack
1639 * maintained on the heap. This function sets the limit to the amount of memory
1640 * that can be used for this purpose. A backtracking stack overflow will
1641 * result in an error from the match operation that caused it.
1643 * A limit is desirable because a malicious or poorly designed pattern can use
1644 * excessive memory, potentially crashing the process. A limit is enabled
1647 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1648 * A value of zero means no limit.
1649 * The limit must be greater or equal to zero.
1651 * @param status A reference to a UErrorCode to receive any errors.
1655 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1658 * Get the size of the heap storage available for use by the back tracking stack.
1660 * @return the maximum backtracking stack size, in bytes, or zero if the
1661 * stack size is unlimited.
1664 virtual int32_t getStackLimit() const;
1668 * Set a callback function for use with this Matcher.
1669 * During matching operations the function will be called periodically,
1670 * giving the application the opportunity to terminate a long-running
1673 * @param callback A pointer to the user-supplied callback function.
1674 * @param context User context pointer. The value supplied at the
1675 * time the callback function is set will be saved
1676 * and passed to the callback each time that it is called.
1677 * @param status A reference to a UErrorCode to receive any errors.
1680 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1681 const void *context
,
1682 UErrorCode
&status
);
1686 * Get the callback function for this URegularExpression.
1688 * @param callback Out parameter, receives a pointer to the user-supplied
1689 * callback function.
1690 * @param context Out parameter, receives the user context pointer that
1691 * was set when uregex_setMatchCallback() was called.
1692 * @param status A reference to a UErrorCode to receive any errors.
1695 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1696 const void *&context
,
1697 UErrorCode
&status
);
1701 * Set a progress callback function for use with find operations on this Matcher.
1702 * During find operations, the callback will be invoked after each return from a
1703 * match attempt, giving the application the opportunity to terminate a long-running
1706 * @param callback A pointer to the user-supplied callback function.
1707 * @param context User context pointer. The value supplied at the
1708 * time the callback function is set will be saved
1709 * and passed to the callback each time that it is called.
1710 * @param status A reference to a UErrorCode to receive any errors.
1713 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1714 const void *context
,
1715 UErrorCode
&status
);
1719 * Get the find progress callback function for this URegularExpression.
1721 * @param callback Out parameter, receives a pointer to the user-supplied
1722 * callback function.
1723 * @param context Out parameter, receives the user context pointer that
1724 * was set when uregex_setFindProgressCallback() was called.
1725 * @param status A reference to a UErrorCode to receive any errors.
1728 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1729 const void *&context
,
1730 UErrorCode
&status
);
1732 #ifndef U_HIDE_INTERNAL_API
1734 * setTrace Debug function, enable/disable tracing of the matching engine.
1735 * For internal ICU development use only. DO NO USE!!!!
1738 void setTrace(UBool state
);
1739 #endif /* U_HIDE_INTERNAL_API */
1742 * ICU "poor man's RTTI", returns a UClassID for this class.
1746 static UClassID U_EXPORT2
getStaticClassID();
1749 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1753 virtual UClassID
getDynamicClassID() const;
1756 // Constructors and other object boilerplate are private.
1757 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1758 RegexMatcher(); // default constructor not implemented
1759 RegexMatcher(const RegexPattern
*pat
);
1760 RegexMatcher(const RegexMatcher
&other
);
1761 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1762 void init(UErrorCode
&status
); // Common initialization
1763 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1765 friend class RegexPattern
;
1766 friend class RegexCImpl
;
1768 #ifndef U_HIDE_INTERNAL_API
1770 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1771 #endif /* U_HIDE_INTERNAL_API */
1775 // MatchAt This is the internal interface to the match engine itself.
1776 // Match status comes back in matcher member variables.
1778 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1779 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1780 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1781 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1782 REStackFrame
*resetStack();
1783 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1784 void IncrementTime(UErrorCode
&status
);
1786 // Call user find callback function, if set. Return TRUE if operation should be interrupted.
1787 inline UBool
findProgressInterrupt(int64_t matchIndex
, UErrorCode
&status
);
1789 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1791 UBool
findUsingChunk(UErrorCode
&status
);
1792 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1793 UBool
isChunkWordBoundary(int32_t pos
);
1795 const RegexPattern
*fPattern
;
1796 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1797 // should delete it when through.
1799 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1800 UText
*fInputText
; // The text being matched. Is never NULL.
1801 UText
*fAltInputText
; // A shallow copy of the text being matched.
1802 // Only created if the pattern contains backreferences.
1803 int64_t fInputLength
; // Full length of the input text.
1804 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1806 int64_t fRegionStart
; // Start of the input region, default = 0.
1807 int64_t fRegionLimit
; // End of input region, default to input.length.
1809 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1810 int64_t fAnchorLimit
; // See useAnchoringBounds
1812 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1813 int64_t fLookLimit
; // and other boundary tests. See
1814 // useTransparentBounds
1816 int64_t fActiveStart
; // Currently active bounds for matching.
1817 int64_t fActiveLimit
; // Usually is the same as region, but
1818 // is changed to fLookStart/Limit when
1819 // entering look around regions.
1821 UBool fTransparentBounds
; // True if using transparent bounds.
1822 UBool fAnchoringBounds
; // True if using anchoring bounds.
1824 UBool fMatch
; // True if the last attempted match was successful.
1825 int64_t fMatchStart
; // Position of the start of the most recent match
1826 int64_t fMatchEnd
; // First position after the end of the most recent match
1827 // Zero if no previous match, even when a region
1829 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1830 // or -1 if there was no previous match.
1831 int64_t fAppendPosition
; // First position after the end of the previous
1832 // appendReplacement(). As described by the
1833 // JavaDoc for Java Matcher, where it is called
1834 // "append position"
1835 UBool fHitEnd
; // True if the last match touched the end of input.
1836 UBool fRequireEnd
; // True if the last match required end-of-input
1840 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1841 // which will contain the capture group results.
1842 // NOT valid while match engine is running.
1844 int64_t *fData
; // Data area for use by the compiled pattern.
1845 int64_t fSmallData
[8]; // Use this for data if it's enough.
1847 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1848 // match engine run. Zero for unlimited.
1850 int32_t fTime
; // Match time, accumulates while matching.
1851 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1852 // Kept separately from fTime to keep as much
1853 // code as possible out of the inline
1854 // StateSave function.
1856 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1857 // stack, in bytes. Zero for unlimited.
1859 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1860 // NULL if there is no callback.
1861 const void *fCallbackContext
; // User Context ptr for callback function.
1863 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1864 // NULL if there is no callback.
1865 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1868 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1870 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1872 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1873 // reported, or that permanently disables this matcher.
1875 RuleBasedBreakIterator
*fWordBreakItr
;
1879 #endif // U_SHOW_CPLUSPLUS_API
1881 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS