Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

can regex contains function?

by toothedsword (Sexton)
on Jun 19, 2020 at 07:11 UTC ( #11118253=perlquestion: print w/replies, xml ) Need Help??

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

can regex contains function? I want convert the first character of words upcase. The code below does not work.

#!/usr/bin/env perl my $t = ' aa bb cc'; $t =~ s/(\s)(\w)/${1}uc($2)/g; print($t);

Replies are listed 'Best First'.
Re: can regex contains function?
by choroba (Archbishop) on Jun 19, 2020 at 07:49 UTC
    > can regex contains function?

    Yes, it can. That's what /e is for.

    $t =~ s/(?<=\s)(.)/uc $1/ge;

    But you don't need it here:

    $t =~ s/(?<=\s)(.)/\U$1/g;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: can regex contains function?
by soonix (Canon) on Jun 19, 2020 at 07:29 UTC
      Thank you so much! Now I know how to solve this specific question. But do you know can I instert a function to the regex?

        If I understand your question, the first responder answered it: use the /e qualifier.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11118253]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-12-02 06:52 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (35 votes). Check out past polls.