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

I was looking at Config::OpenSSH::Authkey and in its parse method I see this line:
if ( $data =~ m/^\s*(?:#|$)/ ) {
Used to match blank or comment lines, I think.

I don't understand the usage of ?: in the grouping. Does not seem to make a difference in my testing.

So, what is the ?: for and why did they use it?

Also where is the construct "?:" documented? I have searched around but it is a hard term to search for.

Thanks in advance

Replies are listed 'Best First'.
Re: Explain regex ?: usage in grouping
by kennethk (Abbot) on Oct 17, 2010 at 14:31 UTC
    To simplify Anonymous Monk's response, it is not the grouping of ?: but (?:...). By adding the ?:, you tell the regular expression engine to group (like mathematical parentheses) but not to store the result in a capture buffer. There are a number of side effects that might make you want to pick one or the other, but you are correct that it does not modify the gross behavior of the above snippet. They are documented in Non capturing groupings in perlretut.
Re: Explain regex ?: usage in grouping
by moritz (Cardinal) on Oct 17, 2010 at 14:32 UTC

    It's (?: ... ) that forms a semantic unit, not the ?: itself. The (?: ... ) is documented in perlre as a non-capturing group.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Explain regex ?: usage in grouping
by Anonymous Monk on Oct 17, 2010 at 13:51 UTC
    $ perl -MYAPE::Regex::Explain -le"print YAPE::Regex::Explain->new(qr/^ +\s*(?:#|$)/ )->explain" The regular expression: (?-imsx:^\s*(?:#|$)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- \s* whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- # '#' ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    perlre, perlretut, YAPE::Regex::Explain, Tutorials: Pattern Matching, Regular Expressions, and Parsing
Re: Explain regex ?: usage in grouping
by aquarium (Curate) on Oct 18, 2010 at 00:43 UTC
    (?:xxxxx) and friends are perl extensions to regex syntax, and do not capture. there's also similar looking ones for doing look-ahead/look-behind matching without eating up the string, and also posix and other convenience ones to match alphanumeric or numeric etc characters on multi-lingual (utf8) input.
    the hardest line to type correctly is: stty erase ^H
Re: Explain regex ?: usage in grouping
by ikegami (Patriarch) on Oct 18, 2010 at 05:09 UTC