QRegularExpressionmatch documentation bug

Discuss anything related to product development
Post Reply
seasoned_geek
Posts: 128
Joined: Thu Jun 11 2020 12:18 pm

QRegularExpressionmatch documentation bug

Post 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.

barbara
Posts: 285
Joined: Sat Apr 04 2015 2:32 am

Re: QRegularExpressionmatch documentation bug

Post 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

seasoned_geek
Posts: 128
Joined: Thu Jun 11 2020 12:18 pm

Re: QRegularExpressionmatch documentation bug

Post by seasoned_geek »

https://www.copperspice.com/docs/cs_api/class_qregularexpressionmatch.html#aac85caf16f5fa718565f09d336d33bf2

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

barbara
Posts: 285
Joined: Sat Apr 04 2015 2:32 am

Re: QRegularExpressionmatch documentation bug

Post 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

barbara
Posts: 285
Joined: Sat Apr 04 2015 2:32 am

Re: QRegularExpressionmatch documentation bug

Post 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.

Post Reply