Logo Search packages:      
Sourcecode: cb2bib version File versions

QStringList EQRegExp::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:

        EQRegExp rx("(\\d+)(\\s*)(cm|inch(es)?)");
        int pos = rx.indexIn("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:

        EQRegExp rx("(\\d+)(?:\\s*)(cm|inch(?:es)?)");
        int pos = rx.indexIn("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()) {

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.indexIn(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 {EQRegExp::cap_in_a_loop}{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()

Definition at line 3898 of file eqregexp.cpp.

Referenced by cap().

    if (priv->capturedCache.isEmpty()) {
        const QVector<int> &captured = priv->matchState.captured;
        int n = captured.size();

        for (int i = 0; i < n; i += 2) {
            QString m;
            if (captured.at(i + 1) == 0)
                m = QLatin1String(""); // ### Qt 5: don't distinguish between null and empty
            else if (captured.at(i) >= 0)
                m = priv->t.mid(captured.at(i), captured.at(i + 1));
    return priv->capturedCache;

Generated by  Doxygen 1.6.0   Back to index