Simple Regex Problem

R

Robert

I am struggling with the following (simple) problem.
Given a list of vehicles I need to capitalise the first letter of the
make and model and change the spec to uppercase.

eg.
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX

The closest I can get is this but it capitalises every word.

$model =~ s/(\w+)/\u\L$1/g;

Any pointers would be appreciated.
 
A

Anno Siegel

Robert said:
I am struggling with the following (simple) problem.

How do you know it's simple if you're still struggling?
Given a list of vehicles I need to capitalise the first letter of the
make and model and change the spec to uppercase.

eg.
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX

How do you recognize which part is make, model, and spec? I'll assume
it's always the first, second, and fourth of blank-separated words.
The closest I can get is this but it capitalises every word.

$model =~ s/(\w+)/\u\L$1/g;

Well, what did you expect? s///g treats all matches alike (/g stands
for global), so the result is the same for all. You must treat the
parts separately. Here is one way:

$_ = 'ford mondeo 2.0 glx';
my @parts = split;
$_ = ucfirst( $_) for @parts[ 0, 1];
$_ = uc( $_) for $parts[ 3];
# less consistent, but more conventional: $parts[ 3] = uc $parts[ 3];
my $trans = join ' ', @parts;
print "$_ -> $trans\n";

Anno
 
L

Lack Mr G M

|>
|> Given a list of vehicles I need to capitalise the first letter of the
|> make and model and change the spec to uppercase.

Assuming you *always* have a make and model and these are single words.

perl -lpe 's/^(.+?)\s(.+?)\s(.*)/\u\L$1\E \u\L$2\E \U$3/'
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX
FORD MONDEO 2.0 glx
Ford Mondeo 2.0 GLX
 
T

Tad McClellan

Robert said:
Given a list of vehicles I need to capitalise the first letter of the
make and model and change the spec to uppercase.


How can you tell which part is "the spec" ?

I'll assume it is always the last thing.

eg.
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX

The closest I can get is this but it capitalises every word.

$model =~ s/(\w+)/\u\L$1/g;


$model =~ s/(\w+)$/\U$1/g; # uc "the spec"
 
J

James E Keenan

Robert said:
I am struggling with the following (simple) problem.
Given a list of vehicles I need to capitalise the first letter of the
make and model and change the spec to uppercase.

eg.
ford mondeo 2.0 glx
Ford Mondeo 2.0 GLX

The closest I can get is this but it capitalises every word.

$model =~ s/(\w+)/\u\L$1/g;

Any pointers would be appreciated.

Have you consulted this item in the docs?

perldoc -f ucfirst
 
T

Tad McClellan

^^
^^ aka: ucfirst()
Have you consulted this item in the docs?

perldoc -f ucfirst


He is already using ucfirst().

Your followup does not address the question that was asked, namely
how to treat the "spec" differently from the make and model.

(the "spec" could be called a "trim level" in the automotive biz)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top