in reply to Search for a character in CAPS

<update>

Untested code rocks. :) dvergin is absolutely correct, this code will not work. I will leave it as is, to serve as a warning to others about posting without testing. Thanks for the lesson, dvergin. :)

And now back to our regular show...

</update>

I would try something like:

$agent =~ s/.([A-Z])/ $1/g;
Which should insert a space in front of each capital letter, except if it is at the start of the string (the dot makes sure there must be at least one letter of any kind in front of the matched uppercase letter).

If you don't want to insert when there are capital letters standing together, like in CIA => C I A, you could try something like:

$agent =~ s/[^A-Z]([A-Z])/ $1/g;
Which should make sure it matches an uppercase letter, that is preceded with anything but an uppercase letter.

Hope I understood the question correctly. :)


You have moved into a dark place.
It is pitch black. You are likely to be eaten by a grue.

Replies are listed 'Best First'.
Re: Re: Search for a character in CAPS
by dvergin (Monsignor) on Mar 07, 2002 at 16:58 UTC
    You did not test your code. The character matched by the dot is being lost in the replacement. The following snippet:
    my $agent = 'PerlBeginnerPerl'; $agent =~ s/.([A-Z])/ $1/g; print $agent;
    produces: Per Beginne Perl

    To make your approach work, you would need: $agent =~ s/(.)([A-Z])/$1 $2/g;

      The code definitely did work. Thanks a lot. c_lhee@yahoo.com