in reply to Search for a character in CAPS

In your example, I think you mean to use a character class, $agent =~ /[A-Z]/. Applying that to your question, you want to use the substitution operator:

$agent = 'PerlBeginnerPerl'; $agent =~ s/([A-Z])/ $1/g;
Taking that apart, the parens store the matched character in $1. A space followed by the value of $1 is substituted. The g flag is for global, it make the substitution apply all through the string..

This will insert a leading space if the first character is capitol. To avoid that we can use a more sophisticated gadget. The additional element is a negative lookbehind assertion that we're not at the beginning of the string:

$agent =~ s/(?<!^)([A-Z])/ $1/g;

Update: (Assuming my reading of the question was correct) The second version can be converted to capture nothing by using positive lookahead for the capitol letter:

$agent =~ s/(?<!^)(?=[A-Z])/ /g;
This does the insertion by looking at the surroundings of the notch before a character, and inserting a space if conditions warrant.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Search for a character in CAPS
by grummerX (Pilgrim) on Mar 07, 2002 at 10:37 UTC
    Conversely, you could use \B to match a non-boundary. This would still take into account the beginning of the string, while also allowing for spaces within the string.
    $agent = 'PerlBeginnerPerl AndAnother'; $agent =~ s/\B([A-Z])/ $1/g; # Produces 'Perl Beginner Perl And Another' $agent = 'PerlBeginnerPerl AndAnother'; $agent =~ s/\B([A-Z])/ $1/g; # Produces 'Perl Beginner Perl And Another'
    I realize that spaces within the string is probably outside the scope of the original question, but \B covers his bases and might be a little easier for a beginner to grok than a look-ahead.

    -- grummerX