author | llornkcor <llornkcor> | 2003-07-10 02:40:10 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2003-07-10 02:40:10 (UTC) |
commit | 155d68c1e7d7dc0fed2534ac43d6d77ce2781f55 (patch) (side-by-side diff) | |
tree | e6edaa5a7040fe6c224c3943d1094dcf02e4f74c /qmake/tools/qregexp.cpp | |
parent | 86703e8a5527ef114facd02c005b6b3a7e62e263 (diff) | |
download | opie-155d68c1e7d7dc0fed2534ac43d6d77ce2781f55.zip opie-155d68c1e7d7dc0fed2534ac43d6d77ce2781f55.tar.gz opie-155d68c1e7d7dc0fed2534ac43d6d77ce2781f55.tar.bz2 |
update qmake to 1.05a
-rw-r--r-- | qmake/tools/qregexp.cpp | 70 |
1 files changed, 39 insertions, 31 deletions
diff --git a/qmake/tools/qregexp.cpp b/qmake/tools/qregexp.cpp index 500efed..0c1f060 100644 --- a/qmake/tools/qregexp.cpp +++ b/qmake/tools/qregexp.cpp @@ -218,105 +218,105 @@ with '(?!' and finishes at the ')'. It means that the text it contains, 'amp;' in our example, must \e not follow the expression that preceeds it. Regexps provide a rich language that can be used in a variety of ways. For example suppose we want to count all the occurrences of 'Eric' and 'Eirik' in a string. Two valid regexps to match these are <b>\\b(Eric|Eirik)\\b</b> and <b>\\bEi?ri[ck]\\b</b>. We need the word boundary '\b' so we don't get 'Ericsson' etc. The second regexp actually matches more than we want, 'Eric', 'Erik', 'Eiric' and 'Eirik'. We will implement some the examples above in the \link #code-examples code examples \endlink section. \target characters-and-abbreviations-for-sets-of-characters \section1 Characters and Abbreviations for Sets of Characters \table \header \i Element \i Meaning \row \i <b>c</b> \i Any character represents itself unless it has a special regexp meaning. Thus <b>c</b> matches the character \e c. \row \i <b>\\c</b> \i A character that follows a backslash matches the character itself except where mentioned below. For example if you wished to match a literal caret at the beginning of a string you would write <b>\^</b>. \row \i <b>\\a</b> \i This matches the ASCII bell character (BEL, 0x07). \row \i <b>\\f</b> \i This matches the ASCII form feed character (FF, 0x0C). \row \i <b>\\n</b> \i This matches the ASCII line feed character (LF, 0x0A, Unix newline). \row \i <b>\\r</b> \i This matches the ASCII carriage return character (CR, 0x0D). \row \i <b>\\t</b> \i This matches the ASCII horizontal tab character (HT, 0x09). \row \i <b>\\v</b> \i This matches the ASCII vertical tab character (VT, 0x0B). \row \i <b>\\xhhhh</b> \i This matches the Unicode character corresponding to the hexadecimal number hhhh (between 0x0000 and 0xFFFF). \0ooo (i.e., \zero ooo) matches the ASCII/Latin-1 character corresponding to the octal number ooo (between 0 and 0377). \row \i <b>. (dot)</b> \i This matches any character (including newline). \row \i <b>\\d</b> - \i This matches a digit (see QChar::isDigit()). + \i This matches a digit (QChar::isDigit()). \row \i <b>\\D</b> \i This matches a non-digit. \row \i <b>\\s</b> - \i This matches a whitespace (see QChar::isSpace()). + \i This matches a whitespace (QChar::isSpace()). \row \i <b>\\S</b> \i This matches a non-whitespace. \row \i <b>\\w</b> - \i This matches a word character (see QChar::isLetterOrNumber()). + \i This matches a word character (QChar::isLetterOrNumber() or '_'). \row \i <b>\\W</b> \i This matches a non-word character. \row \i <b>\\n</b> \i The n-th \link #capturing-text backreference \endlink, e.g. \1, \2, etc. \endtable \e {Note that the C++ compiler transforms backslashes in strings so to include a <b>\\</b> in a regexp you will need to enter it twice, i.e. <b>\\\\</b>.} \target sets-of-characters \section1 Sets of Characters Square brackets are used to match any character in the set of characters contained within the square brackets. All the character set abbreviations described above can be used within square brackets. Apart from the character set abbreviations and the following two exceptions no characters have special meanings in square brackets. \table \row \i <b>^</b> \i The caret negates the character set if it occurs as the first character, i.e. immediately after the opening square bracket. For example, <b>[abc]</b> matches 'a' or 'b' or 'c', but <b>[^abc]</b> matches anything \e except 'a' or 'b' or 'c'. \row \i <b>-</b> \i The dash is used to indicate a range of characters, for example <b>[W-Z]</b> matches 'W' or 'X' or 'Y' or 'Z'. \endtable Using the predefined character set abbreviations is more portable than using character ranges across platforms and languages. For example, <b>[0-9]</b> matches a digit in Western alphabets but <b>\d</b> matches a digit in \e any alphabet. Note that in most regexp literature sets of characters are called "character classes". \target quantifiers \section1 Quantifiers By default an expression is automatically quantified by <b>{1,1}</b>, i.e. it should occur exactly once. In the following list <b>\e {E}</b> stands for any expression. An expression is a character or an abbreviation for a set of characters or a set of @@ -502,97 +502,104 @@ \target perl-users \section1 Notes for Perl Users Most of the character class abbreviations supported by Perl are supported by QRegExp, see \link #characters-and-abbreviations-for-sets-of-characters characters and abbreviations for sets of characters \endlink. In QRegExp, apart from within character classes, \c{^} always signifies the start of the string, so carets must always be escaped unless used for that purpose. In Perl the meaning of caret varies automagically depending on where it occurs so escaping it is rarely necessary. The same applies to \c{$} which in QRegExp always signifies the end of the string. QRegExp's quantifiers are the same as Perl's greedy quantifiers. Non-greedy matching cannot be applied to individual quantifiers, but can be applied to all the quantifiers in the pattern. For example, to match the Perl regexp <b>ro+?m</b> requires: \code QRegExp rx( "ro+m" ); rx.setMinimal( TRUE ); \endcode The equivalent of Perl's \c{/i} option is setCaseSensitive(FALSE). Perl's \c{/g} option can be emulated using a \link #cap_in_a_loop loop \endlink. In QRegExp <b>.</b> matches any character, therefore all QRegExp regexps have the equivalent of Perl's \c{/s} option. QRegExp does not have an equivalent to Perl's \c{/m} option, but this can be emulated in various ways for example by splitting the input into lines or by looping with a regexp that searches for newlines. Because QRegExp is string oriented there are no \A, \Z or \z assertions. The \G assertion is not supported but can be emulated in a loop. Perl's $& is cap(0) or capturedTexts()[0]. There are no QRegExp equivalents for $`, $' or $+. Perl's capturing variables, $1, $2, ... correspond to cap(1) or capturedTexts()[1], cap(2) or capturedTexts()[2], etc. To substitute a pattern use QString::replace(). Perl's extended \c{/x} syntax is not supported, nor are - regexp comments (?#comment) or directives, e.g. (?i). + directives, e.g. (?i), or regexp comments, e.g. (?#comment). On + the other hand, C++'s rules for literal strings can be used to + achieve the same: + \code + QRegExp mark( "\\b" // word boundary + "[Mm]ark" // the word we want to match + ); + \endcode Both zero-width positive and zero-width negative lookahead assertions (?=pattern) and (?!pattern) are supported with the same syntax as Perl. Perl's lookbehind assertions, "independent" subexpressions and conditional expressions are not supported. Non-capturing parentheses are also supported, with the same (?:pattern) syntax. See QStringList::split() and QStringList::join() for equivalents to Perl's split and join functions. Note: because C++ transforms \\'s they must be written \e twice in code, e.g. <b>\\b</b> must be written <b>\\\\b</b>. \target code-examples \section1 Code Examples \code QRegExp rx( "^\\d\\d?$" ); // match integers 0 to 99 rx.search( "123" ); // returns -1 (no match) rx.search( "-6" ); // returns -1 (no match) rx.search( "6" ); // returns 0 (matched as position 0) \endcode The third string matches '<u>6</u>'. This is a simple validation regexp for integers in the range 0 to 99. \code QRegExp rx( "^\\S+$" ); // match strings without whitespace rx.search( "Hello world" ); // returns -1 (no match) rx.search( "This_is-OK" ); // returns 0 (matched at position 0) \endcode The second string matches '<u>This_is-OK</u>'. We've used the character set abbreviation '\S' (non-whitespace) and the anchors to match strings which contain no whitespace. In the following example we match strings containing 'mail' or 'letter' or 'correspondence' but only match whole words i.e. not 'email' \code QRegExp rx( "\\b(mail|letter|correspondence)\\b" ); rx.search( "I sent you an email" ); // returns -1 (no match) rx.search( "Please write the letter" ); // returns 17 \endcode @@ -632,134 +639,139 @@ pos = rx.search( str, pos ); if ( pos >= 0 ) { pos++; // move along in str count++; // count our Eric or Eirik } } \endcode We've used the search() function to repeatedly match the regexp in the string. Note that instead of moving forward by one character at a time \c pos++ we could have written \c {pos += rx.matchedLength()} to skip over the already matched string. The count will equal 3, matching 'One <u>Eric</u> another <u>Eirik</u>, and an Ericsson. How many Eiriks, <u>Eric</u>?'; it doesn't match 'Ericsson' or 'Eiriks' because they are not bounded by non-word boundaries. One common use of regexps is to split lines of delimited data into their component fields. \code str = "Trolltech AS\twww.trolltech.com\tNorway"; QString company, web, country; rx.setPattern( "^([^\t]+)\t([^\t]+)\t([^\t]+)$" ); if ( rx.search( str ) != -1 ) { company = rx.cap( 1 ); web = rx.cap( 2 ); country = rx.cap( 3 ); } \endcode In this example our input lines have the format company name, web address and country. Unfortunately the regexp is rather long and not very versatile -- the code will break if we add any more fields. A simpler and better solution is to look for the separator, '\t' in this case, and take the surrounding text. The QStringList split() function can take a separator string or regexp as an argument and split a string accordingly. \code QStringList field = QStringList::split( "\t", str ); \endcode Here field[0] is the company, field[1] the web address and so on. To imitate the matching of a shell we can use wildcard mode. \code - QRegExp rx( "*.html" ); // invalid regexp: * doesn't quantify anything - rx.setWildcard( TRUE ); // now it's a valid wildcard regexp - rx.search( "index.html" ); // returns 0 (matched at position 0) - rx.search( "default.htm" ); // returns -1 (no match) - rx.search( "readme.txt" ); // returns -1 (no match) + QRegExp rx( "*.html" ); // invalid regexp: * doesn't quantify anything + rx.setWildcard( TRUE ); // now it's a valid wildcard regexp + rx.exactMatch( "index.html" ); // returns TRUE + rx.exactMatch( "default.htm" ); // returns FALSE + rx.exactMatch( "readme.txt" ); // returns FALSE \endcode Wildcard matching can be convenient because of its simplicity, but any wildcard regexp can be defined using full regexps, e.g. <b>.*\.html$</b>. Notice that we can't match both \c .html and \c .htm files with a wildcard unless we use <b>*.htm*</b> which will also match 'test.html.bak'. A full regexp gives us the precision we need, <b>.*\\.html?$</b>. QRegExp can match case insensitively using setCaseSensitive(), and can use non-greedy matching, see setMinimal(). By default QRegExp uses full regexps but this can be changed with setWildcard(). Searching can be forward with search() or backward with searchRev(). Captured text can be accessed using capturedTexts() which returns a string list of all captured strings, or using cap() which returns the captured string for the given index. The pos() function takes a match index and returns the position in the string where the match was made (or -1 if there was no match). \sa QRegExpValidator QString QStringList \target member-function-documentation */ const int NumBadChars = 64; #define BadChar( ch ) ( (ch).unicode() % NumBadChars ) const int NoOccurrence = INT_MAX; const int EmptyCapture = INT_MAX; const int InftyLen = INT_MAX; const int InftyRep = 1025; const int EOS = -1; +static bool isWord( QChar ch ) +{ + return ch.isLetterOrNumber() || ch == QChar( '_' ); +} + /* Merges two QMemArrays of ints and puts the result into the first one. */ static void mergeInto( QMemArray<int> *a, const QMemArray<int>& b ) { int asize = a->size(); int bsize = b.size(); if ( asize == 0 ) { *a = b.copy(); #ifndef QT_NO_REGEXP_OPTIM } else if ( bsize == 1 && (*a)[asize - 1] < b[0] ) { a->resize( asize + 1 ); (*a)[asize] = b[0]; #endif } else if ( bsize >= 1 ) { int csize = asize + bsize; QMemArray<int> c( csize ); int i = 0, j = 0, k = 0; while ( i < asize ) { if ( j < bsize ) { if ( (*a)[i] == b[j] ) { i++; csize--; } else if ( (*a)[i] < b[j] ) { c[k++] = (*a)[i++]; } else { c[k++] = b[j++]; } } else { memcpy( c.data() + k, (*a).data() + i, (asize - i) * sizeof(int) ); break; } } c.resize( csize ); if ( j < bsize ) memcpy( c.data() + k, b.data() + j, (bsize - j) * sizeof(int) ); *a = c; } } /* Merges two disjoint QMaps of (int, int) pairs and puts the result into the first one. */ static void mergeInto( QMap<int, int> *a, const QMap<int, int>& b ) { QMap<int, int>::ConstIterator it; @@ -1635,99 +1647,99 @@ int QRegExpEngine::addLookahead( QRegExpEngine *eng, bool negative ) #ifndef QT_NO_REGEXP_CAPTURE /* We want the longest leftmost captures. */ bool QRegExpEngine::isBetterCapture( const int *begin1, const int *end1, const int *begin2, const int *end2 ) { for ( int i = 0; i < ncap; i++ ) { int delta = begin2[i] - begin1[i]; // it has to start early... if ( delta == 0 ) delta = end1[i] - end2[i]; // ...and end late (like a party) if ( delta != 0 ) return delta > 0; } return FALSE; } #endif /* Returns TRUE if anchor a matches at position mmPos + i in the input string, otherwise FALSE. */ bool QRegExpEngine::testAnchor( int i, int a, const int *capBegin ) { int j; #ifndef QT_NO_REGEXP_ANCHOR_ALT if ( (a & Anchor_Alternation) != 0 ) { return testAnchor( i, aa[a ^ Anchor_Alternation].a, capBegin ) || testAnchor( i, aa[a ^ Anchor_Alternation].b, capBegin ); } #endif if ( (a & Anchor_Caret) != 0 ) { if ( mmPos + i != mmCaretPos ) return FALSE; } if ( (a & Anchor_Dollar) != 0 ) { if ( mmPos + i != mmLen ) return FALSE; } #ifndef QT_NO_REGEXP_ESCAPE if ( (a & (Anchor_Word | Anchor_NonWord)) != 0 ) { bool before = FALSE; bool after = FALSE; if ( mmPos + i != 0 ) - before = mmIn[mmPos + i - 1].isLetterOrNumber(); + before = isWord( mmIn[mmPos + i - 1] ); if ( mmPos + i != mmLen ) - after = mmIn[mmPos + i].isLetterOrNumber(); + after = isWord( mmIn[mmPos + i] ); if ( (a & Anchor_Word) != 0 && (before == after) ) return FALSE; if ( (a & Anchor_NonWord) != 0 && (before != after) ) return FALSE; } #endif #ifndef QT_NO_REGEXP_LOOKAHEAD bool catchx = TRUE; if ( (a & Anchor_LookaheadMask) != 0 ) { QConstString cstr = QConstString( (QChar *) mmIn + mmPos + i, mmLen - mmPos - i ); for ( j = 0; j < (int) ahead.size(); j++ ) { if ( (a & (Anchor_FirstLookahead << j)) != 0 ) { catchx = ahead[j]->eng->match( cstr.string(), 0, TRUE, TRUE, mmCaretPos - mmPos - i )[0] == 0; if ( catchx == ahead[j]->neg ) return FALSE; } } } #endif #ifndef QT_NO_REGEXP_CAPTURE #ifndef QT_NO_REGEXP_BACKREF for ( j = 0; j < nbrefs; j++ ) { if ( (a & (Anchor_BackRef1Empty << j)) != 0 ) { if ( capBegin[j] != EmptyCapture ) return FALSE; } } #endif #endif return TRUE; } #ifndef QT_NO_REGEXP_OPTIM /* The three following functions are what Jeffrey Friedl would call transmissions (or bump-alongs). Using one or the other should make no difference except in performance. */ bool QRegExpEngine::goodStringMatch() { int k = mmPos + goodEarlyStart; while ( (k = mmStr->find(goodStr, k, cs)) != -1 ) { int from = k - goodLateStart; @@ -2587,116 +2599,124 @@ int QRegExpEngine::getEscape() int prevCh = yyCh; if ( prevCh == EOS ) { error( RXERR_END ); return Tok_Char | '\\'; } yyCh = getChar(); #ifndef QT_NO_REGEXP_ESCAPE if ( (prevCh & ~0xff) == 0 ) { const char *p = strchr( tab, prevCh ); if ( p != 0 ) return Tok_Char | backTab[p - tab]; } #endif switch ( prevCh ) { #ifndef QT_NO_REGEXP_ESCAPE case '0': val = 0; for ( i = 0; i < 3; i++ ) { if ( yyCh >= '0' && yyCh <= '7' ) val = ( val << 3 ) | ( yyCh - '0' ); else break; yyCh = getChar(); } if ( (val & ~0377) != 0 ) error( RXERR_OCTAL ); return Tok_Char | val; #endif #ifndef QT_NO_REGEXP_ESCAPE case 'B': return Tok_NonWord; #endif #ifndef QT_NO_REGEXP_CCLASS case 'D': // see QChar::isDigit() yyCharClass->addCategories( 0x7fffffef ); return Tok_CharClass; case 'S': // see QChar::isSpace() yyCharClass->addCategories( 0x7ffff87f ); yyCharClass->addRange( 0x0000, 0x0008 ); yyCharClass->addRange( 0x000e, 0x001f ); yyCharClass->addRange( 0x007f, 0x009f ); return Tok_CharClass; case 'W': // see QChar::isLetterOrNumber() - yyCharClass->addCategories( 0x7ff07f8f ); + yyCharClass->addCategories( 0x7fe07f8f ); + yyCharClass->addRange( 0x203f, 0x2040 ); + yyCharClass->addSingleton( 0x2040 ); + yyCharClass->addSingleton( 0x30fb ); + yyCharClass->addRange( 0xfe33, 0xfe34 ); + yyCharClass->addRange( 0xfe4d, 0xfe4f ); + yyCharClass->addSingleton( 0xff3f ); + yyCharClass->addSingleton( 0xff65 ); return Tok_CharClass; #endif #ifndef QT_NO_REGEXP_ESCAPE case 'b': return Tok_Word; #endif #ifndef QT_NO_REGEXP_CCLASS case 'd': // see QChar::isDigit() yyCharClass->addCategories( 0x00000010 ); return Tok_CharClass; case 's': // see QChar::isSpace() yyCharClass->addCategories( 0x00000380 ); yyCharClass->addRange( 0x0009, 0x000d ); return Tok_CharClass; case 'w': // see QChar::isLetterOrNumber() yyCharClass->addCategories( 0x000f8070 ); + yyCharClass->addSingleton( 0x005f ); // '_' return Tok_CharClass; #endif #ifndef QT_NO_REGEXP_ESCAPE case 'x': val = 0; for ( i = 0; i < 4; i++ ) { low = QChar( yyCh ).lower(); if ( low >= '0' && low <= '9' ) val = ( val << 4 ) | ( low - '0' ); else if ( low >= 'a' && low <= 'f' ) val = ( val << 4 ) | ( low - 'a' + 10 ); else break; yyCh = getChar(); } return Tok_Char | val; #endif default: if ( prevCh >= '1' && prevCh <= '9' ) { #ifndef QT_NO_REGEXP_BACKREF val = prevCh - '0'; while ( yyCh >= '0' && yyCh <= '9' ) { val = ( val *= 10 ) | ( yyCh - '0' ); yyCh = getChar(); } return Tok_BackRef | val; #else error( RXERR_DISABLED ); #endif } return Tok_Char | prevCh; } } #ifndef QT_NO_REGEXP_INTERVAL int QRegExpEngine::getRep( int def ) { if ( yyCh >= '0' && yyCh <= '9' ) { int rep = 0; do { rep = 10 * rep + yyCh - '0'; if ( rep >= InftyRep ) { error( RXERR_REPETITION ); rep = def; } yyCh = getChar(); } while ( yyCh >= '0' && yyCh <= '9' ); return rep; @@ -3138,117 +3158,119 @@ void QRegExpEngine::parseTerm( Box *box ) box->cat( rightBox ); } } void QRegExpEngine::parseExpression( Box *box ) { parseTerm( box ); while ( yyTok == Tok_Bar ) { Box rightBox( this ); yyTok = getToken(); parseTerm( &rightBox ); box->orx( rightBox ); } } /* The struct QRegExpPrivate contains the private data of a regular expression other than the automaton. It makes it possible for many QRegExp objects to use the same QRegExpEngine object with different QRegExpPrivate objects. */ struct QRegExpPrivate { QString pattern; // regular-expression or wildcard pattern QString rxpattern; // regular-expression pattern #ifndef QT_NO_REGEXP_WILDCARD bool wc; // wildcard mode? #endif bool min; // minimal matching? (instead of maximal) #ifndef QT_NO_REGEXP_CAPTURE QString t; // last string passed to QRegExp::search() or searchRev() QStringList capturedCache; // what QRegExp::capturedTexts() returned last #endif QMemArray<int> captured; // what QRegExpEngine::search() returned last QRegExpPrivate() { captured.fill( -1, 2 ); } }; #ifndef QT_NO_REGEXP_OPTIM static QCache<QRegExpEngine> *engineCache = 0; static QSingleCleanupHandler<QCache<QRegExpEngine> > cleanup_cache; #endif static QRegExpEngine *newEngine( const QString& pattern, bool caseSensitive ) { #ifndef QT_NO_REGEXP_OPTIM if ( engineCache != 0 ) { #ifdef QT_THREAD_SUPPORT - QMutexLocker locker( qt_global_mutexpool->get( &engineCache ) ); + QMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( &engineCache ) : 0 ); #endif QRegExpEngine *eng = engineCache->take( pattern ); if ( eng == 0 || eng->caseSensitive() != caseSensitive ) { delete eng; } else { eng->ref(); return eng; } } #endif return new QRegExpEngine( pattern, caseSensitive ); } static void derefEngine( QRegExpEngine *eng, const QString& pattern ) { - if ( eng != 0 && eng->deref() ) { -#ifndef QT_NO_REGEXP_OPTIM #ifdef QT_THREAD_SUPPORT - QMutexLocker locker( qt_global_mutexpool->get( &engineCache ) ); + QMutexLocker locker( qt_global_mutexpool ? + qt_global_mutexpool->get( &engineCache ) : 0 ); #endif + if ( eng != 0 && eng->deref() ) { +#ifndef QT_NO_REGEXP_OPTIM if ( engineCache == 0 ) { engineCache = new QCache<QRegExpEngine>; engineCache->setAutoDelete( TRUE ); cleanup_cache.set( &engineCache ); } if ( !pattern.isNull() && engineCache->insert(pattern, eng, 4 + pattern.length() / 4) ) return; #else Q_UNUSED( pattern ); #endif delete eng; } } /*! \enum QRegExp::CaretMode The CaretMode enum defines the different meanings of the caret (<b>^</b>) in a regular expression. The possible values are: \value CaretAtZero The caret corresponds to index 0 in the searched string. \value CaretAtOffset The caret corresponds to the start offset of the search. \value CaretWontMatch The caret never matches. */ /*! Constructs an empty regexp. \sa isValid() errorString() */ QRegExp::QRegExp() { eng = new QRegExpEngine( TRUE ); priv = new QRegExpPrivate; #ifndef QT_NO_REGEXP_WILDCARD priv->wc = FALSE; #endif priv->min = FALSE; compile( TRUE ); } /*! @@ -3520,226 +3542,212 @@ void QRegExp::setMinimal( bool minimal ) \sa search() searchRev() QRegExpValidator */ bool QRegExp::exactMatch( const QString& str ) const { #ifndef QT_NO_REGEXP_CAPTURE priv->t = str; priv->capturedCache.clear(); #endif priv->captured = eng->match( str, 0, priv->min, TRUE, 0 ); if ( priv->captured[1] == (int) str.length() ) { return TRUE; } else { priv->captured.detach(); priv->captured[0] = 0; priv->captured[1] = eng->matchedLength(); return FALSE; } } #ifndef QT_NO_COMPAT /*! \obsolete Attempts to match in \a str, starting from position \a index. Returns the position of the match, or -1 if there was no match. The length of the match is stored in \a *len, unless \a len is a null pointer. If \a indexIsStart is TRUE (the default), the position \a index in the string will match the start of string anchor, <b>^</b>, in the regexp, if present. Otherwise, position 0 in \a str will match. Use search() and matchedLength() instead of this function. \sa QString::mid() QConstString */ int QRegExp::match( const QString& str, int index, int *len, bool indexIsStart ) const { int pos = search( str, index, indexIsStart ? CaretAtOffset : CaretAtZero ); if ( len != 0 ) *len = matchedLength(); return pos; } #endif // QT_NO_COMPAT -/*! - \overload - - This convenience function searches with a \c CaretMode of \c - CaretAtZero which is the most common usage. -*/ - int QRegExp::search( const QString& str, int offset ) const { return search( str, offset, CaretAtZero ); } /*! Attempts to find a match in \a str from position \a offset (0 by default). If \a offset is -1, the search starts at the last character; if -2, at the next to last character; etc. Returns the position of the first match, or -1 if there was no match. The \a caretMode parameter can be used to instruct whether <b>^</b> should match at index 0 or at \a offset. You might prefer to use QString::find(), QString::contains() or even QStringList::grep(). To replace matches use QString::replace(). Example: \code QString str = "offsets: 1.23 .50 71.00 6.00"; QRegExp rx( "\\d*\\.\\d+" ); // primitive floating point matching int count = 0; int pos = 0; while ( (pos = rx.search(str, pos)) != -1 ) { count++; pos += rx.matchedLength(); } // pos will be 9, 14, 18 and finally 24; count will end up as 4 \endcode Although const, this function sets matchedLength(), capturedTexts() and pos(). \sa searchRev() exactMatch() */ int QRegExp::search( const QString& str, int offset, CaretMode caretMode ) const { if ( offset < 0 ) offset += str.length(); #ifndef QT_NO_REGEXP_CAPTURE priv->t = str; priv->capturedCache.clear(); #endif priv->captured = eng->match( str, offset, priv->min, FALSE, caretIndex(offset, caretMode) ); return priv->captured[0]; } -/*! - \overload - - This convenience function searches with a \c CaretMode of \c - CaretAtZero which is the most common usage. -*/ - int QRegExp::searchRev( const QString& str, int offset ) const { return searchRev( str, offset, CaretAtZero ); } /*! Attempts to find a match backwards in \a str from position \a offset. If \a offset is -1 (the default), the search starts at the last character; if -2, at the next to last character; etc. Returns the position of the first match, or -1 if there was no match. The \a caretMode parameter can be used to instruct whether <b>^</b> should match at index 0 or at \a offset. Although const, this function sets matchedLength(), capturedTexts() and pos(). \warning Searching backwards is much slower than searching forwards. \sa search() exactMatch() */ int QRegExp::searchRev( const QString& str, int offset, CaretMode caretMode ) const { if ( offset < 0 ) offset += str.length(); #ifndef QT_NO_REGEXP_CAPTURE priv->t = str; priv->capturedCache.clear(); #endif if ( offset < 0 || offset > (int) str.length() ) { priv->captured.detach(); priv->captured.fill( -1 ); return -1; } while ( offset >= 0 ) { priv->captured = eng->match( str, offset, priv->min, TRUE, caretIndex(offset, caretMode) ); if ( priv->captured[0] == offset ) return offset; offset--; } return -1; } /*! Returns the length of the last matched string, or -1 if there was no match. \sa exactMatch() search() searchRev() */ int QRegExp::matchedLength() const { return priv->captured[1]; } #ifndef QT_NO_REGEXP_CAPTURE -/*! +/*! Returns the number of captures contained in the regular expression. */ int QRegExp::numCaptures() const { return eng->numCaptures(); } /*! Returns a list of the captured text strings. The first string in the list is the entire matched string. Each subsequent list element contains a string that matched a (capturing) subexpression of the regexp. For example: \code QRegExp rx( "(\\d+)(\\s*)(cm|inch(es)?)" ); int pos = rx.search( "Length: 36 inches" ); QStringList list = rx.capturedTexts(); // list is now ( "36 inches", "36", " ", "inches", "es" ) \endcode The above example also captures elements that may be present but which we have no interest in. This problem can be solved by using non-capturing parentheses: \code QRegExp rx( "(\\d+)(?:\\s*)(cm|inch(?:es)?)" ); int pos = rx.search( "Length: 36 inches" ); QStringList list = rx.capturedTexts(); // list is now ( "36 inches", "36", "inches" ) \endcode Note that if you want to iterate over the list, you should iterate over a copy, e.g. \code QStringList list = rx.capturedTexts(); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; } \endcode Some regexps can match an indeterminate number of times. For example if the input string is "Offsets: 12 14 99 231 7" and the |