Re: help with a regexp
by fruiture (Curate) on Sep 26, 2002 at 19:51 UTC
|
$ perl -e '$_ = "xoxo";$_=ucfirst(reverse(ucfirst(reverse($_))));print
+'
Yes, this _is_ faster than any regular expression.
--
http://fruiture.de | [reply] [d/l] |
Re: help with a regexp
by husoft (Monk) on Sep 26, 2002 at 19:17 UTC
|
#!/usr/bin/perl -wl
use strict;
my $foo="xoxoxoxoxoxoxoxoxoxoxoxoxoxoxox";
$foo=~s/^(.)(.+?)(.)$/\U$1\E\L$2\E\U$3\E/;
print $foo;
This works!
| [reply] [d/l] |
|
|
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 :)
| [reply] [d/l] |
|
|
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
|
|
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.";
| [reply] |
|
|
I dont think that lowercasing the middle is wrong.
$foo="XOXOXOXOXOXOXOXOXOXOXOXOXOXXO";
you will need to lowercase the middle!...
| [reply] |
Re: help with a regexp
by grep (Monsignor) on Sep 26, 2002 at 20:14 UTC
|
You can avoid regex's altogether
#!/usr/bin/perl
use strict;
use warnings;
my @strings = qw/foo BAR Baz blah q/;
foreach (@strings) {
# leave string as is
print ucfirst(reverse(ucfirst(reverse()))),"\n";
# or lc everything else
print ucfirst(reverse(ucfirst(reverse(lc())))),"\n";
}
UPDATE: well that serves me right for not refreshing my browser, looks like I had the same idea as fruiture
grep
|
Mynd you, mønk bites Kan be pretti nasti... |
| [reply] [d/l] |
Re: help with a regexp
by sauoq (Abbot) on Sep 26, 2002 at 19:40 UTC
|
I would do it in two steps.
perl -le '$_="xooox"; $_ = ucfirst; s/(.)$/\U$1\E/; print'
Update: Well, maybe one step:
perl -le '$_="xoox"; s/(^.|.$)/\U$1\E/g; print'
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
Re: help with a regexp
by diotalevi (Canon) on Sep 26, 2002 at 20:35 UTC
|
Don't use a regexp, you don't need it and it'll only make things more complicated. Just alter each character individually.
for (0, -1)
{
$bar = substr $foo, $_, 1;
$foo = substr($foo, $_, 1) = uc bar;
}
| [reply] [d/l] |
|
|
But if you really do want to use a Regex the following will work and does so for the special cases of one or two letter words.
s/\b\w|\w\b/\U$&\E/g
or if you don't want "toasted weasels" to go to "ToasteD WeaselS" but "Toasted weaselS" then:
s/\A\w|\w\z/\U$&\E/g
Dedalus. | [reply] [d/l] [select] |
Re: help with a regexp
by Anonymous Monk on Sep 26, 2002 at 21:31 UTC
|
I'll stay with the husoft one! Thanks everyone! | [reply] |