CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
how will i get rid of this --> ' <--

 



siskos1
Novice

Mar 12, 2010, 6:13 PM

Post #1 of 2 (2745 views)
how will i get rid of this --> ' <-- Can't Post

hi, i need some help. any help will be great. i am doing an exercise and it is; "read sentences from a text file and write the first letters of each word in each sentence and the number of words in the sentence to a text file. You will also convert the letters to uppercase. The sentences will end with dot."

Sample input file (input.txt)
The tanker traffic on the Bosphorus becomes more dangerous day by day. An earthquake in Turkey's Elazig Province kills more than fifty people. The Hurt Locker wins six Academy Awards including Best Picture and the first female Best Director.

Sample output file (output.txt)
TTTOTBBMDDBD 12
AEITEPKMTFP 11
THLWSAAIBPATFFBD 16

----------
my code is;

use strict;
use warnings;

open( IN, 'input.txt');

$/ = undef;
my $string = <IN>; close (IN);



$string =~ s/\b(\w).+?\b/\u$1/g; $string =~ s/\s//g;

my @words = split (/\./,$string);



open (OUT, '>output.txt');

foreach(@words) { my $a=length($_); print (OUT "\n$_ $a"); }

close(OUT);

----------------

however output becomes like this;

TTTOTBBMDDBD 12
AEIT'SEPKMTFP 13
THLWSAAIBPATFFBD 16

------------

i want my code to eleminate " ' " (and the letter after that) in the first place. how can i change my
$string =~ s/\b(\w).+?\b/\u$1/g; not to include them. and what is the logic behind it that . doesnt cover ' ?


thanks to anyone who read till here.


(This post was edited by siskos1 on Mar 12, 2010, 6:16 PM)


shawnhcorey
Enthusiast


Mar 13, 2010, 7:04 AM

Post #2 of 2 (2724 views)
Re: [siskos1] how will i get rid of this --> ' <-- [In reply to] Can't Post


In Reply To
i want my code to eleminate " ' " (and the letter after that) in the first place. how can i change my
$string =~ s/\b(\w).+?\b/\u$1/g; not to include them. and what is the logic behind it that . doesnt cover ' ?


The pattern \b means to look for a word boundary, that is, a place where a \W is followed by a \w or where a \w is followed by a \W. It is the second one that is messing up. Try this:

Code
$string =~ s/\b(\w)[\w']*/\u$1/g;

This will look for a start of a word and eat up any alphanumeric or apostrophe character.

See:
perldoc perlretut
perldoc perlre

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives