Reputation: 22094
I have the following function which should give me the matched string from a regular expression:
QString selectByPattern(QString const &oValue, QString const &oPattern, bool bRegularExpression) const
{
QString s;
QRegExp regex;
// default
regex.setPatternSyntax(QRegExp::RegExp);
regex.setPattern(oPattern);
int i = oValue.indexOf(regex);
int l = regex.matchedLength();
if(i == -1 || l < 1)
return s;
s = oValue.mid(i, l);
return s;
}
I'm using the string *VALUE*
as input for testing this. Now i get the following results:
oPattern = "[A-Z]"
Output = "V"
which is correct. For this pattern it gives me the first upper case character and a length of 1.
But when I use this:
oPattern = "[A-Z]*"
Output = ""
and I don't understand why. The index returned is 0
and length is also 0
. First of all, of course the index is wrong, because it should be 1
. And then I don't understand the meaning of 0
as length. According to the documentation (http://qt-project.org/doc/qt-4.8/qregexp.html#pattern) Either there is a match, then the index is a positive value, and in that case I would expect that matchedLength()
should also return some value `> 0``.
Am I missunderstanding something here, or is this a problem in Qt 5.2.1?
Upvotes: 0
Views: 300
Reputation: 67988
[A-Z]*
can matched the string VALUE
and an empty string as well. See demo. The second match will be of 0
length as after VALUE
is matched nothing is left.
You can try [A-Z]+
Your index is 0, because positions in strings start with 0.
string: "VALUE"
^ ^
position: 0 4
So the match (pos=0
, len=3
) would be the following VAL
, (pos=0
, len=1
) is V
and (pos=0
, len=0
) is the empty string.
Upvotes: 3