|
agehacat 发表于 2013-9-10 23:44
楼主,你好.下面有个问题我没搞懂.
QString str3="One Eric another Eirik,and an Ericsson.How many Eiriks ...
这个是匹配字符串中第一个对应的字符串,并返回其位置。我们这里先获得了第一个位置,然后从该位置继续向后匹配,直到最后没有匹配到返回-1为止。
帮助中的说明:
int QRegExp::indexIn ( const QString & str, int offset = 0, CaretMode caretMode = CaretAtZero ) const
Attempts to find a match in str from position offset (0 by default). If 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 caretMode parameter can be used to instruct whether ^ should match at index 0 or at offset.
You might prefer to use QString::indexOf(), QString::contains(), or even QStringList::filter(). To replace matches use QString::replace().
Example:
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.indexIn(str, pos)) != -1) {
++count;
pos += rx.matchedLength();
}
// pos will be 9, 14, 18 and finally 24; count will end up as 4 |
|