Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

regular expression search and replace

by heman (Novice)
on Nov 18, 2014 at 17:05 UTC ( [id://1107597]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys,

I'm trying to read 5 lines and replace any character which starts with a lower case to upper case. and replace the remaining characters to lower case. ex:

perlmonk is a gREat site. i loVE it. in this case i expect the output to be: Perlmonk is a great site. I love it.

I'm trying to use the following string replacement scheme. however, i don't see the replacement of [A-Z] being recognized as a keyword. But, it does recognize in the 1st case where i'm searching for a-zA-Z.

s/^\s*[a-zA-Z]/[A-Z]/g

Not sure if i'm missing some detail.

Thanks

Replies are listed 'Best First'.
Re: regular expression search and replace
by hippo (Bishop) on Nov 18, 2014 at 17:55 UTC

    Your replacement has to be something tangible, not a pattern. However you can use the /e modifier to execute code in the replacement. Combining this with a capture group works like this:

    #!/usr/bin/perl use strict; use warnings; my $line = "perlmonk is a gREat site. i loVE it.\n"; print $line; $line = ucfirst lc $line; print $line; $line =~ s/(\. [a-z])/uc($1)/eg; print $line;

    Giving this output:

    perlmonk is a gREat site. i loVE it. Perlmonk is a great site. i love it. Perlmonk is a great site. I love it.

    Be sure to have a good read of perlre as regexes and the s/// function are bedrocks of Perl.

    HTH, Hippo

Re: regular expression search and replace
by GrandFather (Saint) on Nov 18, 2014 at 19:55 UTC

    Take a look at the transliteration operator (y/// or tr///) in Quote Like Operators. You'll need to scroll down a little of search for tr/.

    Perl is the programming world's equivalent of English
      Thanks guys :) All your inputs helped!! However, For some reason tr/ didn't work. It wouldn't recognize the \s in the replacement as space.

        Go read the documentation again. tr/ doesn't do \s. It isn't really related to regular expression processing, it just happens to use the same binding operator (=~).

        Perl is the programming world's equivalent of English
Re: regular expression search and replace
by AnomalousMonk (Archbishop) on Nov 19, 2014 at 00:17 UTC

    You seem to be parsing out sentences, and that's a tricky, natural language sort of thing to do, and perhaps not best done with regexen. If you can do it, the  \u \U \l \L "interpolation control" (as I think of them) operators or escape sequences may come in handy for the transformations you want (see Quote and Quote-like Operators in perlop — and don't forget  \Q \E too). You can avoid the  /e regex modifier. With a very naive definition of a sentence pattern:

    c:\@Work\Perl>perl -wMstrict -le "my $s = 'perlmonks IS a gREat site. dO you tHiNk SO? I loVE it!'; print qq{'$s'}; ;; my $senterm = '.?!'; my $sentence = qr{ \b [[:alpha:]] [^\Q$senterm\E]* [\Q$senterm\E] }xm +s; ;; $s =~ s{ ($sentence) }{\u\L$1}xmsg; print qq{'$s'}; " 'perlmonks IS a gREat site. dO you tHiNk SO? I loVE it!' 'Perlmonks is a great site. Do you think so? I love it!'

    An equivalent  /e substitution would be
        $s =~ s{ ($sentence) }{ ucfirst lc $1 }xmsge;
    Is there any significant speed difference?

Re: regular expression search and replace
by ronin78 (Novice) on Nov 18, 2014 at 17:56 UTC
    As I understand it, you want to capitalize the first letter in each sentence. You could do what you are trying to do above with a capture group (get the match and then uppercase it in your replace). However, you could also split the string into sentences, and then call uppercase on the first letter in the string and lowercase on the remainder.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1107597]
Approved by hippo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found