Re: RegEx Problem
by Kanji (Parson) on May 29, 2002 at 00:38 UTC
|
s/\B([A-Z])(?=[a-z])/-\l$1/g; # ?
Although if you can guarantee that a capital letter will never occur at the beginning of the string and will always denote a new word (ie, samIAm), you can simplify that to just...
s/([A-Z])/-\l$1/g;
--k.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
$moo = 'boo-hoo-hoo';
$hoo = lcfirst join '', map { ucfirst $_ } split /-/, $moo;
To do that exact reverse of the above... is there a better way (I'm sure there is)? | [reply] [Watch: Dir/Any] [d/l] |
|
$str =~ s/-([a-z])/uc $1/ge;
/me crosses fingers
Update: Oh dang. Damn you, Kanji ;) | [reply] [Watch: Dir/Any] [d/l] |
|
Re: RegEx Problem
by clintp (Curate) on May 29, 2002 at 00:39 UTC
|
s/([A-Z])/-lc($1)/eg
Near as I can tell. | [reply] [Watch: Dir/Any] [d/l] |
|
s/([A-Z])/-lc($1)/eg
Near as I can tell.
-i think it loses on an edge condition.
| [reply] [Watch: Dir/Any] [d/l] |
Re: RegEx Problem
by dws (Chancellor) on May 29, 2002 at 06:21 UTC
|
Here's one I hadn't tried before -- /e nested within /e.
$phrase = "NoMatch cowsCanFly sheepAreVeryCool NoMatch";
$phrase =~ s{
\b
(
[a-z]+
(?:[A-Z][a-z]+)+
)
\b
}{
my $word = $1;
$word =~ s/([A-Z])/"-" . lc($1)/eg;
$word;
}gex;
print $phrase, "\n";
| [reply] [Watch: Dir/Any] [d/l] |
|
If that works it's only because you got lucky. In general the regex engine is not reentrant. Seg-faults are the most common result of this kind of code...
-sam
| [reply] [Watch: Dir/Any] |
|
my $str = "foo bar baz quux";
$str =~ s<([a-z]+)
(?{ s| \1|\L$&|; })><\U$1>xg;
But this is fine
$str =~ s<([a-z]+ ?)><local $_ = $1; s/$1/\U$&/g; $_>eg;
HTH
_________ broquaint
[1] in theory, I'll be happy to change it to a proper re-entrant regex if someone will provide me with one | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
Re: RegEx Problem
by tea-ice (Initiate) on May 29, 2002 at 09:36 UTC
|
Hi guys, new to the boards, and yes this is my first post. LP try this. I have tested it and should work for you as well.
$word=<STDIN>;
chomp ($word);
$word=~s/[A-Z]/-$&/g; #converts A to -A
$word=~tr [A-Z][a-z]; #converts -A to -a
print $word
| [reply] [Watch: Dir/Any] [d/l] |
Re: RegEx Problem
by lestrrat (Deacon) on May 29, 2002 at 00:40 UTC
|
my $str = "cowsCanFly";
$str =~ s/\G([A-Z]?[a-z])/lcfirst($1) . '-'/eg;
chop($str);
print $str, "\n";
update: man, I feel ashamed posting this. Kanji++ | [reply] [Watch: Dir/Any] [d/l] |
Re: RegEx Problem
by marvell (Pilgrim) on May 29, 2002 at 11:00 UTC
|
$word =~ s/[A-Z]/-\l$&/g;
--
Steve Marvell
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |