Logo Search packages:      
Sourcecode: qt-x11-free version File versions

QStringList QRegExp::capturedTexts (  ) 

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:

      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" )

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:

      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" )

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

      QStringList list = rx.capturedTexts();
      QStringList::Iterator it = list.begin();
      while( it != list.end() ) {
          myProcessing( *it );
          ++it;
      }

Some regexps can match an indeterminate number of times. For example if the input string is "Offsets: 12 14 99 231 7" and the regexp, {rx}, is (\d+)+, we would hope to get a list of all the numbers matched. However, after calling {rx.search(str)}, capturedTexts() will return the list ( "12", "12" ), i.e. the entire match was "12" and the first subexpression matched was "12". The correct approach is to use cap() in a loop .

The order of elements in the string list is as follows. The first element is the entire matching string. Each subsequent element corresponds to the next capturing open left parentheses. Thus capturedTexts()[1] is the text of the first capturing parentheses, capturedTexts()[2] is the text of the second and so on (corresponding to $1, $2, etc., in some other regexp languages).

See also:
cap() pos() exactMatch() search() searchRev()

Definition at line 3795 of file qregexp.cpp.

References QString::fromLatin1().

Referenced by cap().

{
    if ( priv->capturedCache.isEmpty() ) {
      for ( int i = 0; i < (int) priv->captured.size(); i += 2 ) {
          QString m;
          if ( priv->captured[i + 1] == 0 )
            m = QString::fromLatin1( "" );
          else if ( priv->captured[i] >= 0 )
            m = priv->t.mid( priv->captured[i],
                         priv->captured[i + 1] );
          priv->capturedCache.append( m );
      }
      priv->t = QString::null;
    }
    return priv->capturedCache;
}


Generated by  Doxygen 1.6.0   Back to index