Page 1 of 1

QRegularExpressionmatch documentation bug

Posted: Sat Nov 13 2021 9:28 pm
by seasoned_geek
https://www.copperspice.com/docs/cs_api/class_qregularexpressionmatch.html

The very first example

Code: Select all

if (match.hasMatch()) {
    QString number = match.captured(1);                     // first equals "35"
    QString name   = match.captured("name");                // name  equals "Bob"
}
Does not jive with the documentation for captured() which states indexes start at 0 instead of 1.

Re: QRegularExpressionmatch documentation bug

Posted: Tue Nov 16 2021 6:04 am
by barbara
Looking at the full example we noticed the comment for "first equals "35" should have said "number equals "35". This is being corrected in the documentation.

Calling "match.captured(0)" will always returns the entire match. In this example this returns the string "35 Bob".

If you use "match.captured(1)" the return value is "35" and for "match.captured(2)" it returns "Bob". Any index value greater than 2 returns an empty string.

Can you let us know where you saw "indexes start at 0"?

Barbara

Re: QRegularExpressionmatch documentation bug

Posted: Sat Nov 20 2021 2:45 pm
by seasoned_geek
https://www.copperspice.com/docs/cs_api/class_qregularexpressionmatch.html#aac85caf16f5fa718565f09d336d33bf2

S QRegularExpressionMatch< S >::captured ( int index = 0 ) const

Re: QRegularExpressionmatch documentation bug

Posted: Sat Nov 20 2021 6:52 pm
by barbara
The code you quoted which says int index = 0 indicates the default value when calling this method.

If you do not pass a value for the parameter it will return the entire match. When you want to return the first capture pass one, to return the second capture pass two, etc.

Barbara

Re: QRegularExpressionmatch documentation bug

Posted: Wed Dec 01 2021 1:14 am
by barbara
S QRegularExpressionMatch< S >::captured ( int index = 0 ) const

Every other place in the doc index=0 means that indexes start at zero.
In C++ default arguments are a value provided in a method or function declaration. The default value is automatically assigned by the compiler if the caller of the function does not provide a value for the argument.

No where in our documentation does a default value indicate the starting value for the argument. There are places where we set the default value for an argument to some given Enum value. This does not indicate it is the "first enum value" but rather one possibility.

The value of ZERO in this particular method has a meaning that the entire match will be returned. To return the contents of the first capture group you would need to pass 1. We will amend our documentation for this method to clarify the difference between index with a value of 0 and a value of 1.