2 **********************************************************************
3 * Copyright (C) 2002-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 * created on: 2002oct22
11 * created by: Andy Heninger
13 * ICU Regular Expressions, API for C++
23 * \brief C++ API: Regular Expressions
25 * <h2>Regular Expression API</h2>
27 * <p>The ICU API for processing regular expressions consists of two classes,
28 * <code>RegexPattern</code> and <code>RegexMatcher</code>.
29 * <code>RegexPattern</code> objects represent a pre-processed, or compiled
30 * regular expression. They are created from a regular expression pattern string,
31 * and can be used to create <code>RegexMatcher</code> objects for the pattern.</p>
33 * <p>Class <code>RegexMatcher</code> bundles together a regular expression
34 * pattern and a target string to which the search pattern will be applied.
35 * <code>RegexMatcher</code> includes API for doing plain find or search
36 * operations, for search and replace operations, and for obtaining detailed
37 * information about bounds of a match. </p>
39 * <p>Note that by constructing <code>RegexMatcher</code> objects directly from regular
40 * expression pattern strings application code can be simplified and the explicit
41 * need for <code>RegexPattern</code> objects can usually be eliminated.
45 #include "unicode/utypes.h"
47 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
49 #include "unicode/uobject.h"
50 #include "unicode/unistr.h"
51 #include "unicode/utext.h"
52 #include "unicode/parseerr.h"
54 #include "unicode/uregex.h"
59 // Forward Declarations...
69 class RuleBasedBreakIterator
;
76 * RBBIPatternDump Debug function, displays the compiled form of a pattern.
80 U_INTERNAL
void U_EXPORT2
81 RegexPatternDump(const RegexPattern
*pat
);
83 #undef RegexPatternDump
84 #define RegexPatternDump(pat)
90 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
91 * factory methods for creating a RegexPattern object from the source (string) form
92 * of a regular expression, methods for creating RegexMatchers that allow the pattern
93 * to be applied to input text, and a few convenience methods for simple common
94 * uses of regular expressions.
96 * <p>Class RegexPattern is not intended to be subclassed.</p>
100 class U_I18N_API RegexPattern
: public UObject
{
104 * default constructor. Create a RegexPattern object that refers to no actual
105 * pattern. Not normally needed; RegexPattern objects are usually
106 * created using the factory method <code>compile()</code>.
113 * Copy Constructor. Create a new RegexPattern object that is equivalent
114 * to the source object.
115 * @param source the pattern object to be copied.
118 RegexPattern(const RegexPattern
&source
);
121 * Destructor. Note that a RegexPattern object must persist so long as any
122 * RegexMatcher objects that were created from the RegexPattern are active.
125 virtual ~RegexPattern();
128 * Comparison operator. Two RegexPattern objects are considered equal if they
129 * were constructed from identical source patterns using the same match flag
131 * @param that a RegexPattern object to compare with "this".
132 * @return TRUE if the objects are equivalent.
135 UBool
operator==(const RegexPattern
& that
) const;
138 * Comparison operator. Two RegexPattern objects are considered equal if they
139 * were constructed from identical source patterns using the same match flag
141 * @param that a RegexPattern object to compare with "this".
142 * @return TRUE if the objects are different.
145 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);};
148 * Assignment operator. After assignment, this RegexPattern will behave identically
149 * to the source object.
152 RegexPattern
&operator =(const RegexPattern
&source
);
155 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
156 * intended to be subclasses, <code>clone()</code> and the copy construction are
157 * equivalent operations.
158 * @return the copy of this RegexPattern
161 virtual RegexPattern
*clone() const;
165 * Compiles the regular expression in string form into a RegexPattern
166 * object. These compile methods, rather than the constructors, are the usual
167 * way that RegexPattern objects are created.
169 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
170 * objects created from the pattern are active. RegexMatchers keep a pointer
171 * back to their pattern, so premature deletion of the pattern is a
172 * catastrophic error.</p>
174 * <p>All pattern match mode flags are set to their default values.</p>
176 * <p>Note that it is often more convenient to construct a RegexMatcher directly
177 * from a pattern string rather than separately compiling the pattern and
178 * then creating a RegexMatcher object from the pattern.</p>
180 * @param regex The regular expression to be compiled.
181 * @param pe Receives the position (line and column nubers) of any error
182 * within the regular expression.)
183 * @param status A reference to a UErrorCode to receive any errors.
184 * @return A regexPattern object for the compiled pattern.
188 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
194 * Compiles the regular expression in string form into a RegexPattern
195 * object. These compile methods, rather than the constructors, are the usual
196 * way that RegexPattern objects are created.
198 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
199 * objects created from the pattern are active. RegexMatchers keep a pointer
200 * back to their pattern, so premature deletion of the pattern is a
201 * catastrophic error.</p>
203 * <p>All pattern match mode flags are set to their default values.</p>
205 * <p>Note that it is often more convenient to construct a RegexMatcher directly
206 * from a pattern string rather than separately compiling the pattern and
207 * then creating a RegexMatcher object from the pattern.</p>
209 * @param regex The regular expression to be compiled. Note, the text referred
210 * to by this UText must not be deleted during the lifetime of the
211 * RegexPattern object or any RegexMatcher object created from it.
212 * @param pe Receives the position (line and column nubers) of any error
213 * within the regular expression.)
214 * @param status A reference to a UErrorCode to receive any errors.
215 * @return A regexPattern object for the compiled pattern.
219 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
224 * Compiles the regular expression in string form into a RegexPattern
225 * object using the specified match mode flags. These compile methods,
226 * rather than the constructors, are the usual way that RegexPattern objects
229 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
230 * objects created from the pattern are active. RegexMatchers keep a pointer
231 * back to their pattern, so premature deletion of the pattern is a
232 * catastrophic error.</p>
234 * <p>Note that it is often more convenient to construct a RegexMatcher directly
235 * from a pattern string instead of than separately compiling the pattern and
236 * then creating a RegexMatcher object from the pattern.</p>
238 * @param regex The regular expression to be compiled.
239 * @param flags The match mode flags to be used.
240 * @param pe Receives the position (line and column numbers) of any error
241 * within the regular expression.)
242 * @param status A reference to a UErrorCode to receive any errors.
243 * @return A regexPattern object for the compiled pattern.
247 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
254 * Compiles the regular expression in string form into a RegexPattern
255 * object using the specified match mode flags. These compile methods,
256 * rather than the constructors, are the usual way that RegexPattern objects
259 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
260 * objects created from the pattern are active. RegexMatchers keep a pointer
261 * back to their pattern, so premature deletion of the pattern is a
262 * catastrophic error.</p>
264 * <p>Note that it is often more convenient to construct a RegexMatcher directly
265 * from a pattern string instead of than separately compiling the pattern and
266 * then creating a RegexMatcher object from the pattern.</p>
268 * @param regex The regular expression to be compiled. Note, the text referred
269 * to by this UText must not be deleted during the lifetime of the
270 * RegexPattern object or any RegexMatcher object created from it.
271 * @param flags The match mode flags to be used.
272 * @param pe Receives the position (line and column numbers) of any error
273 * within the regular expression.)
274 * @param status A reference to a UErrorCode to receive any errors.
275 * @return A regexPattern object for the compiled pattern.
279 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
286 * Compiles the regular expression in string form into a RegexPattern
287 * object using the specified match mode flags. These compile methods,
288 * rather than the constructors, are the usual way that RegexPattern objects
291 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
292 * objects created from the pattern are active. RegexMatchers keep a pointer
293 * back to their pattern, so premature deletion of the pattern is a
294 * catastrophic error.</p>
296 * <p>Note that it is often more convenient to construct a RegexMatcher directly
297 * from a pattern string instead of than separately compiling the pattern and
298 * then creating a RegexMatcher object from the pattern.</p>
300 * @param regex The regular expression to be compiled.
301 * @param flags The match mode flags to be used.
302 * @param status A reference to a UErrorCode to receive any errors.
303 * @return A regexPattern object for the compiled pattern.
307 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
313 * Compiles the regular expression in string form into a RegexPattern
314 * object using the specified match mode flags. These compile methods,
315 * rather than the constructors, are the usual way that RegexPattern objects
318 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
319 * objects created from the pattern are active. RegexMatchers keep a pointer
320 * back to their pattern, so premature deletion of the pattern is a
321 * catastrophic error.</p>
323 * <p>Note that it is often more convenient to construct a RegexMatcher directly
324 * from a pattern string instead of than separately compiling the pattern and
325 * then creating a RegexMatcher object from the pattern.</p>
327 * @param regex The regular expression to be compiled. Note, the text referred
328 * to by this UText must not be deleted during the lifetime of the
329 * RegexPattern object or any RegexMatcher object created from it.
330 * @param flags The match mode flags to be used.
331 * @param status A reference to a UErrorCode to receive any errors.
332 * @return A regexPattern object for the compiled pattern.
336 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
342 * Get the match mode flags that were used when compiling this pattern.
343 * @return the match mode flags
346 virtual uint32_t flags() const;
349 * Creates a RegexMatcher that will match the given input against this pattern. The
350 * RegexMatcher can then be used to perform match, find or replace operations
351 * on the input. Note that a RegexPattern object must not be deleted while
352 * RegexMatchers created from it still exist and might possibly be used again.
354 * The matcher will retain a reference to the supplied input string, and all regexp
355 * pattern matching operations happen directly on this original string. It is
356 * critical that the string not be altered or deleted before use by the regular
357 * expression operations is complete.
359 * @param input The input string to which the regular expression will be applied.
360 * @param status A reference to a UErrorCode to receive any errors.
361 * @return A RegexMatcher object for this pattern and input.
365 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
366 UErrorCode
&status
) const;
370 * Flag to disambiguate RegexPattern::matcher signature
373 enum PatternIsUTextFlag
{ PATTERN_IS_UTEXT
};
376 * Creates a RegexMatcher that will match the given input against this pattern. The
377 * RegexMatcher can then be used to perform match, find or replace operations
378 * on the input. Note that a RegexPattern object must not be deleted while
379 * RegexMatchers created from it still exist and might possibly be used again.
381 * The matcher will make a shallow clone of the supplied input text, and all regexp
382 * pattern matching operations happen on this clone. While read-only operations on
383 * the supplied text are permitted, it is critical that the underlying string not be
384 * altered or deleted before use by the regular expression operations is complete.
386 * @param input The input text to which the regular expression will be applied.
387 * @param flag Must be RegexPattern::PATTERN_IS_UTEXT; used to disambiguate
389 * @param status A reference to a UErrorCode to receive any errors.
390 * @return A RegexMatcher object for this pattern and input.
394 virtual RegexMatcher
*matcher(UText
*input
,
395 PatternIsUTextFlag flag
,
396 UErrorCode
&status
) const;
400 * Cause a compilation error if an application accidently attempts to
401 * create a matcher with a (UChar *) string as input rather than
402 * a UnicodeString. Avoids a dangling reference to a temporary string.
404 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
405 * using one of the aliasing constructors, such as
406 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
407 * or in a UText, using
408 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
412 RegexMatcher
*matcher(const UChar
*input
,
413 UErrorCode
&status
) const;
418 * Creates a RegexMatcher that will match against this pattern. The
419 * RegexMatcher can be used to perform match, find or replace operations.
420 * Note that a RegexPattern object must not be deleted while
421 * RegexMatchers created from it still exist and might possibly be used again.
423 * @param status A reference to a UErrorCode to receive any errors.
424 * @return A RegexMatcher object for this pattern and input.
428 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
432 * Test whether a string matches a regular expression. This convenience function
433 * both compiles the reguluar expression and applies it in a single operation.
434 * Note that if the same pattern needs to be applied repeatedly, this method will be
435 * less efficient than creating and reusing a RegexMatcher object.
437 * @param regex The regular expression
438 * @param input The string data to be matched
439 * @param pe Receives the position of any syntax errors within the regular expression
440 * @param status A reference to a UErrorCode to receive any errors.
441 * @return True if the regular expression exactly matches the full input string.
445 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
446 const UnicodeString
&input
,
452 * Test whether a string matches a regular expression. This convenience function
453 * both compiles the reguluar expression and applies it in a single operation.
454 * Note that if the same pattern needs to be applied repeatedly, this method will be
455 * less efficient than creating and reusing a RegexMatcher object.
457 * @param regex The regular expression
458 * @param input The string data to be matched
459 * @param pe Receives the position of any syntax errors within the regular expression
460 * @param status A reference to a UErrorCode to receive any errors.
461 * @return True if the regular expression exactly matches the full input string.
465 static UBool U_EXPORT2
matches(UText
*regex
,
472 * Returns the regular expression from which this pattern was compiled. This method will work
473 * even if the pattern was compiled from a UText.
475 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
476 * the returned string may no longer reflect the RegexPattern object.
479 virtual UnicodeString
pattern() const;
483 * Returns the regular expression from which this pattern was compiled. This method will work
484 * even if the pattern was compiled from a UnicodeString.
486 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
487 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
492 virtual UText
*patternText(UErrorCode
&status
) const;
496 * Split a string into fields. Somewhat like split() from Perl.
497 * The pattern matches identify delimiters that separate the input
498 * into fields. The input data between the matches becomes the
501 * For the best performance on split() operations,
502 * <code>RegexMatcher::split</code> is perferable to this function
504 * @param input The string to be split into fields. The field delimiters
505 * match the pattern (in the "this" object)
506 * @param dest An array of UnicodeStrings to receive the results of the split.
507 * This is an array of actual UnicodeString objects, not an
508 * array of pointers to strings. Local (stack based) arrays can
510 * @param destCapacity The number of elements in the destination array.
511 * If the number of fields found is less than destCapacity, the
512 * extra strings in the destination array are not altered.
513 * If the number of destination strings is less than the number
514 * of fields, the trailing part of the input string, including any
515 * field delimiters, is placed in the last destination string.
516 * @param status A reference to a UErrorCode to receive any errors.
517 * @return The number of fields into which the input string was split.
520 virtual int32_t split(const UnicodeString
&input
,
521 UnicodeString dest
[],
522 int32_t destCapacity
,
523 UErrorCode
&status
) const;
527 * Split a string into fields. Somewhat like split() from Perl.
528 * The pattern matches identify delimiters that separate the input
529 * into fields. The input data between the matches becomes the
532 * For the best performance on split() operations,
533 * <code>RegexMatcher::split</code> is perferable to this function
535 * @param input The string to be split into fields. The field delimiters
536 * match the pattern (in the "this" object)
537 * @param dest An array of mutable UText structs to receive the results of the split.
538 * If a field is NULL, a new UText is allocated to contain the results for
539 * that field. This new UText is not guaranteed to be mutable.
540 * @param destCapacity The number of elements in the destination array.
541 * If the number of fields found is less than destCapacity, the
542 * extra strings in the destination array are not altered.
543 * If the number of destination strings is less than the number
544 * of fields, the trailing part of the input string, including any
545 * field delimiters, is placed in the last destination string.
546 * @param status A reference to a UErrorCode to receive any errors.
547 * @return The number of fields into which the input string was split.
551 virtual int32_t split(UText
*input
,
553 int32_t destCapacity
,
554 UErrorCode
&status
) const;
558 * ICU "poor man's RTTI", returns a UClassID for the actual class.
562 virtual UClassID
getDynamicClassID() const;
565 * ICU "poor man's RTTI", returns a UClassID for this class.
569 static UClassID U_EXPORT2
getStaticClassID();
573 // Implementation Data
575 UText
*fPattern
; // The original pattern string.
576 UnicodeString
*fPatternString
; // The original pattern UncodeString if relevant
577 uint32_t fFlags
; // The flags used when compiling the pattern.
579 UVector64
*fCompiledPat
; // The compiled pattern p-code.
580 UnicodeString fLiteralText
; // Any literal string data from the pattern,
581 // after un-escaping, for use during the match.
583 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
584 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
587 UErrorCode fDeferredStatus
; // status if some prior error has left this
588 // RegexPattern in an unusable state.
590 int32_t fMinMatchLen
; // Minimum Match Length. All matches will have length
591 // >= this value. For some patterns, this calculated
592 // value may be less than the true shortest
595 int32_t fFrameSize
; // Size of a state stack frame in the
598 int32_t fDataSize
; // The size of the data needed by the pattern that
599 // does not go on the state stack, but has just
600 // a single copy per matcher.
602 UVector32
*fGroupMap
; // Map from capture group number to position of
603 // the group's variables in the matcher stack frame.
605 int32_t fMaxCaptureDigits
;
607 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
608 // regex character classes, e.g. Word.
610 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
611 // sets for predefined regex classes.
613 int32_t fStartType
; // Info on how a match must start.
614 int32_t fInitialStringIdx
; //
615 int32_t fInitialStringLen
;
616 UnicodeSet
*fInitialChars
;
617 UChar32 fInitialChar
;
618 Regex8BitSet
*fInitialChars8
;
619 UBool fNeedsAltInput
;
621 friend class RegexCompile
;
622 friend class RegexMatcher
;
623 friend class RegexCImpl
;
626 // Implementation Methods
628 void init(); // Common initialization, for use by constructors.
629 void zap(); // Common cleanup
631 void dumpOp(int32_t index
) const;
632 friend void U_EXPORT2
RegexPatternDump(const RegexPattern
*);
640 * class RegexMatcher bundles together a reular expression pattern and
641 * input text to which the expression can be applied. It includes methods
642 * for testing for matches, and for find and replace operations.
644 * <p>Class RegexMatcher is not intended to be subclassed.</p>
648 class U_I18N_API RegexMatcher
: public UObject
{
652 * Construct a RegexMatcher for a regular expression.
653 * This is a convenience method that avoids the need to explicitly create
654 * a RegexPattern object. Note that if several RegexMatchers need to be
655 * created for the same expression, it will be more efficient to
656 * separately create and cache a RegexPattern object, and use
657 * its matcher() method to create the RegexMatcher objects.
659 * @param regexp The Regular Expression to be compiled.
660 * @param flags Regular expression options, such as case insensitive matching.
661 * @see UREGEX_CASE_INSENSITIVE
662 * @param status Any errors are reported by setting this UErrorCode variable.
665 RegexMatcher(const UnicodeString
®exp
, uint32_t flags
, UErrorCode
&status
);
668 * Construct a RegexMatcher for a regular expression.
669 * This is a convenience method that avoids the need to explicitly create
670 * a RegexPattern object. Note that if several RegexMatchers need to be
671 * created for the same expression, it will be more efficient to
672 * separately create and cache a RegexPattern object, and use
673 * its matcher() method to create the RegexMatcher objects.
675 * @param regexp The regular expression to be compiled.
676 * @param flags Regular expression options, such as case insensitive matching.
677 * @see UREGEX_CASE_INSENSITIVE
678 * @param status Any errors are reported by setting this UErrorCode variable.
682 RegexMatcher(UText
*regexp
, 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 * The matcher will retain a reference to the supplied input string, and all regexp
693 * pattern matching operations happen directly on the original string. It is
694 * critical that the string not be altered or deleted before use by the regular
695 * expression operations is complete.
697 * @param regexp The Regular Expression to be compiled.
698 * @param input The string to match. The matcher retains a reference to the
699 * caller's string; mo copy is made.
700 * @param flags Regular expression options, such as case insensitive matching.
701 * @see UREGEX_CASE_INSENSITIVE
702 * @param status Any errors are reported by setting this UErrorCode variable.
705 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
706 uint32_t flags
, UErrorCode
&status
);
709 * Construct a RegexMatcher for a regular expression.
710 * This is a convenience method that avoids the need to explicitly create
711 * a RegexPattern object. Note that if several RegexMatchers need to be
712 * created for the same expression, it will be more efficient to
713 * separately create and cache a RegexPattern object, and use
714 * its matcher() method to create the RegexMatcher objects.
716 * The matcher will make a shallow clone of the supplied input text, and all regexp
717 * pattern matching operations happen on this clone. While read-only operations on
718 * the supplied text are permitted, it is critical that the underlying string not be
719 * altered or deleted before use by the regular expression operations is complete.
721 * @param regexp The Regular Expression to be compiled.
722 * @param input The string to match. The matcher retains a shallow clone of the text.
723 * @param flags Regular expression options, such as case insensitive matching.
724 * @see UREGEX_CASE_INSENSITIVE
725 * @param status Any errors are reported by setting this UErrorCode variable.
729 RegexMatcher(UText
*regexp
, UText
*input
,
730 uint32_t flags
, UErrorCode
&status
);
734 * Cause a compilation error if an application accidently attempts to
735 * create a matcher with a (UChar *) string as input rather than
736 * a UnicodeString. Avoids a dangling reference to a temporary string.
738 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
739 * using one of the aliasing constructors, such as
740 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
741 * or in a UText, using
742 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
746 RegexMatcher(const UnicodeString
®exp
, const UChar
*input
,
747 uint32_t flags
, UErrorCode
&status
);
756 virtual ~RegexMatcher();
760 * Attempts to match the entire input region against the pattern.
761 * @param status A reference to a UErrorCode to receive any errors.
762 * @return TRUE if there is a match
765 virtual UBool
matches(UErrorCode
&status
);
769 * Resets the matcher, then attempts to match the input beginning
770 * at the specified startIndex, and extending to the end of the input.
771 * The input region is reset to include the entire input string.
772 * A successful match must extend to the end of the input.
773 * @param startIndex The input string (native) index at which to begin matching.
774 * @param status A reference to a UErrorCode to receive any errors.
775 * @return TRUE if there is a match
778 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
782 * Attempts to match the input string, starting from the beginning of the region,
783 * against the pattern. Like the matches() method, this function
784 * always starts at the beginning of the input region;
785 * unlike that function, it does not require that the entire region be matched.
787 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
788 * <code>end()</code>, and <code>group()</code> functions.</p>
790 * @param status A reference to a UErrorCode to receive any errors.
791 * @return TRUE if there is a match at the start of the input string.
794 virtual UBool
lookingAt(UErrorCode
&status
);
798 * Attempts to match the input string, starting from the specified index, against the pattern.
799 * The match may be of any length, and is not required to extend to the end
800 * of the input string. Contrast with match().
802 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
803 * <code>end()</code>, and <code>group()</code> functions.</p>
805 * @param startIndex The input string (native) index at which to begin matching.
806 * @param status A reference to a UErrorCode to receive any errors.
807 * @return TRUE if there is a match.
810 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
814 * Find the next pattern match in the input string.
815 * The find begins searching the input at the location following the end of
816 * the previous match, or at the start of the string if there is no previous match.
817 * If a match is found, <code>start(), end()</code> and <code>group()</code>
818 * will provide more information regarding the match.
819 * <p>Note that if the input string is changed by the application,
820 * use find(startPos, status) instead of find(), because the saved starting
821 * position may not be valid with the altered input string.</p>
822 * @return TRUE if a match is found.
825 virtual UBool
find();
829 * Resets this RegexMatcher and then attempts to find the next substring of the
830 * input string that matches the pattern, starting at the specified index.
832 * @param start The (native) index in the input string to begin the search.
833 * @param status A reference to a UErrorCode to receive any errors.
834 * @return TRUE if a match is found.
837 virtual UBool
find(int64_t start
, UErrorCode
&status
);
841 * Returns a string containing the text matched by the previous match.
842 * If the pattern can match an empty string, an empty string may be returned.
843 * @param status A reference to a UErrorCode to receive any errors.
844 * Possible errors are U_REGEX_INVALID_STATE if no match
845 * has been attempted or the last match failed.
846 * @return a string containing the matched input text.
849 virtual UnicodeString
group(UErrorCode
&status
) const;
853 * Returns a string containing the text captured by the given group
854 * during the previous match operation. Group(0) is the entire match.
856 * @param groupNum the capture group number
857 * @param status A reference to a UErrorCode to receive any errors.
858 * Possible errors are U_REGEX_INVALID_STATE if no match
859 * has been attempted or the last match failed and
860 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
861 * @return the captured text
864 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
868 * Returns the number of capturing groups in this matcher's pattern.
869 * @return the number of capture groups
872 virtual int32_t groupCount() const;
876 * Returns a shallow clone of the entire live input string with the UText current native index
877 * set to the beginning of the requested group.
878 * Note that copying the entire input string may cause significant performance and memory issues.
879 * @param dest The UText into which the input should be copied, or NULL to create a new UText
880 * @param group_len A reference to receive the length of the desired capture group
881 * @param status A reference to a UErrorCode to receive any errors.
882 * Possible errors are U_REGEX_INVALID_STATE if no match
883 * has been attempted or the last match failed and
884 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
885 * @return dest if non-NULL, a shallow copy of the input text otherwise
889 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
894 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
897 * Returns a string containing the text captured by the given group
898 * during the previous match operation. Group(0) is the entire match.
900 * @param groupNum the capture group number
901 * @param dest A mutable UText in which the matching text is placed.
902 * If NULL, a new UText will be created (which may not be mutable).
903 * @param status A reference to a UErrorCode to receive any errors.
904 * Possible errors are U_REGEX_INVALID_STATE if no match
905 * has been attempted or the last match failed.
906 * @return A string containing the matched input text. If a pre-allocated UText
907 * was provided, it will always be used and returned.
909 * @internal ICU 4.4 technology preview
911 virtual UText
*group(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
915 * Returns the index in the input string of the start of the text matched
916 * during the previous match operation.
917 * @param status a reference to a UErrorCode to receive any errors.
918 * @return The (native) position in the input string of the start of the last match.
921 virtual int32_t start(UErrorCode
&status
) const;
926 virtual int64_t start64(UErrorCode
&status
) const;
930 * Returns the index in the input string of the start of the text matched by the
931 * specified capture group during the previous match operation. Return -1 if
932 * the capture group exists in the pattern, but was not part of the last match.
934 * @param group the capture group number
935 * @param status A reference to a UErrorCode to receive any errors. Possible
936 * errors are U_REGEX_INVALID_STATE if no match has been
937 * attempted or the last match failed, and
938 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
939 * @return the (native) start position of substring matched by the specified group.
942 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
947 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
951 * Returns the index in the input string of the first character following the
952 * text matched during the previous match operation.
953 * @param status A reference to a UErrorCode to receive any errors. Possible
954 * errors are U_REGEX_INVALID_STATE if no match has been
955 * attempted or the last match failed.
956 * @return the index of the last character matched, plus one.
957 * The index value returned is a native index, corresponding to
958 * code units for the underlying encoding type, for example,
959 * a byte index for UTF8.
962 virtual int32_t end(UErrorCode
&status
) const;
967 virtual int64_t end64(UErrorCode
&status
) const;
971 * Returns the index in the input string of the character following the
972 * text matched by the specified capture group during the previous match operation.
973 * @param group the capture group number
974 * @param status A reference to a UErrorCode to receive any errors. Possible
975 * errors are U_REGEX_INVALID_STATE if no match has been
976 * attempted or the last match failed and
977 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
978 * @return the index of the first character following the text
979 * captured by the specifed group during the previous match operation.
980 * Return -1 if the capture group exists in the pattern but was not part of the match.
981 * The index value returned is a native index, corresponding to
982 * code units for the underlying encoding type, for example,
983 * a byte index for UTF8.
986 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
991 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
995 * Resets this matcher. The effect is to remove any memory of previous matches,
996 * and to cause subsequent find() operations to begin at the beginning of
999 * @return this RegexMatcher.
1002 virtual RegexMatcher
&reset();
1006 * Resets this matcher, and set the current input position.
1007 * The effect is to remove any memory of previous matches,
1008 * and to cause subsequent find() operations to begin at
1009 * the specified (native) position in the input string.
1011 * The matcher's region is reset to its default, which is the entire
1014 * An alternative to this function is to set a match region
1015 * beginning at the desired index.
1017 * @return this RegexMatcher.
1020 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1024 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1025 * to be reused, which is more efficient than creating a new RegexMatcher for
1026 * each input string to be processed.
1027 * @param input The new string on which subsequent pattern matches will operate.
1028 * The matcher retains a reference to the callers string, and operates
1029 * directly on that. Ownership of the string remains with the caller.
1030 * Because no copy of the string is made, it is essential that the
1031 * caller not delete the string until after regexp operations on it
1033 * Note that while a reset on the matcher with an input string that is then
1034 * modified across/during matcher operations may be supported currently for UnicodeString,
1035 * this was not originally intended behavior, and support for this is not guaranteed
1036 * in upcoming versions of ICU.
1037 * @return this RegexMatcher.
1040 virtual RegexMatcher
&reset(const UnicodeString
&input
);
1044 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1045 * to be reused, which is more efficient than creating a new RegexMatcher for
1046 * each input string to be processed.
1047 * @param input The new string on which subsequent pattern matches will operate.
1048 * The matcher makes a shallow clone of the given text; ownership of the
1049 * original string remains with the caller. Because no deep copy of the
1050 * text is made, it is essential that the caller not modify the string
1051 * until after regexp operations on it are done.
1052 * @return this RegexMatcher.
1056 virtual RegexMatcher
&reset(UText
*input
);
1060 * Cause a compilation error if an application accidently attempts to
1061 * reset a matcher with a (UChar *) string as input rather than
1062 * a UnicodeString. Avoids a dangling reference to a temporary string.
1064 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
1065 * using one of the aliasing constructors, such as
1066 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
1067 * or in a UText, using
1068 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
1072 RegexMatcher
&reset(const UChar
*input
);
1076 * Returns the input string being matched. Ownership of the string belongs to
1077 * the matcher; it should not be altered or deleted. This method will work even if the input
1078 * was originally supplied as a UText.
1079 * @return the input string
1082 virtual const UnicodeString
&input() const;
1085 * Returns the input string being matched. This is the live input text; it should not be
1086 * altered or deleted. This method will work even if the input was originally supplied as
1088 * @return the input text
1092 virtual UText
*inputText() const;
1095 * Returns the input string being matched, either by copying it into the provided
1096 * UText parameter or by returning a shallow clone of the live input. Note that copying
1097 * the entire input may cause significant performance and memory issues.
1098 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1099 * @return dest if non-NULL, a shallow copy of the input text otherwise
1103 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1106 /** Sets the limits of this matcher's region.
1107 * The region is the part of the input string that will be searched to find a match.
1108 * Invoking this method resets the matcher, and then sets the region to start
1109 * at the index specified by the start parameter and end at the index specified
1110 * by the end parameter.
1112 * Depending on the transparency and anchoring being used (see useTransparentBounds
1113 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1114 * at or around the boundaries of the region
1116 * The function will fail if start is greater than limit, or if either index
1117 * is less than zero or greater than the length of the string being matched.
1119 * @param start The (native) index to begin searches at.
1120 * @param limit The index to end searches at (exclusive).
1121 * @param status A reference to a UErrorCode to receive any errors.
1124 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1127 * Identical to region(start, limit, status) but also allows a start position without
1128 * resetting the region state.
1129 * @param startIndex The (native) index within the region bounds at which to begin searches.
1130 * @param status A reference to a UErrorCode to receive any errors.
1131 * If startIndex is not within the specified region bounds,
1132 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1135 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
1138 * Reports the start index of this matcher's region. The searches this matcher
1139 * conducts are limited to finding matches within regionStart (inclusive) and
1140 * regionEnd (exclusive).
1142 * @return The starting (native) index of this matcher's region.
1145 virtual int32_t regionStart() const;
1150 virtual int64_t regionStart64() const;
1154 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1155 * this matcher conducts are limited to finding matches within regionStart
1156 * (inclusive) and regionEnd (exclusive).
1158 * @return The ending point (native) of this matcher's region.
1161 virtual int32_t regionEnd() const;
1166 virtual int64_t regionEnd64() const;
1169 * Queries the transparency of region bounds for this matcher.
1170 * See useTransparentBounds for a description of transparent and opaque bounds.
1171 * By default, a matcher uses opaque region boundaries.
1173 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1176 virtual UBool
hasTransparentBounds() const;
1179 * Sets the transparency of region bounds for this matcher.
1180 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1181 * If the boolean argument is false, then opaque bounds will be used.
1183 * Using transparent bounds, the boundaries of this matcher's region are transparent
1184 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1185 * see text beyond the boundaries of the region while checking for a match.
1187 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1188 * lookbehind, and boundary matching constructs.
1190 * By default, a matcher uses opaque bounds.
1192 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1193 * @return This Matcher;
1196 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1200 * Return true if this matcher is using anchoring bounds.
1201 * By default, matchers use anchoring region boounds.
1203 * @return TRUE if this matcher is using anchoring bounds.
1206 virtual UBool
hasAnchoringBounds() const;
1210 * Set whether this matcher is using Anchoring Bounds for its region.
1211 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1212 * and end of the region. Without Anchoring Bounds, anchors will only match at
1213 * the positions they would in the complete text.
1215 * Anchoring Bounds are the default for regions.
1217 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1218 * @return This Matcher
1221 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1225 * Return TRUE if the most recent matching operation touched the
1226 * end of the text being processed. In this case, additional input text could
1227 * change the results of that match.
1229 * hitEnd() is defined for both successful and unsuccessful matches.
1230 * In either case hitEnd() will return TRUE if if the end of the text was
1231 * reached at any point during the matching process.
1233 * @return TRUE if the most recent match hit the end of input
1236 virtual UBool
hitEnd() const;
1239 * Return TRUE the most recent match succeeded and additional input could cause
1240 * it to fail. If this method returns false and a match was found, then more input
1241 * might change the match but the match won't be lost. If a match was not found,
1242 * then requireEnd has no meaning.
1244 * @return TRUE if more input could cause the most recent match to no longer match.
1247 virtual UBool
requireEnd() const;
1251 * Returns the pattern that is interpreted by this matcher.
1252 * @return the RegexPattern for this RegexMatcher
1255 virtual const RegexPattern
&pattern() const;
1259 * Replaces every substring of the input that matches the pattern
1260 * with the given replacement string. This is a convenience function that
1261 * provides a complete find-and-replace-all operation.
1263 * This method first resets this matcher. It then scans the input string
1264 * looking for matches of the pattern. Input that is not part of any
1265 * match is left unchanged; each match is replaced in the result by the
1266 * replacement string. The replacement string may contain references to
1269 * @param replacement a string containing the replacement text.
1270 * @param status a reference to a UErrorCode to receive any errors.
1271 * @return a string containing the results of the find and replace.
1274 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
1278 * Replaces every substring of the input that matches the pattern
1279 * with the given replacement string. This is a convenience function that
1280 * provides a complete find-and-replace-all operation.
1282 * This method first resets this matcher. It then scans the input string
1283 * looking for matches of the pattern. Input that is not part of any
1284 * match is left unchanged; each match is replaced in the result by the
1285 * replacement string. The replacement string may contain references to
1288 * @param replacement a string containing the replacement text.
1289 * @param dest a mutable UText in which the results are placed.
1290 * If NULL, a new UText will be created (which may not be mutable).
1291 * @param status a reference to a UErrorCode to receive any errors.
1292 * @return a string containing the results of the find and replace.
1293 * If a pre-allocated UText was provided, it will always be used and returned.
1297 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1301 * Replaces the first substring of the input that matches
1302 * the pattern with the replacement string. This is a convenience
1303 * function that provides a complete find-and-replace operation.
1305 * <p>This function first resets this RegexMatcher. It then scans the input string
1306 * looking for a match of the pattern. Input that is not part
1307 * of the match is appended directly to the result string; the match is replaced
1308 * in the result by the replacement string. The replacement string may contain
1309 * references to captured groups.</p>
1311 * <p>The state of the matcher (the position at which a subsequent find()
1312 * would begin) after completing a replaceFirst() is not specified. The
1313 * RegexMatcher should be reset before doing additional find() operations.</p>
1315 * @param replacement a string containing the replacement text.
1316 * @param status a reference to a UErrorCode to receive any errors.
1317 * @return a string containing the results of the find and replace.
1320 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1324 * Replaces the first substring of the input that matches
1325 * the pattern with the replacement string. This is a convenience
1326 * function that provides a complete find-and-replace operation.
1328 * <p>This function first resets this RegexMatcher. It then scans the input string
1329 * looking for a match of the pattern. Input that is not part
1330 * of the match is appended directly to the result string; the match is replaced
1331 * in the result by the replacement string. The replacement string may contain
1332 * references to captured groups.</p>
1334 * <p>The state of the matcher (the position at which a subsequent find()
1335 * would begin) after completing a replaceFirst() is not specified. The
1336 * RegexMatcher should be reset before doing additional find() operations.</p>
1338 * @param replacement a string containing the replacement text.
1339 * @param dest a mutable UText in which the results are placed.
1340 * If NULL, a new UText will be created (which may not be mutable).
1341 * @param status a reference to a UErrorCode to receive any errors.
1342 * @return a string containing the results of the find and replace.
1343 * If a pre-allocated UText was provided, it will always be used and returned.
1347 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1351 * Implements a replace operation intended to be used as part of an
1352 * incremental find-and-replace.
1354 * <p>The input string, starting from the end of the previous replacement and ending at
1355 * the start of the current match, is appended to the destination string. Then the
1356 * replacement string is appended to the output string,
1357 * including handling any substitutions of captured text.</p>
1359 * <p>For simple, prepackaged, non-incremental find-and-replace
1360 * operations, see replaceFirst() or replaceAll().</p>
1362 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1363 * @param replacement A UnicodeString that provides the text to be substituted for
1364 * the input text that matched the regexp pattern. The replacement
1365 * text may contain references to captured text from the
1367 * @param status A reference to a UErrorCode to receive any errors. Possible
1368 * errors are U_REGEX_INVALID_STATE if no match has been
1369 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1370 * if the replacement text specifies a capture group that
1371 * does not exist in the pattern.
1373 * @return this RegexMatcher
1377 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1378 const UnicodeString
&replacement
, UErrorCode
&status
);
1382 * Implements a replace operation intended to be used as part of an
1383 * incremental find-and-replace.
1385 * <p>The input string, starting from the end of the previous replacement and ending at
1386 * the start of the current match, is appended to the destination string. Then the
1387 * replacement string is appended to the output string,
1388 * including handling any substitutions of captured text.</p>
1390 * <p>For simple, prepackaged, non-incremental find-and-replace
1391 * operations, see replaceFirst() or replaceAll().</p>
1393 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1395 * @param replacement A UText that provides the text to be substituted for
1396 * the input text that matched the regexp pattern. The replacement
1397 * text may contain references to captured text from the input.
1398 * @param status A reference to a UErrorCode to receive any errors. Possible
1399 * errors are U_REGEX_INVALID_STATE if no match has been
1400 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1401 * if the replacement text specifies a capture group that
1402 * does not exist in the pattern.
1404 * @return this RegexMatcher
1408 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1409 UText
*replacement
, UErrorCode
&status
);
1413 * As the final step in a find-and-replace operation, append the remainder
1414 * of the input string, starting at the position following the last appendReplacement(),
1415 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1416 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1418 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1419 * @return the destination string.
1422 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1426 * As the final step in a find-and-replace operation, append the remainder
1427 * of the input string, starting at the position following the last appendReplacement(),
1428 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1429 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1431 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1433 * @return the destination string.
1437 virtual UText
*appendTail(UText
*dest
, UErrorCode
&status
);
1441 * Split a string into fields. Somewhat like split() from Perl.
1442 * The pattern matches identify delimiters that separate the input
1443 * into fields. The input data between the matches becomes the
1444 * fields themselves.
1446 * @param input The string to be split into fields. The field delimiters
1447 * match the pattern (in the "this" object). This matcher
1448 * will be reset to this input string.
1449 * @param dest An array of UnicodeStrings to receive the results of the split.
1450 * This is an array of actual UnicodeString objects, not an
1451 * array of pointers to strings. Local (stack based) arrays can
1453 * @param destCapacity The number of elements in the destination array.
1454 * If the number of fields found is less than destCapacity, the
1455 * extra strings in the destination array are not altered.
1456 * If the number of destination strings is less than the number
1457 * of fields, the trailing part of the input string, including any
1458 * field delimiters, is placed in the last destination string.
1459 * @param status A reference to a UErrorCode to receive any errors.
1460 * @return The number of fields into which the input string was split.
1463 virtual int32_t split(const UnicodeString
&input
,
1464 UnicodeString dest
[],
1465 int32_t destCapacity
,
1466 UErrorCode
&status
);
1470 * Split a string into fields. Somewhat like split() from Perl.
1471 * The pattern matches identify delimiters that separate the input
1472 * into fields. The input data between the matches becomes the
1473 * fields themselves.
1475 * @param input The string to be split into fields. The field delimiters
1476 * match the pattern (in the "this" object). This matcher
1477 * will be reset to this input string.
1478 * @param dest An array of mutable UText structs to receive the results of the split.
1479 * If a field is NULL, a new UText is allocated to contain the results for
1480 * that field. This new UText is not guaranteed to be mutable.
1481 * @param destCapacity The number of elements in the destination array.
1482 * If the number of fields found is less than destCapacity, the
1483 * extra strings in the destination array are not altered.
1484 * If the number of destination strings is less than the number
1485 * of fields, the trailing part of the input string, including any
1486 * field delimiters, is placed in the last destination string.
1487 * @param status A reference to a UErrorCode to receive any errors.
1488 * @return The number of fields into which the input string was split.
1492 virtual int32_t split(UText
*input
,
1494 int32_t destCapacity
,
1495 UErrorCode
&status
);
1498 * Set a processing time limit for match operations with this Matcher.
1500 * Some patterns, when matching certain strings, can run in exponential time.
1501 * For practical purposes, the match operation may appear to be in an
1503 * When a limit is set a match operation will fail with an error if the
1504 * limit is exceeded.
1506 * The units of the limit are steps of the match engine.
1507 * Correspondence with actual processor time will depend on the speed
1508 * of the processor and the details of the specific pattern, but will
1509 * typically be on the order of milliseconds.
1511 * By default, the matching time is not limited.
1514 * @param limit The limit value, or 0 for no limit.
1515 * @param status A reference to a UErrorCode to receive any errors.
1518 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1521 * Get the time limit, if any, for match operations made with this Matcher.
1523 * @return the maximum allowed time for a match, in units of processing steps.
1526 virtual int32_t getTimeLimit() const;
1529 * Set the amount of heap storage avaliable for use by the match backtracking stack.
1530 * The matcher is also reset, discarding any results from previous matches.
1532 * ICU uses a backtracking regular expression engine, with the backtrack stack
1533 * maintained on the heap. This function sets the limit to the amount of memory
1534 * that can be used for this purpose. A backtracking stack overflow will
1535 * result in an error from the match operation that caused it.
1537 * A limit is desirable because a malicious or poorly designed pattern can use
1538 * excessive memory, potentially crashing the process. A limit is enabled
1541 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1542 * A value of zero means no limit.
1543 * The limit must be greater or equal to zero.
1545 * @param status A reference to a UErrorCode to receive any errors.
1549 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1552 * Get the size of the heap storage available for use by the back tracking stack.
1554 * @return the maximum backtracking stack size, in bytes, or zero if the
1555 * stack size is unlimited.
1558 virtual int32_t getStackLimit() const;
1562 * Set a callback function for use with this Matcher.
1563 * During matching operations the function will be called periodically,
1564 * giving the application the opportunity to terminate a long-running
1567 * @param callback A pointer to the user-supplied callback function.
1568 * @param context User context pointer. The value supplied at the
1569 * time the callback function is set will be saved
1570 * and passed to the callback each time that it is called.
1571 * @param status A reference to a UErrorCode to receive any errors.
1574 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1575 const void *context
,
1576 UErrorCode
&status
);
1580 * Get the callback function for this URegularExpression.
1582 * @param callback Out paramater, receives a pointer to the user-supplied
1583 * callback function.
1584 * @param context Out parameter, receives the user context pointer that
1585 * was set when uregex_setMatchCallback() was called.
1586 * @param status A reference to a UErrorCode to receive any errors.
1589 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1590 const void *&context
,
1591 UErrorCode
&status
);
1595 * Set a progress callback function for use with find operations on this Matcher.
1596 * During find operations, the callback will be invoked after each return from a
1597 * match attempt, giving the application the opportunity to terminate a long-running
1600 * @param callback A pointer to the user-supplied callback function.
1601 * @param context User context pointer. The value supplied at the
1602 * time the callback function is set will be saved
1603 * and passed to the callback each time that it is called.
1604 * @param status A reference to a UErrorCode to receive any errors.
1607 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1608 const void *context
,
1609 UErrorCode
&status
);
1613 * Get the find progress callback function for this URegularExpression.
1615 * @param callback Out paramater, receives a pointer to the user-supplied
1616 * callback function.
1617 * @param context Out parameter, receives the user context pointer that
1618 * was set when uregex_setFindProgressCallback() was called.
1619 * @param status A reference to a UErrorCode to receive any errors.
1622 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1623 const void *&context
,
1624 UErrorCode
&status
);
1628 * setTrace Debug function, enable/disable tracing of the matching engine.
1629 * For internal ICU development use only. DO NO USE!!!!
1632 void setTrace(UBool state
);
1636 * ICU "poor man's RTTI", returns a UClassID for this class.
1640 static UClassID U_EXPORT2
getStaticClassID();
1643 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1647 virtual UClassID
getDynamicClassID() const;
1650 // Constructors and other object boilerplate are private.
1651 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1652 RegexMatcher(); // default constructor not implemented
1653 RegexMatcher(const RegexPattern
*pat
);
1654 RegexMatcher(const RegexMatcher
&other
);
1655 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1656 void init(UErrorCode
&status
); // Common initialization
1657 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1659 friend class RegexPattern
;
1660 friend class RegexCImpl
;
1663 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1667 // MatchAt This is the internal interface to the match engine itself.
1668 // Match status comes back in matcher member variables.
1670 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1671 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1672 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1673 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1674 REStackFrame
*resetStack();
1675 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1676 void IncrementTime(UErrorCode
&status
);
1677 UBool
ReportFindProgress(int64_t matchIndex
, UErrorCode
&status
);
1679 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1681 UBool
findUsingChunk();
1682 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1683 UBool
isChunkWordBoundary(int32_t pos
);
1685 const RegexPattern
*fPattern
;
1686 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1687 // should delete it when through.
1689 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1690 UText
*fInputText
; // The text being matched. Is never NULL.
1691 UText
*fAltInputText
; // A shallow copy of the text being matched.
1692 // Only created if the pattern contains backreferences.
1693 int64_t fInputLength
; // Full length of the input text.
1694 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1696 int64_t fRegionStart
; // Start of the input region, default = 0.
1697 int64_t fRegionLimit
; // End of input region, default to input.length.
1699 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1700 int64_t fAnchorLimit
; // See useAnchoringBounds
1702 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1703 int64_t fLookLimit
; // and other boundary tests. See
1704 // useTransparentBounds
1706 int64_t fActiveStart
; // Currently active bounds for matching.
1707 int64_t fActiveLimit
; // Usually is the same as region, but
1708 // is changed to fLookStart/Limit when
1709 // entering look around regions.
1711 UBool fTransparentBounds
; // True if using transparent bounds.
1712 UBool fAnchoringBounds
; // True if using anchoring bounds.
1714 UBool fMatch
; // True if the last attempted match was successful.
1715 int64_t fMatchStart
; // Position of the start of the most recent match
1716 int64_t fMatchEnd
; // First position after the end of the most recent match
1717 // Zero if no previous match, even when a region
1719 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1720 // or -1 if there was no previous match.
1721 int64_t fAppendPosition
; // First position after the end of the previous
1722 // appendReplacement(). As described by the
1723 // JavaDoc for Java Matcher, where it is called
1724 // "append position"
1725 UBool fHitEnd
; // True if the last match touched the end of input.
1726 UBool fRequireEnd
; // True if the last match required end-of-input
1730 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1731 // which will contain the capture group results.
1732 // NOT valid while match engine is running.
1734 int64_t *fData
; // Data area for use by the compiled pattern.
1735 int64_t fSmallData
[8]; // Use this for data if it's enough.
1737 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1738 // match engine run. Zero for unlimited.
1740 int32_t fTime
; // Match time, accumulates while matching.
1741 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1742 // Kept separately from fTime to keep as much
1743 // code as possible out of the inline
1744 // StateSave function.
1746 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1747 // stack, in bytes. Zero for unlimited.
1749 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1750 // NULL if there is no callback.
1751 const void *fCallbackContext
; // User Context ptr for callback function.
1753 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1754 // NULL if there is no callback.
1755 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1758 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1760 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1762 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1763 // reported, or that permanently disables this matcher.
1765 RuleBasedBreakIterator
*fWordBreakItr
;
1771 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS