in reply to help with a regexp

#!/usr/bin/perl -wl use strict; my $foo="xoxoxoxoxoxoxoxoxoxoxoxoxoxoxox"; $foo=~s/^(.)(.+?)(.)$/\U$1\E\L$2\E\U$3\E/; print $foo;
This works!

Replies are listed 'Best First'.
Re: Re: help with a regexp
by Helter (Chaplain) on Sep 26, 2002 at 19:39 UTC
    This soluction does work, but it does force all the letters inbetween the first and last to lowercase. If you want to leave them alone then you should use this:

    #!/usr/bin/perl -wl use strict; my $foo="xoxoxoxoxoxoxoxoxoxoxoxoxoxoxox"; $foo =~ s/^(.)(.*)(.)$/\U$1\E$2\U$3/; print "$foo\n"; #Output: XoxoxoxoxoxoxoxoxoxoxoxoxoxoxoX my $bar="xoXOxoXOxoXOxoXOxoXOxoXOxo"; $bar =~ s/^(.)(.*)(.)$/\U$1\E$2\U$3/; print "$bar\n"; #Ooutput: XoXOxoXOxoXOxoXOxoXOxoXOxO

    The original question was worded with what to do with the middle segment was up in the air. Just trying to add clarification for posterity :)

      Both your solution and husoft's fail on a single character string. As "x" consists of a single character and that character is both the first and the last it should be capitalized.

      -sauoq
      "My two cents aren't worth a dime.";
      
Re: Re: help with a regexp
by sauoq (Abbot) on Sep 26, 2002 at 19:46 UTC

    As I mentioned in my reply to Helter, your solution fails on a single character string. Worse, it requires a string to have 3 characters at a minimum. (I also agree that lowercasing the middle portion is probably wrong.)

    -sauoq
    "My two cents aren't worth a dime.";
    
      I dont think that lowercasing the middle is wrong.
      $foo="XOXOXOXOXOXOXOXOXOXOXOXOXOXXO";
      you will need to lowercase the middle!...