Andrew Taylor
Andrew Taylor

Reputation: 628

Remove all uppercase letters in a string except the first letter using perl

I have a word list imported into an array. Many of the words start with a capital letter. So many of the words may start with an uppercase letter, this is okay. I want to delete all occurrences of uppercase letters unless it's the first letter of the word.

use strict;

my $filter_file = "filter.txt";
my $filtered_file = "filtered.txt";
my $file_data= "";


#### Place file contents into an array ####
open (RESULTS, $filter_file) or die "Unable to open file: $filter_file\n$!";
my @file_data;
@file_data = <RESULTS>;
close(RESULTS);

#### Search the array ####
for(@file_data) {
# attempting different regular expression's 
   s/[[:lower:]]\K[[:upper:]].*//;
   s/([[:lower:]])[[:upper:]].*/$1/;
    }

#### Save filtered data to file ###
open (FILE, ">> $filtered_file") || die "Unable to open file: $filtered_file\n$!";
print FILE @file_data;
close($filtered_file);

Upvotes: 1

Views: 877

Answers (1)

ikegami
ikegami

Reputation: 385764

What's your definition of a word? If you're just looking at letters, you almost had it. You want uppercase following any letter, not just lowercase letters.

$ perl -E'$_ = "ABcDe"; s/\pL\K\p{Lu}+//g; say'
Ace

Upvotes: 1

Related Questions