dsalada has asked for the wisdom of the Perl Monks concerning the following question:

Please be patient with me. I am new to Perl and am used to working with PHP, so regexs are pretty foreign to me. I am trying to find instance in a string where a lowercase letter occurs next to an uppercase letter. After finding them, I want to put "::" between them. Here's my code:
$new_type = "BookInstructional MaterialTeaching Guide"; $new_type =~ s/([a-z])([A-Z])/$1::$2/g; print "$new_type\n";
The output looks shows the string unchanged, but I was hoping it would look like this: Book::Instructional Material::Teaching Guide Any help would be greatly appreciated!

Replies are listed 'Best First'.
Re: Finding lowercase letters next to uppercase letters
by blakem (Monsignor) on Oct 17, 2001 at 23:51 UTC
    I don't see anything wrong with that at all... In fact, when I run it, I get the exact string you're wanting it to create.....
    #!/usr/bin/perl -wT use strict; my $new_type = "BookInstructional MaterialTeaching Guide"; print "Before: $new_type\n"; $new_type =~ s/([a-z])([A-Z])/$1::$2/g; print "After: $new_type\n"; =OUTPUT Before: BookInstructional MaterialTeaching Guide After: Book::Instructional Material::Teaching Guide
    Update: The double colon is special in perl so if it really isn't working for you, I'd try to disambiguate /$1::$2/ using /${1}::${2}/ as in:
    $new_type =~ s/([a-z])([A-Z])/${1}::${2}/g;

    -Blake

(crazyinsomniac) Re: Finding lowercase letters next to uppercase letters
by crazyinsomniac (Prior) on Oct 18, 2001 at 03:41 UTC
    blakems update's on the money, but \\\\\\\\\\\\\\\\\\
    $new_type = "BookInstructional MaterialTeaching Guide"; $new_type =~ s/([a-z])([A-Z])/$1\:\:$2/g; # $1\:\:$2/g; ##\\\\\\ print "$new_type\n";
    ${1}::${2} is a pretty good way to disambiguate, I guess, but \:\: is 2 characters less (in case you ever wanna golf)

    You could even say

    $new_type =~ s/([a-z])([A-Z])/"$1::$2"/g; #"$1::$2" - strings are a monks best friend

    Please be patient with me. I am new to Perl and am used to working with PHP, so regexs are pretty foreign to me.

    So what you're saying is you need to try harder (at least that's what you're *supposed* to do ;D - I know i know, whatever....)

     
    ___crazyinsomniac_______________________________________
    Disclaimer: Don't blame. It came from inside the void

    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Finding lowercase letters next to uppercase letters
by runrig (Abbot) on Oct 18, 2001 at 03:55 UTC
    Just for kicks, and with extended character classes (5.005 or later? Not sure.):
    my $str = "AbcDefGhiJkl"; $str = join "::", split(/(?<=[[:lower:]])(?=[[:upper:]])/, $str); print "$str\n";
    Update: Ok, it is slower, but interesting nonetheless and still maybe worthwhile to use the [:lower:] and [:upper:] character classes even on blakem's answer.