Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: is (?: ... ) capturing

by ysth (Canon)
on Sep 07, 2009 at 07:35 UTC ( #793895=note: print w/replies, xml ) Need Help??


in reply to is (?: ... ) capturing

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

Replies are listed 'Best First'.
Re^2: is (?: ... ) capturing
by jeanluca (Deacon) on Sep 07, 2009 at 07:45 UTC
    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
        s/(\s+)\w/${1}_/g
        simplifies to
        s/(\s)\w/${1}_/g
        Now you can use a zero-width lookbehind to get the speed boost of avoiding captures.
        s/(?<=\s)\w/_/g
        Or in 5.10+:
        s/\s\K\w/_/g
        ok, thnx again!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://793895]
help
Chatterbox?
[jdporter]: I for one think it's kind of ridiculous that people can't ask - here - other people for help just because it involves monetary compensation.
[jdporter]: I think the test for whether to push it to jobs.perl.com should be strict: (a) you're looking for employment, or (b) you're looking to hire an employee or contractor.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-03-23 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (278 votes). Check out past polls.