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

Dear Monks

consider the following example
my $str = "abc def 123 456" ; $str =~ s/(?:\s)\w/_/g ; print $str ;
which gives me: abc_ef_23_56, but I expected abc _ef _23 _56
From this I would say that (?:\s) is capturing. So I must do something wrong. Any comments?

thnx a lot
LuCa

Replies are listed 'Best First'.
Re: is (?: ... ) capturing
by ysth (Canon) on Sep 07, 2009 at 07:35 UTC
    Capturing refers to data that is put into $1, $2, etc. or returned from a list context m//. You aren't using the result of a capture there, so capturing or not isn't relevant. Even though (?: ) doesn't capture, it does consume part of the string and usually becomes part of what s/// will replace. You can avoid that with a lookbehind instead: s/(?<=\s)\w/_/g. Or, in 5.10+, the \K feature: s/\s\K\w/_/g. See perlre for details.
    --
    A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
    Online Fortune Cookie Search
    Office Space merchandise
      thnx a lot!!
      I'm using 5.6, so if I had to do something like
      my $str = "abc def 123 456" ; $str =~ s/(?:\s+)\w/_/g ;
      I would have had to do it differently, correct ? cheers
Re: is (?: ... ) capturing
by Anonymous Monk on Sep 07, 2009 at 07:34 UTC
    Capturing refers to storing values in $1,$2... is not capturing.