hoppfrosch has asked for the wisdom of the Perl Monks concerning the following question:

Looking through various Regexp::Common submodules, I very often saw the usage of "?k:" within regex definitions:
# from Regexp::Common::Comment: sub to_eol ($) {"(?k:(?k:$_[0])(?k:[^\\n]*)(?k:\\n))"} # from Regexp::Common::ZIP Monaco => "(?k:980[0-9][0-9])",
(much more examples could be given)
Can anybody tell me what's the function of "?k:" within regexes? Haven't found anything enlightening yet.

TIA

Replies are listed 'Best First'.
Re: Regex: What does ?k: mean?
by Corion (Patriarch) on Feb 24, 2017 at 08:05 UTC

    See the documentation of Regexp::Common, where ?k: is documented under "adding new regular expressions". It seems to be internally used by Regexp::Common to implement the -keep feature.

      For the curious (like me), the specific code that implements this appears to be:

      if (exists $flags{-keep}) { $pat =~ s/\Q(?k:/(/g; } else { $pat =~ s/\Q(?k:/(?:/g; }

      Update: Added link

      Thanks! I haven't seen the wood for the trees - and thought it's an undocumented regex feature ...
Re: Regex: What does ?k: mean?
by davido (Cardinal) on Feb 24, 2017 at 23:37 UTC

    If your goal at looking through the source is to discover what regexp the module will use for a certain type of match, this is the trick I use:

    perl -MRegexp::Common=RE_ALL -E 'say $RE{num}{real}' (?:(?i)(?:[-+]?)(?:(?=[.]?[0123456789])(?:[0123456789]*)(?:(?:[.])(?:[ +0123456789]{0,}))?)(?:(?:[E])(?:(?:[-+]?)(?:[0123456789]+))|))

    Of course there are many other reasons for reading the source. I've just found this shortcut useful when I want to try to understand the behavior of the patterns that are being used, or when an expression's behavior is almost but not quite what I need.


    Dave

        Regexp::Common isn't really a go-to for debugging. I guess what I meant to convey was that if I have a Regexp::Common call that isn't matching as I expected and want to see what the pattern looks like, that's an easy way to dump it (diving into the source can be opaque if all you want is to see what the pattern looks like).

        For debugging patterns, I really like Regexp::Debugger. It allows you to step through the match and watch what's happening.

        You are correct that YAPE::Regex::Explain is outdated. The main problem is that the last time I checked, it stopped being developed during Perl 5.6's reign, and consequently doesn't know about features that were added to Perl after 5.6. Since Perl 5.6 is now 14 years in the past, it would seem that YAPE::Regex::Explain has been left behind. In fact, after 2001 nothing new happened to the module until 2010, and that flurry of activity (two releases) was just for bugfixes and documentation enhancements. One of the doc enhancements was to explicitly state that only regexes using constructs that were in 5.6 are supported. It's useful to those starting out trying to learn regexp basics, but it doesn't take long to discover newer constructs that fool it.


        Dave