Skip to content

[SEARCHING] Difference between \g and \k is not addressed. #896

Description

@pbarney

I just spent an hour delving into the possible differences between \g and \k, of which there are no satisfactory answers, and a few wrong answers. This eventually led me to digging into Boost's source code.

The short explanation is that there is no difference between \g and \k other than the history behind each.

Boost's source code treats them identically: regex_traits_defaults.hpp maps both characters to the exact same internal token:

escape_type_extended_backref, /*g*/
...
escape_type_extended_backref, /*k*/

...and basic_regex_parser.hpp has one single case for escape_type_extended_backref. It never checks if the original character was g or k.

After digging a little more, it turns out it's just a historical choice based on Perl's implementation that Boost chose to extend.

For this reason, I think it would be worthwhile mentioning this in the documentation.

My proposed change would be to add the following under the Capture Groups and Backreferences section of the Searching page:

  • \g and \k are functionally synonymous prefixes for extended backreferences. There is no semantic difference in Boost's implementation. They coexist for syntax-compatibility/historical reasons.

I'm about to make a PR for this change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions