2 **********************************************************************
3 * Copyright (C) 2002-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 * created on: 2002oct22
11 * created by: Andy Heninger
13 * ICU Regular Expressions, API for C++
23 * \brief C++ API: Regular Expressions
25 * <h2>Regular Expression API</h2>
27 * <p>The ICU API for processing regular expressions consists of two classes,
28 * <code>RegexPattern</code> and <code>RegexMatcher</code>.
29 * <code>RegexPattern</code> objects represent a pre-processed, or compiled
30 * regular expression. They are created from a regular expression pattern string,
31 * and can be used to create <code>RegexMatcher</code> objects for the pattern.</p>
33 * <p>Class <code>RegexMatcher</code> bundles together a regular expression
34 * pattern and a target string to which the search pattern will be applied.
35 * <code>RegexMatcher</code> includes API for doing plain find or search
36 * operations, for search and replace operations, and for obtaining detailed
37 * information about bounds of a match. </p>
39 * <p>Note that by constructing <code>RegexMatcher</code> objects directly from regular
40 * expression pattern strings application code can be simplified and the explicit
41 * need for <code>RegexPattern</code> objects can usually be eliminated.
45 #include "unicode/utypes.h"
47 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
49 #include "unicode/uobject.h"
50 #include "unicode/unistr.h"
51 #include "unicode/utext.h"
52 #include "unicode/parseerr.h"
54 #include "unicode/uregex.h"
56 // Forward Declarations
65 class RuleBasedBreakIterator
;
72 * RBBIPatternDump Debug function, displays the compiled form of a pattern.
76 U_INTERNAL
void U_EXPORT2
77 RegexPatternDump(const RegexPattern
*pat
);
79 #undef RegexPatternDump
80 #define RegexPatternDump(pat)
86 * Class <code>RegexPattern</code> represents a compiled regular expression. It includes
87 * factory methods for creating a RegexPattern object from the source (string) form
88 * of a regular expression, methods for creating RegexMatchers that allow the pattern
89 * to be applied to input text, and a few convenience methods for simple common
90 * uses of regular expressions.
92 * <p>Class RegexPattern is not intended to be subclassed.</p>
96 class U_I18N_API RegexPattern
: public UObject
{
100 * default constructor. Create a RegexPattern object that refers to no actual
101 * pattern. Not normally needed; RegexPattern objects are usually
102 * created using the factory method <code>compile()</code>.
109 * Copy Constructor. Create a new RegexPattern object that is equivalent
110 * to the source object.
111 * @param source the pattern object to be copied.
114 RegexPattern(const RegexPattern
&source
);
117 * Destructor. Note that a RegexPattern object must persist so long as any
118 * RegexMatcher objects that were created from the RegexPattern are active.
121 virtual ~RegexPattern();
124 * Comparison operator. Two RegexPattern objects are considered equal if they
125 * were constructed from identical source patterns using the same match flag
127 * @param that a RegexPattern object to compare with "this".
128 * @return TRUE if the objects are equivalent.
131 UBool
operator==(const RegexPattern
& that
) const;
134 * Comparison operator. Two RegexPattern objects are considered equal if they
135 * were constructed from identical source patterns using the same match flag
137 * @param that a RegexPattern object to compare with "this".
138 * @return TRUE if the objects are different.
141 inline UBool
operator!=(const RegexPattern
& that
) const {return ! operator ==(that
);}
144 * Assignment operator. After assignment, this RegexPattern will behave identically
145 * to the source object.
148 RegexPattern
&operator =(const RegexPattern
&source
);
151 * Create an exact copy of this RegexPattern object. Since RegexPattern is not
152 * intended to be subclasses, <code>clone()</code> and the copy construction are
153 * equivalent operations.
154 * @return the copy of this RegexPattern
157 virtual RegexPattern
*clone() const;
161 * Compiles the regular expression in string form into a RegexPattern
162 * object. These compile methods, rather than the constructors, are the usual
163 * way that RegexPattern objects are created.
165 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
166 * objects created from the pattern are active. RegexMatchers keep a pointer
167 * back to their pattern, so premature deletion of the pattern is a
168 * catastrophic error.</p>
170 * <p>All pattern match mode flags are set to their default values.</p>
172 * <p>Note that it is often more convenient to construct a RegexMatcher directly
173 * from a pattern string rather than separately compiling the pattern and
174 * then creating a RegexMatcher object from the pattern.</p>
176 * @param regex The regular expression to be compiled.
177 * @param pe Receives the position (line and column nubers) of any error
178 * within the regular expression.)
179 * @param status A reference to a UErrorCode to receive any errors.
180 * @return A regexPattern object for the compiled pattern.
184 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
189 * Compiles the regular expression in string form into a RegexPattern
190 * object. These compile methods, rather than the constructors, are the usual
191 * way that RegexPattern objects are created.
193 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
194 * objects created from the pattern are active. RegexMatchers keep a pointer
195 * back to their pattern, so premature deletion of the pattern is a
196 * catastrophic error.</p>
198 * <p>All pattern match mode flags are set to their default values.</p>
200 * <p>Note that it is often more convenient to construct a RegexMatcher directly
201 * from a pattern string rather than separately compiling the pattern and
202 * then creating a RegexMatcher object from the pattern.</p>
204 * @param regex The regular expression to be compiled. Note, the text referred
205 * to by this UText must not be deleted during the lifetime of the
206 * RegexPattern object or any RegexMatcher object created from it.
207 * @param pe Receives the position (line and column nubers) of any error
208 * within the regular expression.)
209 * @param status A reference to a UErrorCode to receive any errors.
210 * @return A regexPattern object for the compiled pattern.
214 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
219 * Compiles the regular expression in string form into a RegexPattern
220 * object using the specified match mode flags. These compile methods,
221 * rather than the constructors, are the usual way that RegexPattern objects
224 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
225 * objects created from the pattern are active. RegexMatchers keep a pointer
226 * back to their pattern, so premature deletion of the pattern is a
227 * catastrophic error.</p>
229 * <p>Note that it is often more convenient to construct a RegexMatcher directly
230 * from a pattern string instead of than separately compiling the pattern and
231 * then creating a RegexMatcher object from the pattern.</p>
233 * @param regex The regular expression to be compiled.
234 * @param flags The match mode flags to be used.
235 * @param pe Receives the position (line and column numbers) of any error
236 * within the regular expression.)
237 * @param status A reference to a UErrorCode to receive any errors.
238 * @return A regexPattern object for the compiled pattern.
242 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
248 * Compiles the regular expression in string form into a RegexPattern
249 * object using the specified match mode flags. These compile methods,
250 * rather than the constructors, are the usual way that RegexPattern objects
253 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
254 * objects created from the pattern are active. RegexMatchers keep a pointer
255 * back to their pattern, so premature deletion of the pattern is a
256 * catastrophic error.</p>
258 * <p>Note that it is often more convenient to construct a RegexMatcher directly
259 * from a pattern string instead of than separately compiling the pattern and
260 * then creating a RegexMatcher object from the pattern.</p>
262 * @param regex The regular expression to be compiled. Note, the text referred
263 * to by this UText must not be deleted during the lifetime of the
264 * RegexPattern object or any RegexMatcher object created from it.
265 * @param flags The match mode flags to be used.
266 * @param pe Receives the position (line and column numbers) of any error
267 * within the regular expression.)
268 * @param status A reference to a UErrorCode to receive any errors.
269 * @return A regexPattern object for the compiled pattern.
273 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
279 * Compiles the regular expression in string form into a RegexPattern
280 * object using the specified match mode flags. These compile methods,
281 * rather than the constructors, are the usual way that RegexPattern objects
284 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
285 * objects created from the pattern are active. RegexMatchers keep a pointer
286 * back to their pattern, so premature deletion of the pattern is a
287 * catastrophic error.</p>
289 * <p>Note that it is often more convenient to construct a RegexMatcher directly
290 * from a pattern string instead of than separately compiling the pattern and
291 * then creating a RegexMatcher object from the pattern.</p>
293 * @param regex The regular expression to be compiled.
294 * @param flags The match mode flags to be used.
295 * @param status A reference to a UErrorCode to receive any errors.
296 * @return A regexPattern object for the compiled pattern.
300 static RegexPattern
* U_EXPORT2
compile( const UnicodeString
®ex
,
305 * Compiles the regular expression in string form into a RegexPattern
306 * object using the specified match mode flags. These compile methods,
307 * rather than the constructors, are the usual way that RegexPattern objects
310 * <p>Note that RegexPattern objects must not be deleted while RegexMatcher
311 * objects created from the pattern are active. RegexMatchers keep a pointer
312 * back to their pattern, so premature deletion of the pattern is a
313 * catastrophic error.</p>
315 * <p>Note that it is often more convenient to construct a RegexMatcher directly
316 * from a pattern string instead of than separately compiling the pattern and
317 * then creating a RegexMatcher object from the pattern.</p>
319 * @param regex The regular expression to be compiled. Note, the text referred
320 * to by this UText must not be deleted during the lifetime of the
321 * RegexPattern object or any RegexMatcher object created from it.
322 * @param flags The match mode flags to be used.
323 * @param status A reference to a UErrorCode to receive any errors.
324 * @return A regexPattern object for the compiled pattern.
328 static RegexPattern
* U_EXPORT2
compile( UText
*regex
,
333 * Get the match mode flags that were used when compiling this pattern.
334 * @return the match mode flags
337 virtual uint32_t flags() const;
340 * Creates a RegexMatcher that will match the given input against this pattern. The
341 * RegexMatcher can then be used to perform match, find or replace operations
342 * on the input. Note that a RegexPattern object must not be deleted while
343 * RegexMatchers created from it still exist and might possibly be used again.
345 * The matcher will retain a reference to the supplied input string, and all regexp
346 * pattern matching operations happen directly on this original string. It is
347 * critical that the string not be altered or deleted before use by the regular
348 * expression operations is complete.
350 * @param input The input string to which the regular expression will be applied.
351 * @param status A reference to a UErrorCode to receive any errors.
352 * @return A RegexMatcher object for this pattern and input.
356 virtual RegexMatcher
*matcher(const UnicodeString
&input
,
357 UErrorCode
&status
) const;
361 * Cause a compilation error if an application accidentally attempts to
362 * create a matcher with a (UChar *) string as input rather than
363 * a UnicodeString. Avoids a dangling reference to a temporary string.
365 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
366 * using one of the aliasing constructors, such as
367 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
368 * or in a UText, using
369 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
373 RegexMatcher
*matcher(const UChar
*input
,
374 UErrorCode
&status
) const;
379 * Creates a RegexMatcher that will match against this pattern. The
380 * RegexMatcher can be used to perform match, find or replace operations.
381 * Note that a RegexPattern object must not be deleted while
382 * RegexMatchers created from it still exist and might possibly be used again.
384 * @param status A reference to a UErrorCode to receive any errors.
385 * @return A RegexMatcher object for this pattern and input.
389 virtual RegexMatcher
*matcher(UErrorCode
&status
) const;
393 * Test whether a string matches a regular expression. This convenience function
394 * both compiles the regular expression and applies it in a single operation.
395 * Note that if the same pattern needs to be applied repeatedly, this method will be
396 * less efficient than creating and reusing a RegexMatcher object.
398 * @param regex The regular expression
399 * @param input The string data to be matched
400 * @param pe Receives the position of any syntax errors within the regular expression
401 * @param status A reference to a UErrorCode to receive any errors.
402 * @return True if the regular expression exactly matches the full input string.
406 static UBool U_EXPORT2
matches(const UnicodeString
®ex
,
407 const UnicodeString
&input
,
412 * Test whether a string matches a regular expression. This convenience function
413 * both compiles the regular expression and applies it in a single operation.
414 * Note that if the same pattern needs to be applied repeatedly, this method will be
415 * less efficient than creating and reusing a RegexMatcher object.
417 * @param regex The regular expression
418 * @param input The string data to be matched
419 * @param pe Receives the position of any syntax errors within the regular expression
420 * @param status A reference to a UErrorCode to receive any errors.
421 * @return True if the regular expression exactly matches the full input string.
425 static UBool U_EXPORT2
matches(UText
*regex
,
431 * Returns the regular expression from which this pattern was compiled. This method will work
432 * even if the pattern was compiled from a UText.
434 * Note: If the pattern was originally compiled from a UText, and that UText was modified,
435 * the returned string may no longer reflect the RegexPattern object.
438 virtual UnicodeString
pattern() const;
442 * Returns the regular expression from which this pattern was compiled. This method will work
443 * even if the pattern was compiled from a UnicodeString.
445 * Note: This is the original input, not a clone. If the pattern was originally compiled from a
446 * UText, and that UText was modified, the returned UText may no longer reflect the RegexPattern
451 virtual UText
*patternText(UErrorCode
&status
) const;
455 * Split a string into fields. Somewhat like split() from Perl or Java.
456 * Pattern matches identify delimiters that separate the input
457 * into fields. The input data between the delimiters becomes the
460 * If the delimiter pattern includes capture groups, the captured text will
461 * also appear in the destination array of output strings, interspersed
462 * with the fields. This is similar to Perl, but differs from Java,
463 * which ignores the presence of capture groups in the pattern.
465 * Trailing empty fields will always be returned, assuming sufficient
466 * destination capacity. This differs from the default behavior for Java
467 * and Perl where trailing empty fields are not returned.
469 * The number of strings produced by the split operation is returned.
470 * This count includes the strings from capture groups in the delimiter pattern.
471 * This behavior differs from Java, which ignores capture groups.
473 * For the best performance on split() operations,
474 * <code>RegexMatcher::split</code> is preferable to this function
476 * @param input The string to be split into fields. The field delimiters
477 * match the pattern (in the "this" object)
478 * @param dest An array of UnicodeStrings to receive the results of the split.
479 * This is an array of actual UnicodeString objects, not an
480 * array of pointers to strings. Local (stack based) arrays can
482 * @param destCapacity The number of elements in the destination array.
483 * If the number of fields found is less than destCapacity, the
484 * extra strings in the destination array are not altered.
485 * If the number of destination strings is less than the number
486 * of fields, the trailing part of the input string, including any
487 * field delimiters, is placed in the last destination string.
488 * @param status A reference to a UErrorCode to receive any errors.
489 * @return The number of fields into which the input string was split.
492 virtual int32_t split(const UnicodeString
&input
,
493 UnicodeString dest
[],
494 int32_t destCapacity
,
495 UErrorCode
&status
) const;
499 * Split a string into fields. Somewhat like split() from Perl or Java.
500 * Pattern matches identify delimiters that separate the input
501 * into fields. The input data between the delimiters becomes the
504 * If the delimiter pattern includes capture groups, the captured text will
505 * also appear in the destination array of output strings, interspersed
506 * with the fields. This is similar to Perl, but differs from Java,
507 * which ignores the presence of capture groups in the pattern.
509 * Trailing empty fields will always be returned, assuming sufficient
510 * destination capacity. This differs from the default behavior for Java
511 * and Perl where trailing empty fields are not returned.
513 * The number of strings produced by the split operation is returned.
514 * This count includes the strings from capture groups in the delimiter pattern.
515 * This behavior differs from Java, which ignores capture groups.
517 * For the best performance on split() operations,
518 * <code>RegexMatcher::split</code> is preferable to this function
520 * @param input The string to be split into fields. The field delimiters
521 * match the pattern (in the "this" object)
522 * @param dest An array of mutable UText structs to receive the results of the split.
523 * If a field is NULL, a new UText is allocated to contain the results for
524 * that field. This new UText is not guaranteed to be mutable.
525 * @param destCapacity The number of elements in the destination array.
526 * If the number of fields found is less than destCapacity, the
527 * extra strings in the destination array are not altered.
528 * If the number of destination strings is less than the number
529 * of fields, the trailing part of the input string, including any
530 * field delimiters, is placed in the last destination string.
531 * @param status A reference to a UErrorCode to receive any errors.
532 * @return The number of destination strings used.
536 virtual int32_t split(UText
*input
,
538 int32_t destCapacity
,
539 UErrorCode
&status
) const;
543 * ICU "poor man's RTTI", returns a UClassID for the actual class.
547 virtual UClassID
getDynamicClassID() const;
550 * ICU "poor man's RTTI", returns a UClassID for this class.
554 static UClassID U_EXPORT2
getStaticClassID();
558 // Implementation Data
560 UText
*fPattern
; // The original pattern string.
561 UnicodeString
*fPatternString
; // The original pattern UncodeString if relevant
562 uint32_t fFlags
; // The flags used when compiling the pattern.
564 UVector64
*fCompiledPat
; // The compiled pattern p-code.
565 UnicodeString fLiteralText
; // Any literal string data from the pattern,
566 // after un-escaping, for use during the match.
568 UVector
*fSets
; // Any UnicodeSets referenced from the pattern.
569 Regex8BitSet
*fSets8
; // (and fast sets for latin-1 range.)
572 UErrorCode fDeferredStatus
; // status if some prior error has left this
573 // RegexPattern in an unusable state.
575 int32_t fMinMatchLen
; // Minimum Match Length. All matches will have length
576 // >= this value. For some patterns, this calculated
577 // value may be less than the true shortest
580 int32_t fFrameSize
; // Size of a state stack frame in the
583 int32_t fDataSize
; // The size of the data needed by the pattern that
584 // does not go on the state stack, but has just
585 // a single copy per matcher.
587 UVector32
*fGroupMap
; // Map from capture group number to position of
588 // the group's variables in the matcher stack frame.
590 int32_t fMaxCaptureDigits
;
592 UnicodeSet
**fStaticSets
; // Ptr to static (shared) sets for predefined
593 // regex character classes, e.g. Word.
595 Regex8BitSet
*fStaticSets8
; // Ptr to the static (shared) latin-1 only
596 // sets for predefined regex classes.
598 int32_t fStartType
; // Info on how a match must start.
599 int32_t fInitialStringIdx
; //
600 int32_t fInitialStringLen
;
601 UnicodeSet
*fInitialChars
;
602 UChar32 fInitialChar
;
603 Regex8BitSet
*fInitialChars8
;
604 UBool fNeedsAltInput
;
606 friend class RegexCompile
;
607 friend class RegexMatcher
;
608 friend class RegexCImpl
;
611 // Implementation Methods
613 void init(); // Common initialization, for use by constructors.
614 void zap(); // Common cleanup
616 void dumpOp(int32_t index
) const;
617 friend void U_EXPORT2
RegexPatternDump(const RegexPattern
*);
625 * class RegexMatcher bundles together a regular expression pattern and
626 * input text to which the expression can be applied. It includes methods
627 * for testing for matches, and for find and replace operations.
629 * <p>Class RegexMatcher is not intended to be subclassed.</p>
633 class U_I18N_API RegexMatcher
: public UObject
{
637 * Construct a RegexMatcher for a regular expression.
638 * This is a convenience method that avoids the need to explicitly create
639 * a RegexPattern object. Note that if several RegexMatchers need to be
640 * created for the same expression, it will be more efficient to
641 * separately create and cache a RegexPattern object, and use
642 * its matcher() method to create the RegexMatcher objects.
644 * @param regexp The Regular Expression to be compiled.
645 * @param flags Regular expression options, such as case insensitive matching.
646 * @see UREGEX_CASE_INSENSITIVE
647 * @param status Any errors are reported by setting this UErrorCode variable.
650 RegexMatcher(const UnicodeString
®exp
, uint32_t flags
, UErrorCode
&status
);
653 * Construct a RegexMatcher for a regular expression.
654 * This is a convenience method that avoids the need to explicitly create
655 * a RegexPattern object. Note that if several RegexMatchers need to be
656 * created for the same expression, it will be more efficient to
657 * separately create and cache a RegexPattern object, and use
658 * its matcher() method to create the RegexMatcher objects.
660 * @param regexp The regular expression to be compiled.
661 * @param flags Regular expression options, such as case insensitive matching.
662 * @see UREGEX_CASE_INSENSITIVE
663 * @param status Any errors are reported by setting this UErrorCode variable.
667 RegexMatcher(UText
*regexp
, uint32_t flags
, UErrorCode
&status
);
670 * Construct a RegexMatcher for a regular expression.
671 * This is a convenience method that avoids the need to explicitly create
672 * a RegexPattern object. Note that if several RegexMatchers need to be
673 * created for the same expression, it will be more efficient to
674 * separately create and cache a RegexPattern object, and use
675 * its matcher() method to create the RegexMatcher objects.
677 * The matcher will retain a reference to the supplied input string, and all regexp
678 * pattern matching operations happen directly on the original string. It is
679 * critical that the string not be altered or deleted before use by the regular
680 * expression operations is complete.
682 * @param regexp The Regular Expression to be compiled.
683 * @param input The string to match. The matcher retains a reference to the
684 * caller's string; mo copy is made.
685 * @param flags Regular expression options, such as case insensitive matching.
686 * @see UREGEX_CASE_INSENSITIVE
687 * @param status Any errors are reported by setting this UErrorCode variable.
690 RegexMatcher(const UnicodeString
®exp
, const UnicodeString
&input
,
691 uint32_t flags
, UErrorCode
&status
);
694 * Construct a RegexMatcher for a regular expression.
695 * This is a convenience method that avoids the need to explicitly create
696 * a RegexPattern object. Note that if several RegexMatchers need to be
697 * created for the same expression, it will be more efficient to
698 * separately create and cache a RegexPattern object, and use
699 * its matcher() method to create the RegexMatcher objects.
701 * The matcher will make a shallow clone of the supplied input text, and all regexp
702 * pattern matching operations happen on this clone. While read-only operations on
703 * the supplied text are permitted, it is critical that the underlying string not be
704 * altered or deleted before use by the regular expression operations is complete.
706 * @param regexp The Regular Expression to be compiled.
707 * @param input The string to match. The matcher retains a shallow clone of the text.
708 * @param flags Regular expression options, such as case insensitive matching.
709 * @see UREGEX_CASE_INSENSITIVE
710 * @param status Any errors are reported by setting this UErrorCode variable.
714 RegexMatcher(UText
*regexp
, UText
*input
,
715 uint32_t flags
, UErrorCode
&status
);
719 * Cause a compilation error if an application accidentally attempts to
720 * create a matcher with a (UChar *) string as input rather than
721 * a UnicodeString. Avoids a dangling reference to a temporary string.
723 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
724 * using one of the aliasing constructors, such as
725 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
726 * or in a UText, using
727 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
731 RegexMatcher(const UnicodeString
®exp
, const UChar
*input
,
732 uint32_t flags
, UErrorCode
&status
);
741 virtual ~RegexMatcher();
745 * Attempts to match the entire input region against the pattern.
746 * @param status A reference to a UErrorCode to receive any errors.
747 * @return TRUE if there is a match
750 virtual UBool
matches(UErrorCode
&status
);
754 * Resets the matcher, then attempts to match the input beginning
755 * at the specified startIndex, and extending to the end of the input.
756 * The input region is reset to include the entire input string.
757 * A successful match must extend to the end of the input.
758 * @param startIndex The input string (native) index at which to begin matching.
759 * @param status A reference to a UErrorCode to receive any errors.
760 * @return TRUE if there is a match
763 virtual UBool
matches(int64_t startIndex
, UErrorCode
&status
);
767 * Attempts to match the input string, starting from the beginning of the region,
768 * against the pattern. Like the matches() method, this function
769 * always starts at the beginning of the input region;
770 * unlike that function, it does not require that the entire region be matched.
772 * <p>If the match succeeds then more information can be obtained via the <code>start()</code>,
773 * <code>end()</code>, and <code>group()</code> functions.</p>
775 * @param status A reference to a UErrorCode to receive any errors.
776 * @return TRUE if there is a match at the start of the input string.
779 virtual UBool
lookingAt(UErrorCode
&status
);
783 * Attempts to match the input string, starting from the specified index, against the pattern.
784 * The match may be of any length, and is not required to extend to the end
785 * of the input string. Contrast with match().
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 startIndex The input string (native) index at which to begin matching.
791 * @param status A reference to a UErrorCode to receive any errors.
792 * @return TRUE if there is a match.
795 virtual UBool
lookingAt(int64_t startIndex
, UErrorCode
&status
);
799 * Find the next pattern match in the input string.
800 * The find begins searching the input at the location following the end of
801 * the previous match, or at the start of the string if there is no previous match.
802 * If a match is found, <code>start(), end()</code> and <code>group()</code>
803 * will provide more information regarding the match.
804 * <p>Note that if the input string is changed by the application,
805 * use find(startPos, status) instead of find(), because the saved starting
806 * position may not be valid with the altered input string.</p>
807 * @return TRUE if a match is found.
810 virtual UBool
find();
814 * Resets this RegexMatcher and then attempts to find the next substring of the
815 * input string that matches the pattern, starting at the specified index.
817 * @param start The (native) index in the input string to begin the search.
818 * @param status A reference to a UErrorCode to receive any errors.
819 * @return TRUE if a match is found.
822 virtual UBool
find(int64_t start
, UErrorCode
&status
);
826 * Returns a string containing the text matched by the previous match.
827 * If the pattern can match an empty string, an empty string may be returned.
828 * @param status A reference to a UErrorCode to receive any errors.
829 * Possible errors are U_REGEX_INVALID_STATE if no match
830 * has been attempted or the last match failed.
831 * @return a string containing the matched input text.
834 virtual UnicodeString
group(UErrorCode
&status
) const;
838 * Returns a string containing the text captured by the given group
839 * during the previous match operation. Group(0) is the entire match.
841 * @param groupNum the capture group number
842 * @param status A reference to a UErrorCode to receive any errors.
843 * Possible errors are U_REGEX_INVALID_STATE if no match
844 * has been attempted or the last match failed and
845 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
846 * @return the captured text
849 virtual UnicodeString
group(int32_t groupNum
, UErrorCode
&status
) const;
853 * Returns the number of capturing groups in this matcher's pattern.
854 * @return the number of capture groups
857 virtual int32_t groupCount() const;
861 * Returns a shallow clone of the entire live input string with the UText current native index
862 * set to the beginning of the requested group.
864 * @param dest The UText into which the input should be cloned, or NULL to create a new UText
865 * @param group_len A reference to receive the length of the desired capture group
866 * @param status A reference to a UErrorCode to receive any errors.
867 * Possible errors are U_REGEX_INVALID_STATE if no match
868 * has been attempted or the last match failed and
869 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
870 * @return dest if non-NULL, a shallow copy of the input text otherwise
874 virtual UText
*group(UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
877 * Returns a shallow clone of the entire live input string with the UText current native index
878 * set to the beginning of the requested group.
880 * @param groupNum The capture group number.
881 * @param dest The UText into which the input should be cloned, or NULL to create a new UText.
882 * @param group_len A reference to receive the length of the desired capture group
883 * @param status A reference to a UErrorCode to receive any errors.
884 * Possible errors are U_REGEX_INVALID_STATE if no match
885 * has been attempted or the last match failed and
886 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
887 * @return dest if non-NULL, a shallow copy of the input text otherwise
891 virtual UText
*group(int32_t groupNum
, UText
*dest
, int64_t &group_len
, UErrorCode
&status
) const;
894 * Returns a string containing the text captured by the given group
895 * during the previous match operation. Group(0) is the entire match.
897 * @param groupNum the capture group number
898 * @param dest A mutable UText in which the matching text is placed.
899 * If NULL, a new UText will be created (which may not be mutable).
900 * @param status A reference to a UErrorCode to receive any errors.
901 * Possible errors are U_REGEX_INVALID_STATE if no match
902 * has been attempted or the last match failed.
903 * @return A string containing the matched input text. If a pre-allocated UText
904 * was provided, it will always be used and returned.
906 * @internal ICU 4.4 technology preview
908 virtual UText
*group(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
912 * Returns the index in the input string of the start of the text matched
913 * during the previous match operation.
914 * @param status a reference to a UErrorCode to receive any errors.
915 * @return The (native) position in the input string of the start of the last match.
918 virtual int32_t start(UErrorCode
&status
) const;
921 * Returns the index in the input string of the start of the text matched
922 * during the previous match operation.
923 * @param status a reference to a UErrorCode to receive any errors.
924 * @return The (native) position in the input string of the start of the last match.
927 virtual int64_t start64(UErrorCode
&status
) const;
931 * Returns the index in the input string of the start of the text matched by the
932 * specified capture group during the previous match operation. Return -1 if
933 * the capture group exists in the pattern, but was not part of the last match.
935 * @param group the capture group number
936 * @param status A reference to a UErrorCode to receive any errors. Possible
937 * errors are U_REGEX_INVALID_STATE if no match has been
938 * attempted or the last match failed, and
939 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
940 * @return the (native) start position of substring matched by the specified group.
943 virtual int32_t start(int32_t group
, UErrorCode
&status
) const;
946 * Returns the index in the input string of the start of the text matched by the
947 * specified capture group during the previous match operation. Return -1 if
948 * the capture group exists in the pattern, but was not part of the last match.
950 * @param group the capture group number.
951 * @param status A reference to a UErrorCode to receive any errors. Possible
952 * errors are U_REGEX_INVALID_STATE if no match has been
953 * attempted or the last match failed, and
954 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number.
955 * @return the (native) start position of substring matched by the specified group.
958 virtual int64_t start64(int32_t group
, UErrorCode
&status
) const;
962 * Returns the index in the input string of the first character following the
963 * text matched during the previous match operation.
965 * @param status A reference to a UErrorCode to receive any errors. Possible
966 * errors are U_REGEX_INVALID_STATE if no match has been
967 * attempted or the last match failed.
968 * @return the index of the last character matched, plus one.
969 * The index value returned is a native index, corresponding to
970 * code units for the underlying encoding type, for example,
971 * a byte index for UTF-8.
974 virtual int32_t end(UErrorCode
&status
) const;
977 * Returns the index in the input string of the first character following the
978 * text matched during the previous match operation.
980 * @param status A reference to a UErrorCode to receive any errors. Possible
981 * errors are U_REGEX_INVALID_STATE if no match has been
982 * attempted or the last match failed.
983 * @return the index of the last character matched, plus one.
984 * The index value returned is a native index, corresponding to
985 * code units for the underlying encoding type, for example,
986 * a byte index for UTF-8.
989 virtual int64_t end64(UErrorCode
&status
) const;
993 * Returns the index in the input string of the character following the
994 * text matched by the specified capture group during the previous match operation.
996 * @param group the capture group number
997 * @param status A reference to a UErrorCode to receive any errors. Possible
998 * errors are U_REGEX_INVALID_STATE if no match has been
999 * attempted or the last match failed and
1000 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1001 * @return the index of the first character following the text
1002 * captured by the specified group during the previous match operation.
1003 * Return -1 if the capture group exists in the pattern but was not part of the match.
1004 * The index value returned is a native index, corresponding to
1005 * code units for the underlying encoding type, for example,
1006 * a byte index for UTF8.
1009 virtual int32_t end(int32_t group
, UErrorCode
&status
) const;
1012 * Returns the index in the input string of the character following the
1013 * text matched by the specified capture group during the previous match operation.
1015 * @param group the capture group number
1016 * @param status A reference to a UErrorCode to receive any errors. Possible
1017 * errors are U_REGEX_INVALID_STATE if no match has been
1018 * attempted or the last match failed and
1019 * U_INDEX_OUTOFBOUNDS_ERROR for a bad capture group number
1020 * @return the index of the first character following the text
1021 * captured by the specified group during the previous match operation.
1022 * Return -1 if the capture group exists in the pattern but was not part of the match.
1023 * The index value returned is a native index, corresponding to
1024 * code units for the underlying encoding type, for example,
1025 * a byte index for UTF8.
1028 virtual int64_t end64(int32_t group
, UErrorCode
&status
) const;
1032 * Resets this matcher. The effect is to remove any memory of previous matches,
1033 * and to cause subsequent find() operations to begin at the beginning of
1036 * @return this RegexMatcher.
1039 virtual RegexMatcher
&reset();
1043 * Resets this matcher, and set the current input position.
1044 * The effect is to remove any memory of previous matches,
1045 * and to cause subsequent find() operations to begin at
1046 * the specified (native) position in the input string.
1048 * The matcher's region is reset to its default, which is the entire
1051 * An alternative to this function is to set a match region
1052 * beginning at the desired index.
1054 * @return this RegexMatcher.
1057 virtual RegexMatcher
&reset(int64_t index
, UErrorCode
&status
);
1061 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1062 * to be reused, which is more efficient than creating a new RegexMatcher for
1063 * each input string to be processed.
1064 * @param input The new string on which subsequent pattern matches will operate.
1065 * The matcher retains a reference to the callers string, and operates
1066 * directly on that. Ownership of the string remains with the caller.
1067 * Because no copy of the string is made, it is essential that the
1068 * caller not delete the string until after regexp operations on it
1070 * Note that while a reset on the matcher with an input string that is then
1071 * modified across/during matcher operations may be supported currently for UnicodeString,
1072 * this was not originally intended behavior, and support for this is not guaranteed
1073 * in upcoming versions of ICU.
1074 * @return this RegexMatcher.
1077 virtual RegexMatcher
&reset(const UnicodeString
&input
);
1081 * Resets this matcher with a new input string. This allows instances of RegexMatcher
1082 * to be reused, which is more efficient than creating a new RegexMatcher for
1083 * each input string to be processed.
1084 * @param input The new string on which subsequent pattern matches will operate.
1085 * The matcher makes a shallow clone of the given text; ownership of the
1086 * original string remains with the caller. Because no deep copy of the
1087 * text is made, it is essential that the caller not modify the string
1088 * until after regexp operations on it are done.
1089 * @return this RegexMatcher.
1093 virtual RegexMatcher
&reset(UText
*input
);
1097 * Set the subject text string upon which the regular expression is looking for matches
1098 * without changing any other aspect of the matching state.
1099 * The new and previous text strings must have the same content.
1101 * This function is intended for use in environments where ICU is operating on
1102 * strings that may move around in memory. It provides a mechanism for notifying
1103 * ICU that the string has been relocated, and providing a new UText to access the
1104 * string in its new position.
1106 * Note that the regular expression implementation never copies the underlying text
1107 * of a string being matched, but always operates directly on the original text
1108 * provided by the user. Refreshing simply drops the references to the old text
1109 * and replaces them with references to the new.
1111 * Caution: this function is normally used only by very specialized,
1112 * system-level code. One example use case is with garbage collection that moves
1113 * the text in memory.
1115 * @param input The new (moved) text string.
1116 * @param status Receives errors detected by this function.
1120 virtual RegexMatcher
&refreshInputText(UText
*input
, UErrorCode
&status
);
1124 * Cause a compilation error if an application accidentally attempts to
1125 * reset a matcher with a (UChar *) string as input rather than
1126 * a UnicodeString. Avoids a dangling reference to a temporary string.
1128 * To efficiently work with UChar *strings, wrap the data in a UnicodeString
1129 * using one of the aliasing constructors, such as
1130 * <code>UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength);</code>
1131 * or in a UText, using
1132 * <code>utext_openUChars(UText *ut, const UChar *text, int64_t textLength, UErrorCode *status);</code>
1136 RegexMatcher
&reset(const UChar
*input
);
1140 * Returns the input string being matched. Ownership of the string belongs to
1141 * the matcher; it should not be altered or deleted. This method will work even if the input
1142 * was originally supplied as a UText.
1143 * @return the input string
1146 virtual const UnicodeString
&input() const;
1149 * Returns the input string being matched. This is the live input text; it should not be
1150 * altered or deleted. This method will work even if the input was originally supplied as
1152 * @return the input text
1156 virtual UText
*inputText() const;
1159 * Returns the input string being matched, either by copying it into the provided
1160 * UText parameter or by returning a shallow clone of the live input. Note that copying
1161 * the entire input may cause significant performance and memory issues.
1162 * @param dest The UText into which the input should be copied, or NULL to create a new UText
1163 * @param status error code
1164 * @return dest if non-NULL, a shallow copy of the input text otherwise
1168 virtual UText
*getInput(UText
*dest
, UErrorCode
&status
) const;
1171 /** Sets the limits of this matcher's region.
1172 * The region is the part of the input string that will be searched to find a match.
1173 * Invoking this method resets the matcher, and then sets the region to start
1174 * at the index specified by the start parameter and end at the index specified
1175 * by the end parameter.
1177 * Depending on the transparency and anchoring being used (see useTransparentBounds
1178 * and useAnchoringBounds), certain constructs such as anchors may behave differently
1179 * at or around the boundaries of the region
1181 * The function will fail if start is greater than limit, or if either index
1182 * is less than zero or greater than the length of the string being matched.
1184 * @param start The (native) index to begin searches at.
1185 * @param limit The index to end searches at (exclusive).
1186 * @param status A reference to a UErrorCode to receive any errors.
1189 virtual RegexMatcher
®ion(int64_t start
, int64_t limit
, UErrorCode
&status
);
1192 * Identical to region(start, limit, status) but also allows a start position without
1193 * resetting the region state.
1194 * @param regionStart The region start
1195 * @param regionLimit the limit of the region
1196 * @param startIndex The (native) index within the region bounds at which to begin searches.
1197 * @param status A reference to a UErrorCode to receive any errors.
1198 * If startIndex is not within the specified region bounds,
1199 * U_INDEX_OUTOFBOUNDS_ERROR is returned.
1202 virtual RegexMatcher
®ion(int64_t regionStart
, int64_t regionLimit
, int64_t startIndex
, UErrorCode
&status
);
1205 * Reports the start index of this matcher's region. The searches this matcher
1206 * conducts are limited to finding matches within regionStart (inclusive) and
1207 * regionEnd (exclusive).
1209 * @return The starting (native) index of this matcher's region.
1212 virtual int32_t regionStart() const;
1215 * Reports the start index of this matcher's region. The searches this matcher
1216 * conducts are limited to finding matches within regionStart (inclusive) and
1217 * regionEnd (exclusive).
1219 * @return The starting (native) index of this matcher's region.
1222 virtual int64_t regionStart64() const;
1226 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1227 * this matcher conducts are limited to finding matches within regionStart
1228 * (inclusive) and regionEnd (exclusive).
1230 * @return The ending point (native) of this matcher's region.
1233 virtual int32_t regionEnd() const;
1236 * Reports the end (limit) index (exclusive) of this matcher's region. The searches
1237 * this matcher conducts are limited to finding matches within regionStart
1238 * (inclusive) and regionEnd (exclusive).
1240 * @return The ending point (native) of this matcher's region.
1243 virtual int64_t regionEnd64() const;
1246 * Queries the transparency of region bounds for this matcher.
1247 * See useTransparentBounds for a description of transparent and opaque bounds.
1248 * By default, a matcher uses opaque region boundaries.
1250 * @return TRUE if this matcher is using opaque bounds, false if it is not.
1253 virtual UBool
hasTransparentBounds() const;
1256 * Sets the transparency of region bounds for this matcher.
1257 * Invoking this function with an argument of true will set this matcher to use transparent bounds.
1258 * If the boolean argument is false, then opaque bounds will be used.
1260 * Using transparent bounds, the boundaries of this matcher's region are transparent
1261 * to lookahead, lookbehind, and boundary matching constructs. Those constructs can
1262 * see text beyond the boundaries of the region while checking for a match.
1264 * With opaque bounds, no text outside of the matcher's region is visible to lookahead,
1265 * lookbehind, and boundary matching constructs.
1267 * By default, a matcher uses opaque bounds.
1269 * @param b TRUE for transparent bounds; FALSE for opaque bounds
1270 * @return This Matcher;
1273 virtual RegexMatcher
&useTransparentBounds(UBool b
);
1277 * Return true if this matcher is using anchoring bounds.
1278 * By default, matchers use anchoring region bounds.
1280 * @return TRUE if this matcher is using anchoring bounds.
1283 virtual UBool
hasAnchoringBounds() const;
1287 * Set whether this matcher is using Anchoring Bounds for its region.
1288 * With anchoring bounds, pattern anchors such as ^ and $ will match at the start
1289 * and end of the region. Without Anchoring Bounds, anchors will only match at
1290 * the positions they would in the complete text.
1292 * Anchoring Bounds are the default for regions.
1294 * @param b TRUE if to enable anchoring bounds; FALSE to disable them.
1295 * @return This Matcher
1298 virtual RegexMatcher
&useAnchoringBounds(UBool b
);
1302 * Return TRUE if the most recent matching operation attempted to access
1303 * additional input beyond the available input text.
1304 * In this case, additional input text could change the results of the match.
1306 * hitEnd() is defined for both successful and unsuccessful matches.
1307 * In either case hitEnd() will return TRUE if if the end of the text was
1308 * reached at any point during the matching process.
1310 * @return TRUE if the most recent match hit the end of input
1313 virtual UBool
hitEnd() const;
1316 * Return TRUE the most recent match succeeded and additional input could cause
1317 * it to fail. If this method returns false and a match was found, then more input
1318 * might change the match but the match won't be lost. If a match was not found,
1319 * then requireEnd has no meaning.
1321 * @return TRUE if more input could cause the most recent match to no longer match.
1324 virtual UBool
requireEnd() const;
1328 * Returns the pattern that is interpreted by this matcher.
1329 * @return the RegexPattern for this RegexMatcher
1332 virtual const RegexPattern
&pattern() const;
1336 * Replaces every substring of the input that matches the pattern
1337 * with the given replacement string. This is a convenience function that
1338 * provides a complete find-and-replace-all operation.
1340 * This method first resets this matcher. It then scans the input string
1341 * looking for matches of the pattern. Input that is not part of any
1342 * match is left unchanged; each match is replaced in the result by the
1343 * replacement string. The replacement string may contain references to
1346 * @param replacement a string containing the replacement text.
1347 * @param status a reference to a UErrorCode to receive any errors.
1348 * @return a string containing the results of the find and replace.
1351 virtual UnicodeString
replaceAll(const UnicodeString
&replacement
, UErrorCode
&status
);
1355 * Replaces every substring of the input that matches the pattern
1356 * with the given replacement string. This is a convenience function that
1357 * provides a complete find-and-replace-all operation.
1359 * This method first resets this matcher. It then scans the input string
1360 * looking for matches of the pattern. Input that is not part of any
1361 * match is left unchanged; each match is replaced in the result by the
1362 * replacement string. The replacement string may contain references to
1365 * @param replacement a string containing the replacement text.
1366 * @param dest a mutable UText in which the results are placed.
1367 * If NULL, a new UText will be created (which may not be mutable).
1368 * @param status a reference to a UErrorCode to receive any errors.
1369 * @return a string containing the results of the find and replace.
1370 * If a pre-allocated UText was provided, it will always be used and returned.
1374 virtual UText
*replaceAll(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1378 * Replaces the first substring of the input that matches
1379 * the pattern with the replacement string. This is a convenience
1380 * function that provides a complete find-and-replace operation.
1382 * <p>This function first resets this RegexMatcher. It then scans the input string
1383 * looking for a match of the pattern. Input that is not part
1384 * of the match is appended directly to the result string; the match is replaced
1385 * in the result by the replacement string. The replacement string may contain
1386 * references to captured groups.</p>
1388 * <p>The state of the matcher (the position at which a subsequent find()
1389 * would begin) after completing a replaceFirst() is not specified. The
1390 * RegexMatcher should be reset before doing additional find() operations.</p>
1392 * @param replacement a string containing the replacement text.
1393 * @param status a reference to a UErrorCode to receive any errors.
1394 * @return a string containing the results of the find and replace.
1397 virtual UnicodeString
replaceFirst(const UnicodeString
&replacement
, UErrorCode
&status
);
1401 * Replaces the first substring of the input that matches
1402 * the pattern with the replacement string. This is a convenience
1403 * function that provides a complete find-and-replace operation.
1405 * <p>This function first resets this RegexMatcher. It then scans the input string
1406 * looking for a match of the pattern. Input that is not part
1407 * of the match is appended directly to the result string; the match is replaced
1408 * in the result by the replacement string. The replacement string may contain
1409 * references to captured groups.</p>
1411 * <p>The state of the matcher (the position at which a subsequent find()
1412 * would begin) after completing a replaceFirst() is not specified. The
1413 * RegexMatcher should be reset before doing additional find() operations.</p>
1415 * @param replacement a string containing the replacement text.
1416 * @param dest a mutable UText in which the results are placed.
1417 * If NULL, a new UText will be created (which may not be mutable).
1418 * @param status a reference to a UErrorCode to receive any errors.
1419 * @return a string containing the results of the find and replace.
1420 * If a pre-allocated UText was provided, it will always be used and returned.
1424 virtual UText
*replaceFirst(UText
*replacement
, UText
*dest
, UErrorCode
&status
);
1428 * Implements a replace operation intended to be used as part of an
1429 * incremental find-and-replace.
1431 * <p>The input string, starting from the end of the previous replacement and ending at
1432 * the start of the current match, is appended to the destination string. Then the
1433 * replacement string is appended to the output string,
1434 * including handling any substitutions of captured text.</p>
1436 * <p>For simple, prepackaged, non-incremental find-and-replace
1437 * operations, see replaceFirst() or replaceAll().</p>
1439 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1440 * @param replacement A UnicodeString that provides the text to be substituted for
1441 * the input text that matched the regexp pattern. The replacement
1442 * text may contain references to captured text from the
1444 * @param status A reference to a UErrorCode to receive any errors. Possible
1445 * errors are U_REGEX_INVALID_STATE if no match has been
1446 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1447 * if the replacement text specifies a capture group that
1448 * does not exist in the pattern.
1450 * @return this RegexMatcher
1454 virtual RegexMatcher
&appendReplacement(UnicodeString
&dest
,
1455 const UnicodeString
&replacement
, UErrorCode
&status
);
1459 * Implements a replace operation intended to be used as part of an
1460 * incremental find-and-replace.
1462 * <p>The input string, starting from the end of the previous replacement and ending at
1463 * the start of the current match, is appended to the destination string. Then the
1464 * replacement string is appended to the output string,
1465 * including handling any substitutions of captured text.</p>
1467 * <p>For simple, prepackaged, non-incremental find-and-replace
1468 * operations, see replaceFirst() or replaceAll().</p>
1470 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1472 * @param replacement A UText that provides the text to be substituted for
1473 * the input text that matched the regexp pattern. The replacement
1474 * text may contain references to captured text from the input.
1475 * @param status A reference to a UErrorCode to receive any errors. Possible
1476 * errors are U_REGEX_INVALID_STATE if no match has been
1477 * attempted or the last match failed, and U_INDEX_OUTOFBOUNDS_ERROR
1478 * if the replacement text specifies a capture group that
1479 * does not exist in the pattern.
1481 * @return this RegexMatcher
1485 virtual RegexMatcher
&appendReplacement(UText
*dest
,
1486 UText
*replacement
, UErrorCode
&status
);
1490 * As the final step in a find-and-replace operation, append the remainder
1491 * of the input string, starting at the position following the last appendReplacement(),
1492 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1493 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1495 * @param dest A UnicodeString to which the results of the find-and-replace are appended.
1496 * @return the destination string.
1499 virtual UnicodeString
&appendTail(UnicodeString
&dest
);
1503 * As the final step in a find-and-replace operation, append the remainder
1504 * of the input string, starting at the position following the last appendReplacement(),
1505 * to the destination string. <code>appendTail()</code> is intended to be invoked after one
1506 * or more invocations of the <code>RegexMatcher::appendReplacement()</code>.
1508 * @param dest A mutable UText to which the results of the find-and-replace are appended.
1510 * @param status error cod
1511 * @return the destination string.
1515 virtual UText
*appendTail(UText
*dest
, UErrorCode
&status
);
1519 * Split a string into fields. Somewhat like split() from Perl.
1520 * The pattern matches identify delimiters that separate the input
1521 * into fields. The input data between the matches becomes the
1522 * fields themselves.
1524 * @param input The string to be split into fields. The field delimiters
1525 * match the pattern (in the "this" object). This matcher
1526 * will be reset to this input string.
1527 * @param dest An array of UnicodeStrings to receive the results of the split.
1528 * This is an array of actual UnicodeString objects, not an
1529 * array of pointers to strings. Local (stack based) arrays can
1531 * @param destCapacity The number of elements in the destination array.
1532 * If the number of fields found is less than destCapacity, the
1533 * extra strings in the destination array are not altered.
1534 * If the number of destination strings is less than the number
1535 * of fields, the trailing part of the input string, including any
1536 * field delimiters, is placed in the last destination string.
1537 * @param status A reference to a UErrorCode to receive any errors.
1538 * @return The number of fields into which the input string was split.
1541 virtual int32_t split(const UnicodeString
&input
,
1542 UnicodeString dest
[],
1543 int32_t destCapacity
,
1544 UErrorCode
&status
);
1548 * Split a string into fields. Somewhat like split() from Perl.
1549 * The pattern matches identify delimiters that separate the input
1550 * into fields. The input data between the matches becomes the
1551 * fields themselves.
1553 * @param input The string to be split into fields. The field delimiters
1554 * match the pattern (in the "this" object). This matcher
1555 * will be reset to this input string.
1556 * @param dest An array of mutable UText structs to receive the results of the split.
1557 * If a field is NULL, a new UText is allocated to contain the results for
1558 * that field. This new UText is not guaranteed to be mutable.
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.
1570 virtual int32_t split(UText
*input
,
1572 int32_t destCapacity
,
1573 UErrorCode
&status
);
1576 * Set a processing time limit for match operations with this Matcher.
1578 * Some patterns, when matching certain strings, can run in exponential time.
1579 * For practical purposes, the match operation may appear to be in an
1581 * When a limit is set a match operation will fail with an error if the
1582 * limit is exceeded.
1584 * The units of the limit are steps of the match engine.
1585 * Correspondence with actual processor time will depend on the speed
1586 * of the processor and the details of the specific pattern, but will
1587 * typically be on the order of milliseconds.
1589 * By default, the matching time is not limited.
1592 * @param limit The limit value, or 0 for no limit.
1593 * @param status A reference to a UErrorCode to receive any errors.
1596 virtual void setTimeLimit(int32_t limit
, UErrorCode
&status
);
1599 * Get the time limit, if any, for match operations made with this Matcher.
1601 * @return the maximum allowed time for a match, in units of processing steps.
1604 virtual int32_t getTimeLimit() const;
1607 * Set the amount of heap storage available for use by the match backtracking stack.
1608 * The matcher is also reset, discarding any results from previous matches.
1610 * ICU uses a backtracking regular expression engine, with the backtrack stack
1611 * maintained on the heap. This function sets the limit to the amount of memory
1612 * that can be used for this purpose. A backtracking stack overflow will
1613 * result in an error from the match operation that caused it.
1615 * A limit is desirable because a malicious or poorly designed pattern can use
1616 * excessive memory, potentially crashing the process. A limit is enabled
1619 * @param limit The maximum size, in bytes, of the matching backtrack stack.
1620 * A value of zero means no limit.
1621 * The limit must be greater or equal to zero.
1623 * @param status A reference to a UErrorCode to receive any errors.
1627 virtual void setStackLimit(int32_t limit
, UErrorCode
&status
);
1630 * Get the size of the heap storage available for use by the back tracking stack.
1632 * @return the maximum backtracking stack size, in bytes, or zero if the
1633 * stack size is unlimited.
1636 virtual int32_t getStackLimit() const;
1640 * Set a callback function for use with this Matcher.
1641 * During matching operations the function will be called periodically,
1642 * giving the application the opportunity to terminate a long-running
1645 * @param callback A pointer to the user-supplied callback function.
1646 * @param context User context pointer. The value supplied at the
1647 * time the callback function is set will be saved
1648 * and passed to the callback each time that it is called.
1649 * @param status A reference to a UErrorCode to receive any errors.
1652 virtual void setMatchCallback(URegexMatchCallback
*callback
,
1653 const void *context
,
1654 UErrorCode
&status
);
1658 * Get the callback function for this URegularExpression.
1660 * @param callback Out parameter, receives a pointer to the user-supplied
1661 * callback function.
1662 * @param context Out parameter, receives the user context pointer that
1663 * was set when uregex_setMatchCallback() was called.
1664 * @param status A reference to a UErrorCode to receive any errors.
1667 virtual void getMatchCallback(URegexMatchCallback
*&callback
,
1668 const void *&context
,
1669 UErrorCode
&status
);
1673 * Set a progress callback function for use with find operations on this Matcher.
1674 * During find operations, the callback will be invoked after each return from a
1675 * match attempt, giving the application the opportunity to terminate a long-running
1678 * @param callback A pointer to the user-supplied callback function.
1679 * @param context User context pointer. The value supplied at the
1680 * time the callback function is set will be saved
1681 * and passed to the callback each time that it is called.
1682 * @param status A reference to a UErrorCode to receive any errors.
1685 virtual void setFindProgressCallback(URegexFindProgressCallback
*callback
,
1686 const void *context
,
1687 UErrorCode
&status
);
1691 * Get the find progress callback function for this URegularExpression.
1693 * @param callback Out parameter, receives a pointer to the user-supplied
1694 * callback function.
1695 * @param context Out parameter, receives the user context pointer that
1696 * was set when uregex_setFindProgressCallback() was called.
1697 * @param status A reference to a UErrorCode to receive any errors.
1700 virtual void getFindProgressCallback(URegexFindProgressCallback
*&callback
,
1701 const void *&context
,
1702 UErrorCode
&status
);
1704 #ifndef U_HIDE_INTERNAL_API
1706 * setTrace Debug function, enable/disable tracing of the matching engine.
1707 * For internal ICU development use only. DO NO USE!!!!
1710 void setTrace(UBool state
);
1711 #endif /* U_HIDE_INTERNAL_API */
1714 * ICU "poor man's RTTI", returns a UClassID for this class.
1718 static UClassID U_EXPORT2
getStaticClassID();
1721 * ICU "poor man's RTTI", returns a UClassID for the actual class.
1725 virtual UClassID
getDynamicClassID() const;
1728 // Constructors and other object boilerplate are private.
1729 // Instances of RegexMatcher can not be assigned, copied, cloned, etc.
1730 RegexMatcher(); // default constructor not implemented
1731 RegexMatcher(const RegexPattern
*pat
);
1732 RegexMatcher(const RegexMatcher
&other
);
1733 RegexMatcher
&operator =(const RegexMatcher
&rhs
);
1734 void init(UErrorCode
&status
); // Common initialization
1735 void init2(UText
*t
, UErrorCode
&e
); // Common initialization, part 2.
1737 friend class RegexPattern
;
1738 friend class RegexCImpl
;
1740 #ifndef U_HIDE_INTERNAL_API
1742 void resetPreserveRegion(); // Reset matcher state, but preserve any region.
1743 #endif /* U_HIDE_INTERNAL_API */
1747 // MatchAt This is the internal interface to the match engine itself.
1748 // Match status comes back in matcher member variables.
1750 void MatchAt(int64_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1751 inline void backTrack(int64_t &inputIdx
, int32_t &patIdx
);
1752 UBool
isWordBoundary(int64_t pos
); // perform Perl-like \b test
1753 UBool
isUWordBoundary(int64_t pos
); // perform RBBI based \b test
1754 REStackFrame
*resetStack();
1755 inline REStackFrame
*StateSave(REStackFrame
*fp
, int64_t savePatIdx
, UErrorCode
&status
);
1756 void IncrementTime(UErrorCode
&status
);
1757 UBool
ReportFindProgress(int64_t matchIndex
, UErrorCode
&status
);
1759 int64_t appendGroup(int32_t groupNum
, UText
*dest
, UErrorCode
&status
) const;
1761 UBool
findUsingChunk();
1762 void MatchChunkAt(int32_t startIdx
, UBool toEnd
, UErrorCode
&status
);
1763 UBool
isChunkWordBoundary(int32_t pos
);
1765 const RegexPattern
*fPattern
;
1766 RegexPattern
*fPatternOwned
; // Non-NULL if this matcher owns the pattern, and
1767 // should delete it when through.
1769 const UnicodeString
*fInput
; // The string being matched. Only used for input()
1770 UText
*fInputText
; // The text being matched. Is never NULL.
1771 UText
*fAltInputText
; // A shallow copy of the text being matched.
1772 // Only created if the pattern contains backreferences.
1773 int64_t fInputLength
; // Full length of the input text.
1774 int32_t fFrameSize
; // The size of a frame in the backtrack stack.
1776 int64_t fRegionStart
; // Start of the input region, default = 0.
1777 int64_t fRegionLimit
; // End of input region, default to input.length.
1779 int64_t fAnchorStart
; // Region bounds for anchoring operations (^ or $).
1780 int64_t fAnchorLimit
; // See useAnchoringBounds
1782 int64_t fLookStart
; // Region bounds for look-ahead/behind and
1783 int64_t fLookLimit
; // and other boundary tests. See
1784 // useTransparentBounds
1786 int64_t fActiveStart
; // Currently active bounds for matching.
1787 int64_t fActiveLimit
; // Usually is the same as region, but
1788 // is changed to fLookStart/Limit when
1789 // entering look around regions.
1791 UBool fTransparentBounds
; // True if using transparent bounds.
1792 UBool fAnchoringBounds
; // True if using anchoring bounds.
1794 UBool fMatch
; // True if the last attempted match was successful.
1795 int64_t fMatchStart
; // Position of the start of the most recent match
1796 int64_t fMatchEnd
; // First position after the end of the most recent match
1797 // Zero if no previous match, even when a region
1799 int64_t fLastMatchEnd
; // First position after the end of the previous match,
1800 // or -1 if there was no previous match.
1801 int64_t fAppendPosition
; // First position after the end of the previous
1802 // appendReplacement(). As described by the
1803 // JavaDoc for Java Matcher, where it is called
1804 // "append position"
1805 UBool fHitEnd
; // True if the last match touched the end of input.
1806 UBool fRequireEnd
; // True if the last match required end-of-input
1810 REStackFrame
*fFrame
; // After finding a match, the last active stack frame,
1811 // which will contain the capture group results.
1812 // NOT valid while match engine is running.
1814 int64_t *fData
; // Data area for use by the compiled pattern.
1815 int64_t fSmallData
[8]; // Use this for data if it's enough.
1817 int32_t fTimeLimit
; // Max time (in arbitrary steps) to let the
1818 // match engine run. Zero for unlimited.
1820 int32_t fTime
; // Match time, accumulates while matching.
1821 int32_t fTickCounter
; // Low bits counter for time. Counts down StateSaves.
1822 // Kept separately from fTime to keep as much
1823 // code as possible out of the inline
1824 // StateSave function.
1826 int32_t fStackLimit
; // Maximum memory size to use for the backtrack
1827 // stack, in bytes. Zero for unlimited.
1829 URegexMatchCallback
*fCallbackFn
; // Pointer to match progress callback funct.
1830 // NULL if there is no callback.
1831 const void *fCallbackContext
; // User Context ptr for callback function.
1833 URegexFindProgressCallback
*fFindProgressCallbackFn
; // Pointer to match progress callback funct.
1834 // NULL if there is no callback.
1835 const void *fFindProgressCallbackContext
; // User Context ptr for callback function.
1838 UBool fInputUniStrMaybeMutable
; // Set when fInputText wraps a UnicodeString that may be mutable - compatibility.
1840 UBool fTraceDebug
; // Set true for debug tracing of match engine.
1842 UErrorCode fDeferredStatus
; // Save error state that cannot be immediately
1843 // reported, or that permanently disables this matcher.
1845 RuleBasedBreakIterator
*fWordBreakItr
;
1849 #endif // UCONFIG_NO_REGULAR_EXPRESSIONS